Strategy mode (Strategy)

There are different ways to achieve a purpose, depending on which way to take.

//接口
  public interface Strategy
  {
      public void algorithmInterface();
  }

 

  public class  ConcreStrategyA implments Strategy
  {
      public void  algorithmInterface()

  {

          System.out.println("Strategy A starts to execute");

    }
  }

 

  public class  ConcreStrategyB implments Strategy
  {
      public void  algorithmInterface()

  {

          System.out.println("Strategy B starts executing");

    }
  }

 

publicclassContext{privateStrategy strategy;publicContext(Strategy strategy){this.strategy = strategy;}publicint executeStrategy(){return strategy.doOperation();}}              
客户端调用:
Context context =newContext(new ConcreStrategyA());  
context.executeStrategy(); 

Result: Strategy A starts executing

Context context =newContext(new ConcreStrategyB());  
context.executeStrategy();
Result: Strategy B starts executing

Strategy Pattern vs. Simple Factory

    The strategy pattern is obtained by optimizing the simple factory. When both patterns are suitable, it is natural that the strategy pattern is stronger than the simple factory.

    However, simple factories are widely used. The strategy mode is encapsulated and changed, that is to say, when encountering changes such as "applying different services at different times", the strategy mode is used to encapsulate these changes, so that the client coupling is reduced. In this case, it will be simpler Factory is strong.

    From the above two pictures, you can also see the difference, the simple factory is a dependency, and the strategy mode is an aggregation. The simple factory relies on all subclasses, and the parent class aggregates the Context. Obviously, the coupling of the strategy pattern is significantly reduced

 

The difference between the two: The
strategy pattern and the simple factory pattern look very similar, and they both realize the selection of different subclasses through polymorphism. This idea should be drawn from the overall view of the program. From the perspective of using these two modes, we will find that in the simple factory mode , we only need to pass the corresponding conditions to get the desired object , and then implement the operation of the algorithm through this object. In the strategy mode , you must first create a class object you want to use , and then pass the object as a parameter, and call different algorithms through the object. In the simple factory mode, a class is selected to instantiate an object through a condition, and the strategy mode will hand over the work of selecting the corresponding object to the user of the mode, and it will not do the selection work itself.

Reference article: https://blog.csdn.net/zhangmeihong2/article/details/42365097,

     https://blog.csdn.net/fwj380891124/article/details/7552305

 
 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326432034&siteId=291194637