学会了简单工厂,可以做好多事情了,很多地方都有它的影子,不过仅仅是简单工厂是不够用的。这不需求来了:
商场收银软件 根据客户购买的商品的单价和数量 向客户收费
在我们学习设计模式的时候我们会这么写:
public class Cashier {
public static double doCash(ArrayList<Commodity> commodityArrayList,String activity) {
double all_number = 0;
for (Commodity item : commodityArrayList) {
all_number += item.getPrice() * item.getNumbet();
}
}
return all_number;
}
}
public class Commodity {
private String name;
private double price;
private int numbet;
public Commodity(String name, double price, int numbet) {
this.name = name;
this.price = price;
this.numbet = numbet;
}
@Override
public String toString() {
return "Commodity{" +
"name='" + name + '\'' +
", price=" + price +
", numbet=" + numbet +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getNumbet() {
return numbet;
}
public void setNumbet(int numbet) {
this.numbet = numbet;
}
}
public class main {
/**
* 需求:商场收银软件 根据客户购买的商品的单价和数量 向客户收费
*
*/
public static void main(String[] args) {
Commodity commodity1 = new Commodity("苹果", 2.5, 1);
Commodity commodity2 = new Commodity("果汁", 5, 2);
Commodity commodity3 = new Commodity("蓝莓", 15, 3);
Commodity commodity4 = new Commodity("方便面", 1.5, 4);
Commodity commodity5 = new Commodity("铅笔", 0.5, 5);
ArrayList<Commodity> commodityArrayList = new ArrayList<>();
commodityArrayList.add(commodity1);
commodityArrayList.add(commodity2);
commodityArrayList.add(commodity3);
commodityArrayList.add(commodity4);
commodityArrayList.add(commodity5);
Double all_number = Cashier.doCash(commodityArrayList);
System.out.println("all:" + all_number);
}
}
但是吧,产品经理和项目经历永远是我们的敌人,这不又说:
增加打折功能
一句mmp,不能早说吗?我代码都写好了,又要改,改就改~~
五分钟就改好了:
public class Cashier {
public static double doCash(ArrayList<Commodity> commodityArrayList,String activity) {
double all_number = 0;
for (Commodity item : commodityArrayList) {
all_number += item.getPrice() * item.getNumbet();
}
switch (activity){
case "5折":
all_number = all_number*0.5;
break;
case "6折":
all_number = all_number*0.6;
break;
case "7折":
all_number = all_number*0.7;
break;
case "8折":
all_number = all_number*0.8;
break;
case "9折":
all_number = all_number*0.9;
break;
}
return all_number;
}
}
public class main {
/**
* 原始需求:商场收银软件 根据客户购买的商品的单价和数量 向客户收费
* 增加: 1、增加打折
*/
public static void main(String[] args) {
Commodity commodity1 = new Commodity("苹果", 2.5, 1);
Commodity commodity2 = new Commodity("果汁", 5, 2);
Commodity commodity3 = new Commodity("蓝莓", 15, 3);
Commodity commodity4 = new Commodity("方便面", 1.5, 4);
Commodity commodity5 = new Commodity("铅笔", 0.5, 5);
ArrayList<Commodity> commodityArrayList = new ArrayList<>();
commodityArrayList.add(commodity1);
commodityArrayList.add(commodity2);
commodityArrayList.add(commodity3);
commodityArrayList.add(commodity4);
commodityArrayList.add(commodity5);
Double all_number = Cashier.doCash(commodityArrayList,"5折");
System.out.println("all:" + all_number);
}
}
不错吧,什么又要加功能?
要满100返20~~~~~~
一百句mmp,这个就不好改了啊,再改这个里面的逻辑复杂了很容易出错啊,万一哪天又要加功能怎么办?好吧,我用简单工厂模式还不行吗?具体代码大家参照上一篇来写,相信大家会发现,确实比现在要好很多了,各个收费类分明,容易增加扩展。
但是这个例子和上一篇的例子不太一样,这一篇更加侧重于算法的改变,而不是收费类的改变,因为超市活动很多,更改很频繁,简单工厂模式只是更改对象的创建问题,并没有从根本上解决算法的多样性,这就需要引入策略模式了~~
策略模式:(Strategy)它定义了算法家族,分别封装起来,让他们之间可以相互替换,此模式让算法的变化,不会影响到使用算法的客户。
每次要用设计模式来写程序的时候,建议大家先画一个UML图,这样能够更清晰的了解到我们要如何去做,更方便我们看到设计模式之间的不同和优劣。
public abstract class Strategy {
//算法方法
public abstract void AlgorithmInterface();
}
public class ConcreteStrategyA extends Strategy {
@Override
public void AlgorithmInterface() {
System.out.println("算法A实现");
}
}
public class ConcreteStrategyA extends Strategy {
@Override
public void AlgorithmInterface() {
System.out.println("算法A实现");
}
}
public class ConcreteStrategyC extends Strategy {
@Override
public void AlgorithmInterface() {
System.out.println("算法C实现");
}
}
public class Context {
Strategy strategy;
public Context(Strategy strategy) {
this.strategy = strategy;
}
public void ContextInterface(){
strategy.AlgorithmInterface();
}
}
public static void main(String[] args) {
Context context;
String type = "";
switch (type){
case "正常收费":
context = new Context(new ConcreteStrategyA());
context.ContextInterface();
break;
case "打八折":
context = new Context(new ConcreteStrategyB());
context.ContextInterface();
break;
case "满100返20":
context = new Context(new ConcreteStrategy());
context.ContextInterface();
break;
}
}
}
这是个通用的代码,具体详细的代码大家可以自己写写,这样其实我们会发现和简单工厂的区别:
//简单工厂
Algorithm algorithm = AlgorithmFactory.createAlgorithm('/');
//策略模式
context = new Context(new ConcreteStrategyA());
他们侧重点不同:但是能不能把两者结合一下呢?这样写暴露出的东西太多,我想让用户在不知情的情况就把事情做了。
我们可以重写一下Context方法。
public class Context {
Strategy strategy;
public Context(String type) {
switch (type){
case "正常收费":
strategy = new ConcreteStrategyA();
break;
case "打八折":
strategy = new ConcreteStrategyB();
break;
case "满100返20":
strategy = new ConcreteStrategyC();
break;
}
this.strategy = strategy;
}
public void ContextInterface(){
strategy.AlgorithmInterface();
}
}
public static void main(String[] args) {
Context context;
String type = "";
context = new Context(type);
context.ContextInterface();
}
这样完全只暴露出一个Context,算法和客户端彻底分离。
最后我们发现我们还是没有解决switch的问题,再以后讲解抽象工厂模式的时候会利用反射解决这个问题。