The strategy mode of Java design pattern

Source: Internet
Author: User

This article continues to introduce the strategy model of 23 design pattern series.   Background in software development often encounter this situation, to achieve a certain function has a number of algorithms or strategies, we can choose different algorithms or strategies according to the environment or conditions to complete the function. such as finding, sorting, and so on, a commonly used method is hard-coded (Coding) in a class, if you need to provide a variety of search algorithms, you can write these algorithms into a class, in the class to provide multiple methods, each method corresponding to a specific search algorithm ; Of course, these lookup algorithms can be encapsulated in a unified approach, through the if...else ... or case and other conditional judgment statements to make the selection. Both of these implementations can be called hard-coded, and if a new lookup algorithm needs to be added, the source code of the encapsulated algorithm class needs to be modified, and the client calling code needs to be changed. In this algorithm class to encapsulate a large number of search algorithms, the class code will be more complex, maintenance is more difficult. This is even more undesirable if we include these policies on the client, which will cause the client program to be large and difficult to maintain, and the problem will become more serious if there are a large number of algorithms to choose from.   How do you separate algorithms from objects so that the algorithm can change independently of the customers who use it? The   scheme extracts the parts of a class that change frequently or may change in the future, as an interface, and then contains an instance of the object in the class, so that instances of the class can invoke the behavior of the class that implements the interface at run time. For example, define a series of algorithms, encapsulate each algorithm, and make them interchangeable, so that the algorithm can be independent of the customers who use it to change. This is the policy mode.   applicability many of the related classes are just behavior-specific. Policy provides a way to configure a class with one behavior in multiple behaviors. That is, a system needs to dynamically select one of several algorithms. Used when an application needs to implement a specific service or feature, and the program has multiple implementations. 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.   Advantage 1, can dynamically change the behavior of the object disadvantage 1, the client must know all the policy classes, and decide which policy class to use 2, policy mode will result in a lot of policy classes   composition Environment Class (context): Configured with a Concretestrategy object. Maintains a reference to the Strategy object. You can define an interface to let strategy access its data. Abstract policy Class (strategy): Defines the public interface for all supported algorithms. The context uses this interface to invoke a ConcretestratEGY defines the algorithm. Specific policy class (Concretestrategy): Implement a specific algorithm with strategy interface.   Application scenarios as follows, Liu Bei to the Jiangdong to marry his wife, go before Zhuge Liang to Zhao Yun three an ace, said is to solve the thorny problem by the secret. There are three elements in the scene: three ingenious strategies (specific policy classes), a brocade SAC (Environment Class), Zhao Yun (caller).   Abstract policy classes (strategy) [Java]View PlainCopyprint?
    1. Public interface Strategy {
    2. public Void operate ();
    3. }
Three implementation classes (CONCRETESTRATEGY): A coup: first to the Kingdom of Wu [Java]View PlainCopy print?
    1. Public class Backdoor implements IStrategy {
    2. @Override
    3. public Void Operate () {
    4. System.out.println ("Find Qiaoguo old Help, let the Wu too to Sun Quan pressure, so that he can not kill Liu Bei");
    5. }
    6. }
Good trick two: Ask Wu too green to release [Java]View PlainCopyprint?
    1. Public class Givengreenlight implements IStrategy {
    2. @Override
    3. public Void Operate () {
    4. System.out.println ("Ask Wu to open a green light, release");
    5. }
    6. }
Trick three: neighbour, blocking the pursuers [Java]View PlainCopy print?
    1. Public class Blackenemy implements IStrategy {
    2. @Override
    3. public Void Operate () {
    4. System.out.println ("neighbour, block the pursuers");
    5. }
    6. }

Environment Class (context) [Java]View PlainCopyprint?
  1. Public class Context {
  2. Private strategy strategy;
  3. //constructor, which trick do you want to use ?
  4. Public Context (strategy strategy) {
  5. this.strategy = strategy;
  6. }
  7. public void Setstrategy (Strategy strategy) {
  8. this.strategy = strategy;
  9. }
  10. public Void Operate () {
  11. this.strategy.operate ();
  12. }
  13. }

Here's how it's used. [Java]View PlainCopyprint?
  1. Public class Zhaoyun {
  2. Public static void Main (string[] args) {
  3. Context context;
  4. System.out.println ("----------just arrived in the Kingdom of Wu using the first SAC---------------");
  5. Context = New context (new Backdoor ());
  6. Context.operate ();
  7. System.out.println ("\ n");
  8. System.out.println ("----------Liu Bei to use the second capsule---------------");
  9. Context.setstrategy (new Givengreenlight ());
  10. Context.operate ();
  11. System.out.println ("\ n");
  12. System.out.println ("----------Sun Quan's pursuers came, using the third SAC---------------");
  13. Context.setstrategy (new Blackenemy ());
  14. Context.operate ();
  15. System.out.println ("\ n");
  16. }
  17. }
Three strokes down, get the week Lang is "Lend your Money". The above is the strategy mode, a variety of different solutions dynamically switch, play the effect of changing the behavior of the object. More Design Patterns: 23 design Mode Series

jason0539

Blog: http://blog.csdn.net/jason0539 (reprint please indicate the source)

Sweep code Follow me public number

The strategy mode of Java design pattern

Related Article

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.