Design Mode (1) -- Policy mode, design mode -- Policy Mode

Source: Internet
Author: User
Tags getcolor

Design Mode (1) -- Policy mode, design mode -- Policy Mode

What is a policy model?

The policy mode defines the algorithm family and encapsulates them separately so that they can be replaced with each other. This mode allows the algorithm to be independent of the users who use the algorithm.

Do you understand the above definition?

I think you must have understood it. Let's see how I understand it.

It is to classify some behaviors in a base class and then select these specific classes in the application to a specific class. The following is an example.

In-depth understanding of policy Modes

  If you have a requirement to write a duck base class, the duck base class requires three methods: quak, fly, and display, the specific duck type inherits this base class.

According to this requirement, the first thing that comes to mind is

Public class Duck {private String color; public Duck () {} public Duck (String color) {super (); this. color = color;} public String getColor () {return color;} public void setColor (String color) {this. color = color;} public void quake () {System. out. println ("Ga ga... ");} public void display () {System. out. println ("I am a" + color + "Duck");} public void fly () {System. out. println ("flying, flying... ");}}

 

After the base class is completed, other specific ducks inherit the base class and write other special attributes or methods.

But what? Is there any problem? Are all ducks named as grags? Can all ducks fly? I think there must be some amazing ducks... so we have this problem in our design. How should we solve it at this time? At this time, you should think of it as an interface to let other subclasses implement it. The specific code should be as follows:

public interface BaseDuck {    void quak();    void display();    void fly();}

Alternatively, we can define an abstract class to implement some methods that do not need to be changed in the base class, while other methods are implemented by subclasses, for example:

Public abstract class AbstractDuck {private String color; public String getColor () {return color;} public void setColor (String color) {this. color = color;} public abstract void quak (); public void display () {System. out. println ("I am a" + color + "Duck");} public abstract void fly ();}

At this time, this code is okay, but does it really have no defects? One problem may be ignored, that is, our design is to adapt to the amazing duck species. Most of the other ducks should have the same sound and can fly. So we will write a lot of redundant code when writing child classes. How can we solve this problem?

Policy mode solves this problem. How does it implement it? First, let's analyze this base class. quak and fly are easy to change. The customer may just add a toy duck or something that day. Therefore, these two behaviors are extracted and written separately. First define two interfaces, QuakBehavior and FlyBehavior, and then write some sub-classes as needed, such as flying and not flying, 'grachary' and 'creaking' and not calling. The sample code is as follows:

The following code contains multiple files:

// QuackBehavior. javapublic interface QuackBehavior {void quak ();} // FlyBehavior. javapublic interface FlyBehavior {void fly ();} // Quack. javapublic class Quack implements QuackBehavior {@ Override public void quak () {System. out. println ("Ga ga... ") ;}} // Squeak. javapublic class Squeak implements QuackBehavior {@ Override public void quak () {System. out. println ("squeaking... ") ;}} // QuackNoWay. javapublic class QuackNoWay implements QuackBehavior {@ Override public void quak () {System. out. println ("I won't call it... ") ;}// FlyWithWings. javapublic class FlyWithWings implements FlyBehavior {@ Override public void fly () {System. out. println ("flying, flying... "); }}// FlyNoWay. javapublic class FlyNoWay implements FlyBehavior {@ Override public void fly () {System. out. println ("I won't fly... ");}}

The base class should be designed as follows:

Package model. strategy. v2; public class Duck {private String color; // Add the behavior interface FIELD private QuackBehavior quackBehavior; private FlyBehavior flyBehavior; public Duck () {} public Duck (String color, QuackBehavior quackBehavior, flyBehavior flyBehavior) {super (); this. color = color; this. quackBehavior = quackBehavior; this. flyBehavior = flyBehavior;} public String getColor () {return color;} public void setColor (String color) {this. color = color;} public void quake (){
// Call the interface method quackBehavior. quak ();} public void display () {System. out. println ("I am a" + color + "Duck");} public void fly (){
// Call the interface method flyBehavior. fly ();}}

Now think about whether this problem is a good solution! If a new requirement comes, we only need to define a new interface implementation, without modifying the code in this base class.

I believe you have a good understanding of the policy model at this time. If you have any opinions or questions about this blog, you can leave a message> v <

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.