Design Pattern New Concept 1 ~ Istrategypattern (Policy mode)-Details

Source: Internet
Author: User

I used to list the design patterns one by one to form a document release. I feel that this time is really a problem. Now I think that if I can write one article every day, it takes less time, well, let's start with the policy mode in the policy design mode first,
In the design mode, the policy mode is actually easy to understand. Simply put, it is a scenario, a thing, an operation. Of course, certain conditions are required for the operation, some people have put forward a very vivid column, that is, the column of Jin xiaomiao, saying that Liu Bei went to Jiangdong to fetch his wife. Zhuge Liang was afraid that Liu Bei would not be able to recruit zhou Yu, however, I specially wrote three tips and put them into a brocade forest. Zhao Yun, who went with Liu Bei Jiangdong, kept them, and told me that if you are in a hurry, you can open it. You just need to make a plan each time, here, we can clearly divide the division of labor in this strategic model, that is, the plot is actually practical, and the tip is used to develop a scheme. However, who will execute this scheme? This requires an operator, Zhao Yun. Therefore, in the Java object-oriented programming language, first, we divide the categories by class objects. First, we need a relative strategy, that is, a magic scheme. Here we come up with three ideas. The first one is: if something is wrong, the second one will be left behind. If you encounter a barrier, You need to insert yourself here ~ Open the road, and the third is: if we were found early by Zhou Yu before we ran away, we would need to immediately block it. Well, there is a good plan. Then, it is the execution scenario, this scenario has to be determined based on the above speculation. When it comes down, it will be the executor. Here, I don't hesitate to say Zhao Yunyi Meng. Well, these are all there, but how can we execute it? Here we need a public interface to perform the operation. I feel that the column name here is quite vivid. Below I will post the relevant code:
First, we need to define a public execution interface:
Public interface istrategy {
Public void operate (string getmethod );
}
The above interface defines an interface istrategy, which declares a method and carries a parameter to perform corresponding operations for obtaining its parameters.
Next, we need to define the related tips:
Tip 1: Escape
Public class flee implements istrategy {
Public void operate (string getmethod ){
String getstr = NULL;
If (getmethod! = NULL ){
Getstr = getmethod;
}
System. Out. println ("getmethod:" + getstr );
}
}
In fact, the above can return a value to determine whether you really want to escape. Otherwise, add it yourself.

Tip 2: Let go
Public class putmark implements istrategy {
Public void operate (string getmethod ){
String getstr = NULL;
If (getmethod! = NULL ){
Getstr = getmethod;
}
System. Out. println ("getmethod:" + getstr );
}
}

Tip 3: block attacks and seek troops
Public class blockpursure implements istrategy {
Public void operate (string getmethod ){
String getstr = NULL;
If (getmethod! = NULL ){
Getstr = getmethod;
}
System. Out. println ("getmethod:" + getstr );
}
}

Well, the above execution methods are available, and there are also plans. Next, we need a tip to install these tricks.
Well, how did we install it:
Public class packagecoup {
Private istrategy mistrategy;
Private string Info;
Public packagecoup (){}
Public packagecoup (string info ){
This.info = Info;
}
Public void exeoperatecoup {
This. mistrategy. Operate (Info );
}
}

OK, we have done the above tips and put three tips into it. At this time, Zhao Yun can safely accompany Liu Bei to Jiangdong to fetch his wife, I am still single. If anyone is interested, please contact me. If you have a chance, you can even meet one another, who told me that I am not a girl who likes bad boys? I don't know what girls are now. Hey, who can teach me how to learn bad? I really want to learn, as long as I can find a girl who thinks I can and can share my work with and love each other, I am really grateful. The saying goes far. Let's talk about it. The above tips are available, the tips are available, and the command execution methods are available, next, it requires a certain number of conditions to be executed by the designated persons. This is a question. Since Zhao Yun is responsible, Zhao Yun must have performed such an operation, see the following code:
Public class exeperson {
Private packagecoup MCM;
Private string Info;
Public static void main (string AGRs []) {
System. Out. println ("encountered problem: Zhou Yu got down and tried to find a solution. Open the first plan and check: quickly escape ");
Info = "flee ";
MPC = new packagecoup (New flee (), Info );
Mpc.exe opeartecoup (Info );
System. out, println, we are already in front ---------------------");
Info = "putmark ";
MPC = new packagecoup (New putmark (), Info );
Mpc.exe opeartecoup (Info );
System. out. println, the following section describes how to block it -----------");
Info = "blockpursure ";
MPC = new packagecoup (New blockpursure (), Info );
Mpc.exe operatecoup (Info );
System. Out. println ("OK, all three tips are used up. If you encounter any more problems, the road will remain uneasy. Fortunately, the road will eventually arrive safely ");
}
}

OK, the above is the process of Zhao Yun's rule execution through a clever plan. Let's take a look at it. Today we will talk about its policy model here. What new insights can be sprayed directly, and welcome to the discussion ....

Execution result:

One:
This is for flee!
------------------------------
Two
This is for putmark!
--------------------------------
Three
This is for blockpursure!

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.