Strategy mode strategy--What car do you take home?

Source: Internet
Author: User

1. Existing problems and models

: June 2014 school: Langfang Teachers ' College: People in Shijiazhuang: Students

It's almost the final exam. Home Rhythm also ran on the agenda, bored to think about this home.

For me to go home there are two main ways of transportation:1. Car. 2. Train . Like airplanes, high-speed rail what we do not consider one is the lack of funds, conditions do not agree (Langfang no airport it? Outside, Langfang to Shijiazhuang so close to engage so many tricks also too hurt nerves (especially for the way crazy).

Now analyze both of these ways:

1. The car is easy and convenient. Don't worry about not getting the train ticket while the students are coming home, but for people with motion sickness, the 4h car journey is still quite painful, or a quick emergency will put you up for a few hours or maybe. Have to mention that from a freshman to now Langfang-Shijiazhuang car fares have risen three times, the fourth time seems to be in preparation.

。。

2. Train "home difficult, difficult to a vote to solve thousands of sorrow", the student's advantage is to go out to play the winter vacation home when there is a steady student card, can half price half price also can come a 75 percent, than take a car home more cost-effective.

But we can not ignore this cheap behind the hidden truth, all kinds of tickets to all kinds of midnight camp, the whole to go home sometimes like a night owl.

Compared to the students, for me, the way to go home is mainly train, car supplement, the train does not work and then buy a car ticket. Suppose that one day hands have a lot of spare money, technology again developed point, maybe I went home by UFO .

Uh. Think more, then people should not be in Langfang

2. Structure: UML diagram

3. Pattern composition

1) Environmental Role (context): holds a reference to strategy. Finally called to the client.

Configured with a Concretestrategy object, you can define an interface that allows strategy to access its data.

2) Abstract policy role (strategy): Policy class. Typically implemented by an interface or abstract class. Defines a common interface. Different algorithms implement this interface in different ways.

The context uses this interface to invoke an algorithm defined by a concretestrategy.

3) Detailed strategy role (CONCRETESTRATEGY): Wrapping the related algorithms and behaviors, realizes the interface of strategy definition, and provides the implementation of detailed algorithm.

4. Application

1) Multiple classes differ only in the performance behavior, and dynamically select the behavior to be performed in detail at execution time.

2) Different strategies need to be used in different situations. Or the strategy may be implemented in other ways in the future.

3) Details of the implementation of the detailed policy are hidden from the customer. To avoid exposing complex, algorithmic-related data structures.

4) A class defines a variety of behaviors, and these behaviors appear as multiple conditional statements in the operation of this class. The related conditional branches are moved into their respective strategy classes in place of these conditional statements.

5. Strengths

1) Algorithm series: The Strategy class hierarchy defines a series of reusable algorithms or behaviors for the context.

Inheritance helps to extract public functionality from these algorithms.

2) simplified unit test: Each algorithm has its own class. The ability to test independently on its own interface. Each algorithm ensures that it has no errors, and that any changes to it will not affect other algorithms.

3) Some conditional statements are eliminated: when different behaviors are piled up in a class. It is very difficult to avoid using conditional statements to select the appropriate behavior. Encapsulating these behaviors in separate strategy classes enables you to eliminate conditional statements in classes that use these behaviors.

4) provides a way to replace an inherited relationship: Inheritance can handle multiple algorithms or behaviors, but it also makes it impossible to dynamically change an algorithm or behavior.

Disadvantages

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

2) The creation of a very large number of policy classes, which can reduce the number of objects to some extent by using the enjoy meta mode.

6. Mode implementation

UML Diagram

Code implementation

    Client code static void Main (string[] args) {Personcontext person;        Instantiate different means of transport and finally get home in different person = new Personcontext (new Trainstrategy ()); Person.        Personinterface ();        person = new Personcontext (new Automobilestrategy ()); Person.        Personinterface ();    Console.read ();        }//personcontext class Personcontext {//declares a Gohomestrategy object private gohomestrategy gh;        Through the construction method, incoming detailed home traffic strategy public personcontext (Gohomestrategy gh) {this.gh = GH;        } public void Personinterface () {gh.transportation (); }}//abstract algorithm class, defining all the ways to get home to the traffic abstract class Gohomestrategy {//algorithm method public abstract void Transportati    On (); }//Detailed mode of transportation. Train class Trainstrategy:gohomestrategy {public override void transportation () {CONSOLE.W        Riteline ("Go Home by Train"); }}//Traffic mode, car class Automobilestrategy:gohomestrategy    {public override void transportation () {Console.WriteLine ("Go Home by Car"); }    }

7. Other related modes

State mode, simple Factory mode

8. Summary

Policy mode: It defines a series of algorithms and encapsulates each algorithm. and allow them to be replaced with each other. The policy pattern makes the algorithm independent of the customers who use it.

In the application of this pattern. It is very often used in a way that combines simple Factory mode with a reflection method to better implement the OCP principle.

PS: A small mode algorithm is not a simple computational indicator to learn, but rather a way to implement the functionality of the method.

Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

Strategy mode strategy--What car do you take home?

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.