设计模式 – 策略模式(Strategy)
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
實(shí)例:商店促銷?
(1)首先考慮用簡單工廠模式進(jìn)行設(shè)計(jì)?
?? 客戶端代碼Main.java
package com.yilong.sell.factory;
public class Main {
??? public static void main(String[] args) {
?????? //SellMethod sellMethod = SellMethodFactory.create("原價(jià)");
?????? SellMethod sellMethod = SellMethodFactory.create("方案一");
?????? if(sellMethod != null) {
?????????? sellMethod.setPrePrice(10);
?????????? sellMethod.setAmount(10);
?????????? System.out.println("總價(jià):" + sellMethod.getSumPrice());
?????? } else {
?????????? System.out.println("暫未定義該銷售方案!");
?????? }
??? }??????????????????????????
}
?? 促銷形式類 SellMethod.java
package com.yilong.sell.factory;
public abstract class SellMethod {
??? private double prePrice;
??? private int amount;
??? public abstract double getSumPrice();
???
??? public int getAmount() {
?????? return amount;
??? }
??? public void setAmount(int amount) {
?????? this.amount = amount;
??? }
??? public double getPrePrice() {
?????? return prePrice;
??? }
??? public void setPrePrice(double prePrice) {
?????? this.prePrice = prePrice;
??? }
}
?? 原價(jià)銷售實(shí)現(xiàn)類 SellMethodPrototype.java
package com.yilong.sell.factory;
public class SellMethodPrototype extends SellMethod {
??? @Override
??? public double getSumPrice() {
?????? return this.getPrePrice() * this.getAmount();
??? }
}
?? 打折銷售實(shí)現(xiàn)類 SellMethodDiscount.java
package com.yilong.sell.factory;
public class SellMethodDiscount extends SellMethod {
??? public static final double DISCOUNT = 0.5;
??? @Override
??? public double getSumPrice() {
?????? return this.getPrePrice() * this.getAmount() * this.DISCOUNT;
??? }
}
?? 銷售方法生產(chǎn)類 SellMethodFactory.java
package com.yilong.sell.factory;
public class SellMethodFactory {
??? public static SellMethod create(String method) {
?????? SellMethod sellMethod = null;
?????? if(method.equals("原價(jià)")) {
?????????? sellMethod = new SellMethodPrototype();
?????? } else if(method.equals("方案一")){
?????????? sellMethod = new SellMethodDiscount();
?????? }
?????? return sellMethod;
??? }
}
(2)然后考慮用策略模式實(shí)現(xiàn)
?? 客戶端代碼 Main.java
package com.yilong.sell.strategy;
public class Main {
??? public static void main(String[] args) {
?????? SellGood sellGood = new SellGood("打5折");
?????? sellGood.setPrePrice(10);
?????? sellGood.setAmount(10);
?????? System.out.println("總價(jià):" + sellGood.getSumPrice());
??? }
}
?? 銷售方式類 SellMethod.java
package com.yilong.sell.strategy;
public abstract class SellMethod {
??? public abstract double getSumPrice(double prePrice, int amount);
}
?? 原價(jià)銷售類 SellMethodPrototype.java
package com.yilong.sell.strategy;
public class SellMethodPrototype extends SellMethod {
??? @Override
??? public double getSumPrice(double prePrice, int amount) {
?????? return prePrice * amount;
??? }
}
?? 打折銷售類 SellMethodDiscount.java
package com.yilong.sell.strategy;
public class SellMethodDiscount extends SellMethod {
??? public static final double DISCOUNT = 0.5;
??? @Override
??? public double getSumPrice(double prePrice, int amount) {
?????? return prePrice * amount *
轉(zhuǎn)載于:https://my.oschina.net/lovedreamland/blog/12996
總結(jié)
以上是生活随笔為你收集整理的设计模式 – 策略模式(Strategy)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: WCF Membership Provi
- 下一篇: .NET中的异步编程(四)- IO完成端