Strategy Mode Strategy -- what kind of car is going home?

Source: Internet
Author: User

1. Problems and modes

Time: June 2014 School: Langfang normal home: Shijiazhuang: Student

The final exam is approaching,Go homeThe pace is also on the agenda, bored to think about this home thing. There are two main transport modes for me to go home:1. Car, 2. Train. If an airplane or a high-speed train is used, we will not consider the lack of funds and the disallow of conditions (Langfang does not have an airport ?) In addition, the fact that Langfang and Shijiazhuang are so close to doing so many tricks is also too much of a headache (especially for all the way crazy ). The following two methods can be analyzed one by one:

1. cars are convenient and easy to worry about getting a train ticket when the students go home. However, the 4-hour drive is tough for people with motion sickness, or it is possible to put you in a high-speed emergency for several hours. One thing I have to mention is that from freshman year to Langfang now ~ The taxi fare in Shijiazhuang has risen three times, and the fourth time seems to be in preparation...

2. the train "It's hard to go home, it's hard to get a ticket to get rid of the worry". The advantage of students is that they have a real student ID card when they go home during the cold and summer vacations, A half price can be off without a half price, which is more cost-effective than taking a car home. However, we cannot ignore the truth behind the cheap price. All kinds of ticket snatching are waiting for midnight, so sometimes we have to go home like a night owl.

In comparison, for students, the way to go home is dominated by trains and supplemented by automobiles. If a train doesn't work, buy a bus ticket. If I had a lot of spare money on that day, the technology would try again. Maybe I would just take it.UFOGo home. Er, think too much. At that time, people will not be in Langfang.

2. Structure: UML diagram

3. mode composition

1) Context: holds a reference to Strategy and calls it to the client. It is configured with a ConcreteStrategy object. You can define an interface to allow Strategy to access its data.

2) Abstract policy role (Strategy): A Policy class, usually implemented by an interface or abstract class. Defines a public interface. Different algorithms implement this interface in different ways. Context uses this interface to call a ConcreteStrategy-defined algorithm.

3) ConcreteStrategy: encapsulates related algorithms and behaviors, implements interfaces defined by Strategy, and provides implementation of specific algorithms.

4. Application

1) Multiple classes only differ in performance behaviors, and dynamic selection of specific actions to be executed during runtime.

2) Different policies must be used in different situations, or they may be implemented in other ways in the future.

3) hiding implementation details of specific policies from customers to avoid exposing complex algorithm-related data structures.

4) A class defines multiple behaviors, and these behaviors appear in the form of multiple condition statements in the operations of this class. Move related condition branches into their respective Strategy classes to replace these condition statements.

5. Advantages

1) algorithm series: the Strategy class hierarchy defines a series of reusable algorithms or actions for Context. Inheritance helps to analyze the common functions of these algorithms.

2) Simplified unit testing: each algorithm has its own class and can be tested independently through its own interface. Each algorithm can ensure that there is no error. When you modify any of the algorithms, no other algorithms will be affected.

3) Some conditional statements are eliminated: when different behaviors are stacked in a class, it is difficult to avoid using conditional statements to select appropriate behaviors. Encapsulate these behaviors in independent Strategy classes and eliminate conditional statements in the classes that use these actions.

4) provides a way to replace the inheritance relationship: inheritance can process multiple algorithms or actions, but it also makes it impossible to dynamically change algorithms or actions.

Disadvantages

1) The client must know all the policy classes and decide which policy class to use.

2) There are many strategies. You can use the enjoy mode to reduce the number of objects to a certain extent.

6. Mode implementation

UML diagram

Code Implementation

// Client code static void Main (string [] args) {PersonContext person; // instantiate different transportation tools, the final Way To Go Home is different: person = new PersonContext (new TrainStrategy (); person. personInterface (); person = new PersonContext (new AutomobileStrategy (); person. personInterface (); Console. read () ;}// PersonContext class PersonContext {// declare a GoHomeStrategy object private GoHomeStrategy gh; // pass in the specific home transportation policy public PersonContext (GoHomeStrategy gh) through the constructor) {this. gh = gh;} public void PersonInterface () {gh. transportation () ;}}// abstract algorithm class, which defines all the transport modes for returning home abstract class GoHomeStrategy {// algorithm method public abstract void transportation ();} // The specific transportation mode, train class TrainStrategy: GoHomeStrategy {public override void transportation () {Console. writeLine ("take a train home") ;}// specific transportation mode, vehicle class AutomobileStrategy: GoHomeStrategy {public override void transportation () {Console. writeLine ("Going home by car ");}}

7. Other related modes

Status mode, simple factory Mode

8. Summary

Policy mode: it defines a series of algorithms, encapsulates each algorithm, and enables them to replace each other. The rule mode allows algorithms to change independently of customers who use it.

In the application of this mode, we often use a combination of simple factory mode and reflection method to better implement the OCP principle.

PS: The algorithm in the Rule mode is not simply an exponential calculation step, but a method to implement a certain function.

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.