"Design Mode Design Pattern" reading notes

xiaoxiao2021-03-06  20

"Design Mode Design Pattern" reading notes

Strategy mode

structure

purpose

When a certain / some behavior of an object is multiple algorithms, it is not through the internal Switch / IF method, nor through the method of derivating the subclasses, but put the algorithm to a separate object (there is a common Inside the interface). In this way, the switching of the algorithm can replace a lot of flexibility as long as the corresponding algorithm object is replaced.

discuss

1. Strategy can have a hierarchical inheritance structure to achieve the purpose of unifying a common algorithm.

2. Which Strategy is used by the client decision / selected, and Context does not need to know which Strategy should be used.

3. Since the various STRATEGY uses a common interface, the parameters passing to Strategy may be redundant. Another way is to deliver Context itself's pointer / reference to Strategy, which information is used by Strategy, but the disadvantage is to increase the dependence of Strategy to Context, and Context also needs to provide a corresponding interface to provide various information.

4, if possible, you can design Strategy to Flyweight, so that Strategy can be shared to achieve the purpose of reducing the number of objects.

5, Context can also have a default algorithm itself, only using a Strategy object when the client does set a Strategy to it.

Reference

"Design Pattern" Gang-Of-4 1997

转载请注明原文地址:https://www.9cbs.com/read-40042.html

New Post(0)