Abstract: Although the strategy mode is powerful and widely used, it is not mysterious or profound. The technology behind the strategy pattern is polymorphism, And the idea behind it is encapsulation change. Encapsulation changes actually hide the separation of the changed (Strategy) and the unchanged (context.
I. Review in the previous article
Let's take a look at the previous article. Here we only show the context to be used below.
We have an editable interface and an Editor interface. An editable represents an editable object. The same editable object can be opened or edited in multiple forms. AccordingFind what varies and encapsulate itThe principle changes the method of opening a file. So we encapsulate the editorEditor.
2. Use editable editor or editable Editor
When I provided the Editable interface, editable uses editor by default. However, an alternative is that the Editor contains an editable reference. The latter sounds more intuitive: Since the editor opened the file, the editor should own the file.
Suppose that the Editor contains an editable reference. In editable, a function that returns filepath is required. This function is also called in the openfile of the editor. The class chart structure is as follows:
When opening a file, the editor needs to ask about the path of the Editable file, which violates the "don't ask, tell" principle. For more information about this principle, see explain ). The "don't ask, tell" principle simply means not to make decisions based on the obtained information, but to tell others what to do. In our example, editable should tell the editor which file to open. Instead, the editor decides based on the returned value of the Editable getfilepath. In addition, imagine how editor makes decisions when we expand the stringeditable (see the previous article "Understanding of the Bridge Mode? How does editor know whether the parameter passed to itself is fileeditable or stringeditable? In turn, fileeditable and stringeditable can always know whether to call openfile or openstring, tell it your file name or your string content, and then let the editor execute the command!
Iii. Strategy Mode
Let's first look at the strategy mode. Define a seriesAlgorithm, Encapsulate them and enable them to replace each other. When we decide to extract the editor from editable, we actually implementStrategyMode. Any class can be inserted as long as the Editor interface is implemented. Strategy pattern class diagram:
1. Problems solved by Strategy (context)
A. RepeatedCode : If the strategy mode is not used, another intuitive solution is to generate two editable implementations from Editable: xmlfileeditable and textfileeditable. As you can imagine, there will be a lot of repeated code in these two classes, such as setting the file path, opening and closing the file (the example does not show closing the file, in fact, more methods may need to be repeated ).
B. The editor cannot be changed dynamically. Once the client instantiates the Editable, the corresponding editor will be determined. to change it, unless another editable is instantiated.
C. it is inconvenient to inherit. editable may have multiple open methods and multiple close methods. This may be called closera and closerb. If we want to enable xmleditor and disable closera at the same time, xmlfileeditablewithclosera will appear in the inherited mode, and corresponding records such as begin, texteditablewithclosera, and texteditablewithcloserb will appear. Obviously, duplicate code is not introduced in any inheritance method.
D. Rejected gifts: We use methods that must override the parent class many times because the implementation of the parent class does not meet the requirements. At this time, it is actually suggested that the method has different implementations. Maybe the Strategy Mode can help you solve this problem.
E. Class explosion: the number of classes increases exponentially. Assume that N aspects (the Opening and Closing methods in this example) can be changed. If there are implementation methods in AI, the number of classes in the hierarchy is not counted. Bytes AI classes, as shown in.
2. Advantages of the Strategy Mode
A. Context (editable): You can modify and add algorithms (editor and closer) without affecting the original code. For example, add another editor that supports syntax highlighting in programming languages.
B. Avoid class explosion: Bytes AI classes become 1 + Σ AI classes. For example, it can be changed. What? Or four? That's because 2x2 = 2 + 2
D. Client: can dynamically replace algorithms (compared with the inheritance advantages ). One fileeditable can be opened with xmleditor at one time, while another can be opened with texteditor at another time. I have to admit that the client adds the burden on the behavior assigned to the Editable object, but the cost is that the fileeditable behavior can be modified dynamically. Is it worth looking at the case at that time.
3. When to use the strategy mode and how to abstract strategy
A. Code duplication: when there are a large number of repeated codes in different classes (Child classes of context), the Strategy Mode is preferred. It may mean that the repeated code belongs to a behavior (Strategy) and abstracts this behavior.
B. Switch appearance: execute different behaviors (Strategy) based on a certain condition in the code, which may mean that these different behaviors should be abstracted into strategy.
C. Difficult to derive: You think that functions should be added through derivation, and you find that many parent classes do not do what you want. This behavior may be abstracted as strategy.
D. Class explosion: a large number of classes appear in your code. These classes have names similar to xxxyyyzzz, such as xmleditablewithclosera, but they are combined repeatedly. It may mean that XXX, YYY, and ZZZ represent three aspects of strategy.
Iv. Summary
Although the strategy mode is powerful and widely used, it is not mysterious or profound. The technology behind the strategy pattern is polymorphism, And the idea behind it is encapsulation change. Encapsulation changes actually hide the separation of the changed (Strategy) and the unchanged (context.
In fact, the Strategy Mode is hidden behind many scenarios that use polymorphism. Check the code you have written. I am sure you have used the Strategy Mode.
Thank you for reading myArticleWelcome to communicate with me!
Blog: http://designpatterns.cnblogs.com
Email (MSN): eagle.xiao@gmail.com
Mail List: pattern_study@yahoogroups.com; refactoring@yahoogroups.com; junit@yahoogroups.com
Qq (designpatternsGroup): 26227899