[Design mode]-policy Mode

Source: Internet
Author: User

Question:

Example: Starting from a simple simulation of duck applications, let's abstract a duck superclass. And let the various ducks inherit this superclass.

For example:


Obviously, this still doesn't seem to be a problem, but if the demand changes now (this is what programmers hate most), well, the boss now says it is necessary to make all ducks fly. Well, you can just add this method to the super class, but now the problem is that if one of our rubber ducks inherits this super class, it will fly !! This is obviously unscientific. And it is not a scream, it is a scream. At this time, we thought of the override feature inherited by the subclass. We made the subclass of the Rubber Duck overwrite the quack () inherited by the superclass and overwrite the fly () method so that it could do nothing, in this way, we get another figure:


What are the disadvantages of doing so? Every time we add a new duck, We have to rewrite the code we have already written. If the boss asks us to add a method in the superclass, we have to determine which ducks are not using this method. We need to maintain a lot of code, which is obviously very troublesome. Our programmers never do this troublesome thing. Let's try another way: we abstract the methods that may need to be judged, that is to say, we abstract the Flyable () and Quackable () superclasses and need the ducks to inherit them. This obviously cannot solve the problem of code reuse, we still need to overwrite the inherited methods.


Let's summarize the problems we encountered:

1. The Code repeats in multiple subclasses. 2. It is difficult to know the behavior of all ducks.

3. It is not easy to change during runtime, that is, when adding requirements 4. After we change the superclass, more problems may occur.


Now let's summarize the design model:

"Identify the potential changes in the application and separate them from those that do not need to be changed." In this way, the casual consequences caused by code changes become less, and the system becomes more elastic.


Well, it's time to extract the Duck behavior from the Duck class!

First, we understand the concept that "programming for interfaces" really means "programming for super classes" and "super classes": it is usually an abstract class or an interface. Here, we will no longer bind code to a superclass, which will make the code more flexible. Here we will involve "polymorphism", "Dynamic Link", and "upward transformation, for details, see my next blog. I will not talk about it more here. After all, I only do one thing at a time.


Let's take a look at the figure below:


With this design, flight and cronado actions can be reused by other objects because these actions are irrelevant to the duck class. Some newly-added behaviors will no longer affect existing behaviors.


Okay. Let's take a look at Duck's superclass again:


In this way, we can try to write the code: (only Duck and FlyBehavior interfaces and two FlyWithWings and FlyNoWay)

public abstract class Duck {FlyBehavior flyBehavior;QuackBehavior quackBehavior;public Duck() {}public abstract void dispaly();public void performFly() {flyBehavior.fly();}public void performQuack() {quackBehavior.quack();}public void swim() {System.out.println("All ducks float, even decoys");}}public interface FlyBehavior {public void fly();}public class FlyWithWings implements FlyBehavior {public void fly() {System.out.println("I'm flying");}}public class FlyNoWay implements FlyBehavior {public void fly() {System.out.println("I can't fly");}}


The best thing is that we can also add two methods to the Duck superclass to dynamically set Duck's flight and scream behavior:

public void setFlyBehavior(FlyBehavior fb) {flyBehavior = fb;}public void setQuackBehavior(QuackBehavior qb) {quackBehavior = qb;}

Here we have another design principle: "multi-purpose combination, less inheritance". As you can see, we can make the code more flexible through combination, it can also "dynamically change behavior during running ".


Now let's draw a general picture:


Summary <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Authorization + O3qNflo6y31rHwt + LXsMbwwLSjrMjDy/vDx9auvOS/authorization + authorization/zbunPC9wPgo8cD48aW1nIHNyYz0 = "http://www.2cto.com/uploadfile/Collfiles/20140530/20140530091550215.png" alt = "\">

(

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.