Java design pattern--Strategy mode

Source: Internet
Author: User

1. What are the benefits of the strategy model?

The advantage of the strategy pattern is that you can dynamically change the behavior of the object.

2. Design principles

The design principle is to extract the parts of a class that are often changed or may change in the future, as an interface (a virtual class can be used in C + +), and then include an instance of the object in the class, so that an instance of the class can invoke the behavior of the class that implements the interface at run time. Here is an example.

The policy pattern belongs to the object behavior pattern, which is mainly for a set of algorithms, encapsulating each algorithm in a separate class with a common interface, so that they can be replaced with each other. The policy pattern allows the algorithm to change without affecting the client. Typically, a policy pattern is used when an application needs to implement a particular service or feature, and the program is implemented in a variety of ways.

3. There are three objects in the policy mode:
(1) Environment object: a reference to an interface or abstract class defined in an abstract policy is implemented in this class.
(2) Abstract policy object: it can be implemented by an interface or an abstract class.
(3) Specific policy object: it encapsulates different algorithms for implementing the same function.

Using the policy model to build the application, you can choose different algorithms to implement the functions of the application according to the user configuration and other content. The specific selection is done by the environment object. This approach avoids the code clutter caused by the use of conditional statements, improving application flexibility and rationality.

5, Application Scenario Example:

Liu Bei to the Jiangdong to marry his wife, go before Zhuge Liang to Zhao Yun (best man) Three ace, said is the secret to solve the thorny problem, hey, not to mention, really solve the big problem, get to the end is Zhou Yu accompany the lady and fold soldiers, then we first see what this scene is like.

First of all, talk about the elements of this scene: Three clever tricks, a brocade sac, a Zhao Yun, the ingenious plan is the bright elder brother gives, the ingenious idea puts in the brocade sac, is commonly known is the ace, that Zhao Yun is a work person, from the brocade sac takes out the ingenious plan, executes, then wins. How do you show these with Java programs?

Let's take a look at the picture first.


Three clever tricks are the same type of thing, then we write an interface:

Java code

Package com.yangguangfu.strategy;

/**

*

* @author [email protected]: Ford

* First define a policy interface, this is Zhuge Liang old man to Zhao Yun's three tricks of the interface.

*/

Public interface IStrategy {

Each of these tricks is an executable algorithm.

public void operate ();

}

Then write three implementation classes, there are three clever tricks:

A coup: first to Wu:

Java code

Package com.yangguangfu.strategy;

/**

*

* @author [email protected]: Ford

* Find Qiaoguo old help, so that Sun Quan can not kill Liu Bei.

*/

public class Backdoor implements IStrategy {

    1. @Override
    2. public void operate () {
    3. System.out.println ("Find Qiaoguo old Help, let the Wu too to Sun Quan pressure, so that he can not kill Liu Bei ...");
    4. }
    5. }

Coup II: Ask Wu to open a green light, release:

Java code

Package com.yangguangfu.strategy;

/**

*

* @author [email protected]: Ford

* Ask Wu to open a green light too.

*/

public class Givengreenlight implements IStrategy {

@Override

public void operate () {

SYSTEM.OUT.PRINTLN ("Ask Wu to open a green light, release!") ");

}

}

Ingenious three: neighbour, blocking the pursuers:

Java code

Package com.yangguangfu.strategy;

/**

*

* @author [email protected]: Ford

* Neighbour the rear, blocking the pursuers.

*/

public class Blackenemy implements IStrategy {

@Override

public void operate () {

System.out.println ("Neighbour, blocking the pursuers ...");

}

}

Well, let's see, three ingenious tricks are there, that need to have a place to put the ingenious idea, put in the bag:

Java code

Package com.yangguangfu.strategy;

/**

*

* @author [email protected]: Ford

*

*/

public class Context {

Private IStrategy strategy;

constructor, which trick do you want to use?

Public Context (IStrategy strategy) {

This.strategy = strategy;

}

public void operate () {

This.strategy.operate ();

}

}

Then is Zhao Yun valiantly with three Jin capsule, pulled into the ranks of the elderly, but also want to marry innocent girl, color Mimi Liu Bei to Ruzhui, hey, not to mention, Bright elder brother's three clever tricks is really good, look at:

Java code

Package com.yangguangfu.strategy;

public class Zhaoyun {

/**

* Zhao Yun appearance, he according to Zhuge Liang to his account, and then disassemble the ingenious plan

*/

public static void Main (string[] args) {

Context context;

When I first came to Wu, I opened the one.

System.out.println ("----------opened the first---------------when he arrived in the Kingdom of Wu");

Context = new context (new backdoor ());

Context.operate ();//disassemble execution

System.out.println ("\n\n\n\n\n\n\n\n\n\n\n\n\n");

When Liu Bei had a second,

System.out.println ("----------Liu Bei, took a second---------------");

Context = new Context (new Givengreenlight ());

Context.operate ();//disassemble execution

System.out.println ("\n\n\n\n\n\n\n\n\n\n\n\n\n");

What about Sun Quan's little pursuers? Unpack the third bag.

System.out.println ("----------The Little pursuers of Sun Quan, what should I do?" Unpacking the third---------------");

Context = new Context (new Blackenemy ());

Context.operate ();//disassemble execution

System.out.println ("\n\n\n\n\n\n\n\n\n\n\n\n\n");

}

}

Something: In this three strokes, get the week Lang is "lend your Money" Ah! This is the strategy mode, cohesion poly low-coupling features are also shown, there is an extension, that is, the OCP principle, the strategy class can continue to add gas, just modify the Context.java on it, this does not say more, I understand it.

Java design pattern--Strategy mode

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.