设计模式之——工厂模式与策略模式
工厂模式关注的是创建对象的结果,策略模式关注的是创建对象的过程。
1、工厂模式
介绍
专门负责将大量有共同接口的类实例化,工厂模式可以动态的决定将哪一个类进行实例化,不必事先知道要实例化那一个类。
工厂模式的3中形态
简单工厂模式,又称为静态工厂方法模式。
工厂方法模式,又称为多态性工厂模式。
抽象工厂模式,又称为工具箱模式。
示例代码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package main; public interface Operation { int operate(int num1, int num2); }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package main.Impl; import main.Operation; public class AddOperation implements Operation { @Override public int operate(int num1, int num2) { return num1 + num2; } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package main.Impl; import main.Operation; public class MultiOperation implements Operation { @Override public int operate(int num1, int num2) { return num1 * num2; } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package main; public enum OperationEnum { Add, MINUS, MULTI, DIVIDE; }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class FactoryUtils { public static Operation createOperation(OperationEnum operationEnum){ switch (operationEnum){ case Add: return new AddOperation(); case MULTI: return new MultiOperation(); } return null; } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class OperationTest { public static void main(String[] args){ int num1 = 100; int num2 = 200; Operation addOperation = FactoryUtils.createOperation(OperationEnum.Add); int addResult = addOperation.operate(num1, num2); System.out.println("args = [" + addResult + "]"); Operation multiOperation = FactoryUtils.createOperation(OperationEnum.MULTI); int multiResult = multiOperation.operate(num1, num2); System.out.println("args = [" + multiResult + "]"); } }
2、策略模式
介绍
策略模式定义了算法家族,分别封装起来,让他们之间可以相互替换;此模式让算法的变化,不会影响到使用算法的客户。
策略模式三个角色
抽象策略,定义个抽象类或接口,并个给出具体策略的接口。
具体策略,抽象策略的实现类,具体实现了相关的算法和行为。
环境类,持有一个策略的引用。
示例代码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package main; public interface OperationStrategy { int operate(int num1, int num2); }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package main.Impl; import main.OperationStrategy; public class AddStrategy implements OperationStrategy { @Override public int operate(int num1, int num2) { return num1 + num2; } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package main.Impl; import main.OperationStrategy; public class SubstractionStrategy implements OperationStrategy { @Override public int operate(int num1, int num2) { return num1 - num2; } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package test; import main.Impl.AddStrategy; import main.Impl.OperationContext; import main.Impl.SubstractionStrategy; public class StrategyTest { public static void main(String[] args){ OperationContext addContext = new OperationContext(new AddStrategy()); addContext.executeOperate(100, 200); OperationContext subContext = new OperationContext(new SubstractionStrategy()); subContext.executeOperate(100, 200); } }