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 that can be replaced by each other, which minimizes modified code and improves system extensibility when adding new algorithms or modifying algorithms.
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
Public interface IStrategy {public void GetPrice ();//The specific way of the Sweepstakes}
4.2 Policy specific implementation class
Specific strategy Apublic class Concretestrategya implements IStrategy {@Overridepublic void GetPrice () {System.out.println ("===== = = = Draw method a======= ");}}
Policy Bpublic class Concretestrategyb implements IStrategy {@Overridepublic void GetPrice () {System.out.println ("======= = Lottery Method b======= ");}}
4.3 Environment Class Context
/** * Environment---"Generate a specific strategy */public class context {private IStrategy strategy;public Context (IStrategy strategy) {This.strategy = strategy;} Public Context () {}//The concrete instance of the production policy using the simple engineering method 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
public class Client {public static void main (string[] args) {dogetprice ();} public static void Dogetprice () {Context context = new Context (new Concretestrategya ()); Context.getprice ();// Create a policy instance through the reflection mechanism, reduce the coupling of the environment class CONTEXT2 = new Context (); Context2.createstrategy (" Com.jimi.test.designpattern.strategy.activity.ConcreteStrategyB "); Context2.getprice ();}}
Strategy Mode of design pattern