Design Mode learning 3-Strategy Mode

Source: Internet
Author: User

Scenario Description:

When I woke up, it would take 30 minutes to get to work. I decided to wear my clothes, smoke a cigarette, brush my teeth, and wash my face. I went to work slowly; if I woke up for 15 minutes, I would be late. Even if I had forgotten to smoke, I would like to wear my clothes, brush my teeth and wash my face, and rush to work. If I woke up for only five minutes, I would be swollen! It took 5 seconds to restore your mood and quickly got dressed and rushed out!

We can find that we have done different things for different wake-up times. If we use a program, is there any proper mode? Well, Strategy Mode

Strategy Mode (Policy mode ):

Definition: defines a series of algorithms, encapsulates them one by one, and makes them replaceable. The algorithm selection and implementation are separated.

Implementation: Include an abstract class (Strategy) in the Context of an algorithm. This abstract class has an abstract method that specifies how to call an algorithm. Each derived class implements algorithms as needed.

Structure:

Sample Code:

Based on the scenario description above, we can implement the Strategy Mode.

First, define the abstract class (Strategy ):

1     public interface IStrategy {2         void AlgorithmInterface();3     }

 

Then define the child classes and implement them. These three child classes correspond to three situations in the scenario respectively. We can see that child classes encapsulate their own algorithms (Business Rules ).

1 public class ConcreteStrategyA: IStrategy {2 public void AlgorithmInterface () {3 Console. WriteLine ("wear clothes"); 4 Console. WriteLine ("Smoke root smoke, great! "); 5 Console. writeLine ("brush your teeth and wash your face"); 6} 7} 8 9 public class ConcreteStrategyB: IStrategy {10 public void AlgorithmInterface () {11 Console. writeLine ("wear clothes"); 12 Console. writeLine ("brush your teeth and wash your face"); 13} 14} 15 16 public class ConcreteStrategyC: IStrategy {17 public void AlgorithmInterface () {18 Console. writeLine ("wear clothes! Run! "); 19} 20}

 

The following defines the class Context of the algorithm. From the code below, we can see that Context does not know which specific IStrategy is used (this is type encapsulation ), the advantage is that the Context class does not need to be modified if a new algorithm is added later.

 1 public class Context { 2         public Context(IStrategy strategy) { 3             this.strategy = strategy; 4         } 5  6         public void ContextInterface() { 7             this.strategy.AlgorithmInterface(); 8         } 9         10         private IStrategy strategy;11     }

 

Finally, it is the Client class. We can see that the specific subclass used for selection is undertaken by the Client and transferred to the Context object. This separates algorithm selection from algorithm implementation.

1 class Client {2 static void Main (string [] args) {3 Console. writeLine ("30 minutes later, not urgent =>"); 4 Context context1 = new Context (new ConcreteStrategyA (); 5 context1.ContextInterface (); 6 Console. writeLine ("================================== "); 7 8 Console. writeLine ("15 minutes later =>"); 9 Context context2 = new Context (new ConcreteStrategyB (); 10 context2.ContextInterface (); 11 Console. writeLine ("================== ========================== "); 12 13 Console. WriteLine (" Nima, It's only 5 minutes! => "); 14 Context context3 = new Context (new ConcreteStrategyC (); 15 context3.ContextInterface (); 16 Console. writeLine ("================================== "); 17 18 Console. readLine (); 19}

 

Running result:

Summary:

In general, as long as different business rules need to be applied at different times during the analysis process, you should consider the possibility of using the Strategy Mode to handle such changes.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.