Java design mode (strategy pattern)

Source: Internet
Author: User

Recently found a good book "Your design Mode", after reading two chapters can assert, must be a need for the cock silk writing, and is specially written for development cock Silk Man's wisdom Pillow book, The Little Woman on the grievance, reluctantly see, people stupid, who let him write so easy to understand it! Write it down in your own way to deepen your understanding.

The first chapter of the strategy model before telling a story, explain the background of the design: Liu Bei to go to Jiangdong married wife, before leaving when Zhuge Liang to the best man Zhao Yun three ace, charge according to the secret to solve the thorny problem. This is the later Zhou Yu lend your money. Three clever tricks in the bag, Zhao Yun from the balloon to win the clever trick. Look at the class diagram first:

1. Three ingenious tricks are specific implementation classes, we design an interface, and then write three implementation classes:

Public interface IStrategy {
Every trick is an executable algorithm.
public void operate ();
}

public class Backdoor implements IStrategy {
public void operate () {
System.out.println ("Find Qiaoguo old help, let Wu too to Sun Quan Pressure");
}
}

public class Givengreenlight implements IStrategy {
public void operate () {
SYSTEM.OUT.PRINTLN ("Ask Wu to open a green light, release!") ");
}
}

public class Blockenemy implements IStrategy {
public void operate () {
System.out.println ("neighbour, block the pursuers");
}
}

2. Three ingenious tricks have, in the brocade bag, need to design an entrance class, convenient Zhao Yun call:

public class Context {
constructor, which ingenious plan do you want to use?
Private IStrategy straegy;
Public Context (IStrategy strategy) {
This.straegy = strategy;
}
Use tricks, watch my moves.
public void operate () {
This.straegy.operate ();
}
}

3. Zhao Yun appearance, each to open the ingenious plan, Zhou Yu taken:

public class Zhaoyun {

public static void Main (string[] args) {
context context;
    When I first came to Wu, I tore down the initial
System.out.println ("-----------The first-------------when I arrived in Wu");
Context = new context (new backdoor ());    Get a coup
context.operate ();//Disassemble Execution
System.out.println ("\n\n\n\n\n\n\n\n");
    Liu Bei was reluctant to break the second one
System.out.println ("-----------Liu Bei to have a second-------------");
Context = new Context (new Givengreenlight ());    
Context.operate ();//execute the second SAC
System.out.println ("\n\n\n\n\n\n\n\n");
What if Sun Quan's creeps come after him? Remove the third
System.out.println ("-----------Sun Quan's creeps come after him. Remove the third-------------");
Context = new context (new Blockenemy ());    
Context.operate ();//Neighbour Retreat
System.out.println ("\n\n\n\n\n\n\n\n");
/*
* problem: Zhao Yun actually do not know is that strategy ah, he only know to dismantle the first bag,
* But do not know is backdoor this ingenious idea, how to do? It seems that this strategy pattern has already written out the name of the scheme
* * wrong!    Backdoor, Givengreenlight, Blockenemy is just a code, you write first, second, third, no one will say you are wrong!
*/
}
}

4. This is the strategy mode, cohesion Poly low coupling, and extensibility, to increase the policy class, modify Context.java on the line, so the application of a wide range of.

Java design mode (strategy pattern)

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.