24 kinds of design patterns learning strategy mode of notes

Strategy Mode (Strategy)

    Defines a family of algorithms, encapsulate them, so that they can replace each other, this mode allows changes in the algorithm, will not affect customers using algorithms.

CashSuper: abstract class fee calculation method

   Normal fee categories: normal charges. acceptCash () returns the amount of direct approach
   discounted fee category: discount needs to be initialized in the constructor parameters in the discount calculation acceptCash () way
   over how much rebate class: How many full condition parameters need to be initialized in the constructor. How much rebate parameters, the calculation acceptCash () method. To meet the conditions, the total amount - the amount of the rebate amount = return.

  When used by the client. Only new CashContext ( "Analyzing Parameters"). Then call getResult (). To obtain a final calculation result. Because CashContext class constructor parameters have been passed in accordance with a specific calculation method of the object.

 When to Use

  • When a system has a number of classes, and distinguish them only their immediate behavior

  2. Methods

  • These algorithms into a package of a class, or any alternative

  3. advantage

  • Algorithm can freely switch
  • Avoid using multiple conditional (if not policy mode we may use multiple conditional statements, is not conducive to maintenance)
  • Scalability is good, just add a strategy to implement the interface

  4. shortcomings

  • Strategies will increase the number of classes, each policy is a class, a very small possibility of reuse
  • All classes are required of foreign policy exposed

  5. usage scenarios

  • Multiple classes only on the algorithm or behavior is slightly different scenario
  • Algorithm requires free to switch scenes
  • Rules need to shield algorithm scene

  6. Application Examples

  • Travel, bicycles, cars, etc., each of which is a way to travel policy
  • Mall promotions, discounts, full reduction, etc.
  • Java AWT in LayoutManager, namely layout manager

 

Guess you like

Origin www.cnblogs.com/xiaoyangxiaoen/p/12360827.html
Recommended