24 design patterns-strategy patterns [strategy pattern]

Source: Internet
Author: User

Liu Bei is about to marry his wife in Jiangdong. Zhuge Liang gave Zhao Yun (Best Man) three tips before, saying that they had taken the opportunity to solve the difficult problem. Hey, don't say that it really solved the big problem, in the end, Zhou Yu lost his wife and had a discount. Let's take a look at what the scene looks like.

Let's first talk about the elements in this scenario: three tips, one tip, and one Zhao Yun. The tips are given by Comrade Xiao Liang. They are placed in the tips, which are commonly known as tips, then Zhao Yun is a working person. How can he use a Java program to get out the tips, execute the tricks, and then win?

Let's first look at the class diagram:

If the three tips are of the same type, you can write an interface:

1 package COM. iadmob. strategy; 2 3/** 4 * @ author http://www.cnblogs.com/initial-road/ 5 */6 public interface istrategy {7 8 // each tip is an executable algorithm 9 Public void operate (); 10 11} 12 13 and write three implementation classes. There are three tips: 14 15 16 package COM. iadmob. strategy; 17 18 19/** 20 * @ author http://www.cnblogs.com/initial-road/21 * look for Joe's help, so that Sun Quan cannot kill Liu Bei 22 */23 public class backdoor implements istrategy {24 25 26 @ override27 public Vo Id operate () {28 system. out. println ("Ask Qiao guolao for help and ask Wu Guotai to exert pressure on Sun Quan"); 29} 30 32} 33 34 35 36 package COM. iadmob. strategy; 37 38/** 39 * @ author http://www.cnblogs.com/initial-road/40 * ask Wu Guotai to open a green light 41 */42 public class givengreenlight implements istrategy {43 44 @ override45 public void operate () {46 system. out. println ("ask Wu Guotai to open a green light and let it go! "); 47} 48 49} 50 51 52 53 package COM. iadmob. strategy; 54 55/** 56 * @ author http://www.cnblogs.com/initial-road/57 * after Mrs sun is disconnected, block chase 58 */59 Public class blockenemy implements istrategy {60 61 @ override62 public void operate () {63 system. out. println ("Mrs sun blocks the pursuit of troops"); 64} 65 66} 67 68 // Well, let's take a look. There are three plans, you need to have a solution to this problem: 69 70 package COM. iadmob. strategy; 71 72/* 73 * @ author http://www.cnblogs.com/initial-road/74 * strategy, it also needs a tip 75 */76 public class context {77 // constructor, which one do you want to use? 78 private istrategy strategy; 79 80 public context (istrategy Strategy) {81 This. strategy = strategy; 82} 83 84 // use strategy. I tried 85 public void operate () {86 This. strategy. operate (); 87} 88}

Then Zhao yunxiongyi carried three tips and pulled Liu's father, who had entered the ranks of the elderly and wanted to marry a pure girl, to settle for the family. Hi, don't say that, xiao Liang's three tips are really good:

1 package COM. iadmob. strategy; 2 3/** 4 * @ author http://www.cnblogs.com/initial-road/ 5 */6 public class zhaoyun {7/** 8 * Zhao Yun appeared, according to Zhuge Liang to him, choose 9 */10 public static void main (string [] ARGs) {11 context; 12 // remove the first 13 system when Wu Guo just arrived. out. println ("-------- split a -------- when Wu Guo just arrived"); 14 context = new context (new backdoor (); // 15 context with a clever plan. operate (); // execute 16 system. out. println ("\ n \ N "); 17 18 // Liu Bei is happy, remove the second 19 system. out. println ("-------- Liu Bei is happy, remove the second --------"); 20 context = new context (New givengreenlight (); 21 context. operate (); // executes the second tip 22 system. out. println ("\ n"); 23 24 // What should I do if Sun Quan's sub-troops have followed suit? Remove the third 25 system. out. println ("-------- Sun Quan's small army chased, what should we do, split the third --------"); 26 context = new context (New blockenemy (); 27 context. operate (); // Mrs sun retired 28 system. out. println ("\ n"); 29 30/** 31 * the problem arises: Zhao Yun does not actually know the policy, he only knows how to split the first tip, 32 * but does not know how it is a backdoor solution. What should he do? It seems that this strategy model has written 33 * incorrect strategy name! Backdoor, givengreenlight, and blockenemy are just a code. You write them as first, second, and 34 * Third. No one will say you are wrong! 35*36 * the advantage of the Policy mode is that it reflects the characteristics of high cohesion and low coupling. The disadvantage is that I will go back and check 37 */38 39} 40}

In these three moves, Zhou Lang is "Lost and defeated! This is the policy mode. The features of high cohesion and low coupling are also shown. There is also a scalability, that is, the OCP principle. The policy class can be added as long as the context is modified. java is enough. I will not talk about this much. Let's understand it.

 

24 design patterns-strategy patterns [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.