2006.9.25 Li Jianzhong
Coupling of algorithms and objects
Objects may often use a variety of different algorithms, but if they change frequently, the type will become weak ......
Motivation)
In the process of building software, some objects may use a variety of algorithms that often change. If these algorithms are encoded into objects, the objects will become very complex; in addition, sometimes unsupported algorithms are also a performance burden.
How can I transparently change the object algorithm at runtime as needed? Decoupling algorithms from objects to avoid the above problems?
Intent)
Define a series of algorithms, encapsulate them one by one, and make them replaceable. This mode allows algorithms to change independently of customers who use it.
-- Design Pattern GoF
For example, Strategy Mode Application
This program has two possible changes: When the enumeration type increases, that is, the processing method increases, the Process function needs to modify and add an if else branch; when we want to modify the processing ProcessA of branch 1, we also need to modify the Process function.
To solve the problem above, we first think of writing ProcessA into protected virtual functions (in OO, we generally write all virtual functions as protected functions, because it is a function that can change the behavior of a class, it should generally only appear as a protocol between the subclass and the parent class ).
Strategy Pattern Design
Use the Cart class and ProcessStrategy class as the combination of objects. IProcessStrategy expresses an algorithm abstraction.
Abstraction and specific algorithms
Customer Program
In this way, the algorithm can be dynamically changed. We dynamically add a specific set of Process algorithms and then provide them to the Cart class.
Delegate can be dynamically attached as long as it complies with parameters and return values, whether it is a static method or an abstract method. However, interfaces must have the same abstract meaning. Therefore, we recommend that you use interfaces to express abstract algorithms in this mode.
Structure)
The algorithm is not isolated. It usually requires some context to call it or input some parameters. The Strategy type does not contain the status information (this is the difference from the template method, the template method itself carries the status information), we cannot regard it as an instance, even if there is a status, it is also passed in through parameters. A Strategy defines the complete steps and structure of an algorithm. As long as a specific Strategy class is used, the entire algorithm operation can be completed without other Dependencies and coupling. Context and Strategy are the usage relationships of an object combination. Abstract interfaces in Strategy can be replaced with specific classes at any time to achieve dynamic switching between different algorithms.
The core of this mode is to delegate directly called content to interface object through object combination. As for what the interface object is, it is only known at runtime, that is, it changes during running.
Key points of the Strategy Mode
Strategy and its subclasses provide a series of reusable algorithms for components, so thatRuntimeEasy to implement between algorithms as neededSwitchThe so-called encapsulation algorithm supports algorithm changes.
The Strategy mode provides an alternative to the condition-based judgment statement. The condition-based judgment statement is eliminated, that is, coupling. Code that contains many condition-based judgment statements usually requires the Strategy Mode.
Similar to State, if the Strategy object does not have instance variables, each context can share a Strategy object, saving the object overhead.
The Strategy Mode applies to the change of the entire algorithm in the algorithm structure, rather than the change of a certain part of the algorithm.
Template Method: The Step protocol for Algorithm Execution is itself placed in the abstract class, allowing a common algorithm to operate multiple implementations
Strategy Mode: The Protocol for Algorithm Execution is in a specific class. Each specific implementation has a different general algorithm.
Strategy application in the. NET Framework
For example, in the ArrayList class, the Sort method cannot do anything, because the Point class does not support sorting and does not inherit the IComparer interface.
However, if we want to support sorting, the Sort method is actually a Strategy mode, which supports passing in a specific class that inherits the IComparer interface.
The IComparer interface is actually the IProcessStrategy in the above example. We can implement an IComparer interface and pass in the specific class, so that the Sort method will be executed according to the sorting rules we define.
To switch the sorting method, you only need to change the comparison class passed in Sort.
Although ArrayList does not use the sorting method as a field combination, it uses it as a parameter. Because most methods are not used in ArrayList, only sorting is required. Therefore, it is more appropriate to use it as a parameter.
2010.10.28