Design Pattern-policy pattern & state pattern in. net

Source: Internet
Author: User

Anyone involved in design patterns should have heard of the names of some patterns, including policy patterns and status patterns. they are the objects I want to discuss today. the reason for putting them together is that they have a certain relationship to help you really understand the design pattern. why is there no third one, because there are too many things to be said clearly.

 

The official definitions of these two models are as follows:

Rule mode:Define a seriesAlgorithm, Encapsulate them one by one, and make them replace each other. This allows algorithms to change independently of their customers.

Status mode: Allows an object to change its behavior when its internal state changes. The object seems to have modified its class.

These two definitions are quite easy to understand. They can be roughly summarized as "objects can flexibly select different behaviors/methods based on different conditions". In this case, the policy mode and the state mode are not the same ?! Now it seems that this is the case, but let's continue to look at it.

 

This time, let's look at the class diagrams of the two, as shown below:

Figure 1. Rule Mode

Figure 2. Status Mode

At this time, careful people will find that the UML class diagrams of these two modes are exactly the same. Do you feel a little confused ?! Here, I 'd like to sell a token to you.

 

When you study the design model, you may not find out what I have mentioned today or what I think is wrong with your understanding of the design model. But do you really understand it? There are a lot of posts on the Internet about the design model, almost the same in terms of content and argument. Just give an example orCodeShows you learned the design mode. however, after careful comparison, they found that their content is almost the same as that in textbooks and only contains part of it. as a result, people are still confused, and they do not know how to install it.

 

Now, let's get down to the truth and come back and talk about our strategy mode and state mode. through the above description, we will find that the two are almost identical from implementation to design or conclusion. so are they in the same pattern? There are two answers: first, yes, and second, no.

Answer 1. "Yes": it is from some development or design perspectives, just like the above process. But this is only a superficial phenomenon;

Answer 2 "no": Obviously, the four groups are much higher than most of us. In contrast, we are more likely to make mistakes than they are. so what are the differences between the two design models?

 

I have read this book and we all know that both the policy mode and the status mode belong to the gof23 design mode.Behavior modeThe two have the same appearance, but they have different concerns.Behavior changes, Status mode followHow changes in object status can lead to changes in object behavior.

Two roles in Rule mode:Customers and policies.The customer's responsibility is to understand the differences in behavior,Define the same interface.The role of a policy is to provide different implementations,And select according to the customer's intention.It is generally difficult to know how many kinds of behavior There are, because there will be different changes in the future. in reality, a simple example is to select different encryption algorithms based on customer requirements. when there is a new encryption algorithm, the customer needs to know this change. In this case, the policy can flexibly choose to use the new algorithm.

The state pattern is an image metaphor: "full sleep, hungry ". here, "saturated hunger" is two states, "sleeping and eating" is two kinds of behavior. another typical example is a bank account. different users can perform different operations based on the account balance. it should be noted that the ing between status and behavior in the state mode. although not one-to-one correspondence, some information is hidden, that is, in the actual example, the behavior and status are limited and stable, and the behavior is unique..Limited and stable refers to the behavior of objects,Unless the business needs change,Otherwise, it will not change.Uniqueness means that the object has only one eating behavior,Second, there will be no second food behavior.

At this time, the difference between the rule mode and the state mode is clear.

Rule mode focuses on behavior changes,But in the end, there is only one action,Only the implementation of behaviors is changed.The customer does not pay attention to these issues.New changes have no impact on customers.

The status mode also focuses on behavior changes,However, this change is driven by the status,Generally, each status and action is different.The new status or action is generally different from the existing one,Customers need to pay attention to these changes.

 

The following are two examples.

1. In mobile network optimization, genetic algorithms, simulated annealing algorithms, and topology-based algorithms are generally used. in actual projects, we can often use different algorithms based on different situations, while customers often do not care about the algorithms used. engineers need to know the fourth algorithm, but customers do not need to know it.

2. The second development based on GIS components, out of the existing map mouse operations, we often need to add our own mouse behavior. At this time, a good design is that we expand the mouse state, expand the mouse behavior, and then match them. at the same time, the original mouse behavior is not affected. genetics

Please carefully compare the detailed differences in the following code snippets.

1. Rule Mode

2. Status Mode

 

Code

     Public     Override     Void  Action (  String  Mousestate ){
Base . Action (mousestate );
If (Mousestate = " Pan " )
Pan ();
Else If (Mousestate = " Select " )
Select ();
}
Public Void Pan (){ /* Pan Map */ }
Public Void Select (){ /* Select Image */ }

 

 

 

 

Code

    Public     Void  Topology (){
// Obtain Result B by using the extended topology calculation
}
Public Void Gene (){
// Calculate a using gene to obtain result B
}
Public Void Simulation (){
// Calculate Result B in the simulation mode.
}
Public Void Strategy ()
{
String Flag = "" ;
If (Flag = " Topology " )
Topology ();
Else If (Flag = " Gene " )
Gene ();
Else If (Flag = " Simulation " )
Simulation ();
// Work complated
}

 

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.