Daily 18 / 07 / 22 your design mode! Finally have time to write ~~

1/24: Strategy pattern First, I will give ...
1/24: Strategy pattern

First, I will give you a design mode that I think is simple~

Liu Bei is going to Jiangdong to marry his wife. Before he left, Zhuge Liang gave Zhao Yun (the best man) three masterpieces, saying that it was a natural opportunity to solve difficult problems,
Hey, don't mention it. It's really solved the big problem. After that, Zhou Yu accompanied his wife and broke the army. Let's see what the scene looks like first
.
Let's start with the elements of the scene: three masterpieces, one brocade bag and one Zhao Yun. The masterpieces were given by Comrade Xiao Liang and placed in the brocade bag
Li, which is commonly known as the "magic trick". Zhao Yun is a worker. Take the magic trick out of the magic trick, execute it, and then win. Use JAVA program

Code on OK

package designmodel24.strategypattern; /** * Flowers are blooming again, people are no longer young~ * Daily report ~ 5 articles * 16:54 2018/7/18 * Wednesday * practice */ public class BackDoor implements IStrategy { @Override public void operate() { System.out.println("Ask Qiao Guolao for help and ask Wu Guotai to put pressure on Sun Quan"); } }
package designmodel24.strategypattern; /** * Flowers are blooming again, people are no longer young~ * Daily report ~ 5 articles * 16:57 2018/7/18 * Wednesday * practice */ public class BlockEnemy implements IStrategy{ @Override public void operate() { System.out.println("After Mrs. sun died, stop the pursuers"); } }
package designmodel24.strategypattern; /** * Flowers are blooming again, people are no longer young~ * Daily report ~ 5 articles * 16:57 2018/7/18 * Wednesday * practice * With these three moves, Zhou Lang is "with his wife and then with his soldiers"! This is the strategy mode. The characteristics of high cohesion and low coupling are also shown, * The other one is extensibility, that is, OCP principle. The policy class can continue to be added as long as the Context.java is modified */ public class Context { //The construction is going to use a magic plan private IStrategy strategy; Context(IStrategy strategy) { this.strategy = strategy; } public void operate() { this.strategy.operate(); } }
package designmodel24.strategypattern; /** * Flowers are blooming again, people are no longer young~ * Daily report ~ 5 articles * 16:56 2018/7/18 * Wednesday * practice */ public class GivenGreenLight implements IStrategy { @Override public void operate() { System.out.println("Ask Wu Guotai to turn on the green light"); } }
package designmodel24.strategypattern; /** * Flowers are blooming again, people are no longer young~ * Daily report ~ 5 articles * 16:31 2018/7/18 * Wednesday * practice */ public interface IStrategy { void operate(); }
package designmodel24.strategypattern; /** * Flowers are blooming again, people are no longer young~ * Daily report ~ 5 articles * 17:01 2018/7/18 * Wednesday * practice */ public class ZhaoYun { public static void main(String[] args) { Context context; //When I met Wu Guotai, I broke the first one System.out.println(" //When I met Wu Guotai, I broke the first one“); context = new Context(new BackDoor()); context.operate();//Dismantle and execute System.out.println("\n\n\n"); //Liu Bei is happy not to think of Shu, but to tear down the second one System.out.println("//Liu Bei is happy not to think of Shu, but to tear down the second one“); context = new Context(new BackDoor()); context.operate();//Execute the second System.out.println("\n\n\n"); //The chasers of the eastern Wu Dynasty come and tear down the third one System.out.println(" //The chasers of the eastern Wu Dynasty come and tear down the third one“); context = new Context(new BackDoor()); context.operate();//Execute the third lady sun's retreat System.out.println("\n\n\n"); } } /* * Here comes the question: Zhao Yun actually doesn't know the strategy. He only knows to tear down the first brocade bag, * but doesn't know it's BackDoor. What should I do? It seems that this strategic model has written out the name of the scheme* * Wrong! BackDoor, given greenlight and BlockEnemy are just one code. You write them as first, second and third. No one will say you are wrong* * The advantage of the strategy mode is that it embodies the characteristics of high cohesion and low coupling. The disadvantage is that ^ */

Yo Xi ~ ~ ~ ~ I don't know where I have to leave a message. Hey, hey, hey, blogger goes to play games with beautiful women

2 February 2020, 12:11 | Views: 5301

Add new comment

For adding a comment, please log in
or create account

0 comments