Design pattern of a sentence: excessive design is a crime, to be realistic according to the project, no one design is one step, many functions are based on feedback to improve.
1, background: In the actual development, we often meet the realization of some business functions, there are many different ways of implementation, users can choose one of the ways.
For example, when you sort a series of data, we can choose bubble sort, quick sort, insert sort, heap sort, and so on. During the development process, we usually choose
The different algorithms are encapsulated into a class in a hard-coded way, and when we need to add new algorithms, we modify the class, and the class is modified when an algorithm is modified.
In this way, each change involves the release of other algorithms, which can cause new errors, destabilize the system, and increase the workload of the test. So, is there a
A common design pattern, each time you add or modify an algorithm, just write the algorithm, so that you can change the other code at a minimum. This is the "strategy model" to be explored today.
2. Example
2.1 In the promotion of an event, the user can participate in a variety of ways, there are currently "published micro-Blog", "published" and "recommended users" three ways, if
Activities may add new ways of participation at any time, or modify the process of a participatory approach. Our requirement is to minimize any other forms of participation that we have, so that we do not have to worry about new or different ways of participating in other forms of participation.
2.2 A classic scene is the user can choose according to their actual situation to travel, such as from Guangzhou to Xiamen tourism, users can choose the aircraft, trains, buses and other different ways
3. Strategy mode
3.1 Policy mode (strategy): dynamically defines a sequence of algorithms, which can be replaced between each other, this mode in the addition of new algorithms or modify the algorithm, the modified code to minimize, improve system scalability.
3.2 UML diagram of the policy mode, as follows:
Context Environment Class: maintains a strategy parent class that references a specific Concretestrategy object.
Strategy: A common interface for policies that declares the interfaces to be implemented
Concretestrategy: The concrete strategy implements the class, implements the strategy interface, for example Concretestrategya, Concretestrategyb and so on.
3.3 Advantages and Disadvantages:
Advantages: A. Can provide the replacement of the algorithm, according to the need, choose a different strategy
B. Reduce the coupling of each algorithm, modify and add an algorithm, do not affect other algorithms
Cons: A. Increase the cost of the algorithm concretestrategy to achieve class communication, that is, the complexity of the transfer of parameters.
B. Need to choose a different policy implementation through if else
In view of the above situation, we can combine the simple workshop mode, use reflection to instantiate specific policy implementation class, eliminate if else judgment, so that environment class context
Can stay the same
4, source code implementation reference
4.1 Policy Interface Strategy
[Java] View plaincopy
public interface IStrategy { public void getPrice();//抽奖的具体方式 }
4.2 Policy Specific implementation class
[Java] View plaincopy
//具体策略A public class ConcreteStrategyA implements IStrategy { @Override public void getPrice() { System.out.println("========抽奖方式A======="); } }
[Java] View plaincopy
//策略B public class ConcreteStrategyB implements IStrategy { @Override public void getPrice() { System.out.println("========抽奖方式B======="); } }
4.3 Environment Class Context
[Java] View plaincopy
/** * 环境类---》生成具体的策略 */ public class Context { private IStrategy strategy; public Context(IStrategy strategy){ this.strategy = strategy; } public Context(){} //利用简单工程方式,生产策略的具体实例 public void createStrategy(String strategy){ try { this.strategy = (IStrategy) Class.forName(strategy).newInstance(); } catch (Exception e) { e.printStackTrace(); } } public void getPrice(){ strategy.getPrice(); } }
4.4 Client Clients
[Java] View plaincopy
public class Client { public static void main(String[] args) { doGetPrice(); } public static void doGetPrice(){ Context context = new Context(new ConcreteStrategyA()); context.getPrice(); //通过反射机制创建策略实例,降低环境类的耦合 Context context2 = new Context(); context2.createStrategy("com.jimi.test.designpattern.strategy.activity.ConcreteStrategyB"); context2.getPrice(); } }
1, the design mode of the strategy mode