设计模式--行为模式--策略模式--Java
IntentDefine a family of algorithms, encapsulate each one, and make the minter changeable. Strategy lets the algorithm vary independently from clients that use it.定义一套算法进行封装,使得可以改变。策略使得算法在调用段可以独立变化。ApplicabilityUse the Strategy pattern whenmany related classes differ only in their behavior. Strategies provide a way to configure a class with one of many behaviors.一些相关的类具有各自的行为。策略提供一种方式去配置具有特定行为的类。you need different variants of an algorithm. For example, you might define algorithms reflecting different space/time trade-offs. Strategies can be used when these variants are implemented as a class hierarchy of algorithms [HO87].你需要不同的算法。analgorithm uses data that clients shouldn't know about. Use the Strategy pattern to avoid exposing complex, algorithm-specific data structures.调用段不应该知道算法使用的数据。应用策略模式避免暴漏复杂性以及特定的算法数据结构。a class defines many behaviors, and these appear as multiple conditional statements in its operations. Instead of many conditionals, move related conditional branches into their own Strategy class.一个类定义了许多的行为,并且出现在条件语句多个分支当中,那么将条件语句的分支重构到各自的策略类中。Consequences1.Families of related algorithms.2.An alternative to subclassing.3.Strategies eliminate conditional statements.4.A choice of implementations.5.Clients must be aware of different Strategies.6.Communication overhead between Strategy and Context.7.Increased numberof objects.Strategies increase the number of objects in an application.
UML