Design Pattern-modifier Pattern

Source: Internet
Author: User

The modifier mode can implement a very important object-oriented design principle, that is, the class should be open to expansion, and the modification should be closed. In other words, the function of an object is dynamically extended without changing the original class file and using inheritance. It creates a packaging object, that is, decoration, to package a real object. However, be careful when selecting the code that needs to be extended. Using the open-close principle in every place is a waste of effort and is not necessary, which may make the code complex and hard to understand.


For example, what customers want to do if they want deep coffee with Moka and milk bubbles:


1. Get a dark coffee (darkroast) object

2. decorate it with a mocha object

3. decorate it with a milk bubble (whip) object

4. Call the cost () method and rely on the delegate to add the seasoning price


A mocha object is a modifier and its type "reflects" the object it decorated (in this example, it is a beverage ). The so-called "reflection" refers to the consistency of the two types. Therefore, mocha also has a cost () method. Through polymorphism, you can also regard any beverage wrapped in mocha as a beverage (because mocha is a child type of beverage ).


Whip is also a modifier, so it also reflects the darkroast type and includes a cost () method.


Therefore, the darkroast object wrapped by mocha and whip is still a beverage and can still have all actions of darkroast, including calling its cost () method.


It is time to calculate the money for the customer. You can call the cost () of the outermost modifier (whip. Whip's cost () will first entrust the object it decorated (that is, mocha) to calculate the bid, and then add the price of the milk bubble.


That is to say,

1. First call cost () of whip of the outermost ring modifier ();

2. Whip calls mocha's cost ();

3. Mocha calls darkroast's cost ();

4. drakroast returns the price;

5. In the result of darkroast, mocha will return a new price with its own price;

6. Whip adds its own price to the mocha returned result, and then returns the final result;


Note,

1. the decorator and the decorator have the same super type;

2. You can wrap an object with one or more decorators;

3. Since the decorator and the decorator have the same super type, you can replace it with a decorated object in any case that requires the original object (wrapped;

4. the decorator Can add his/her own behavior before and after entrusting the decorator to achieve a specific purpose;

5. objects can be decorated at any time, so you can use your favorite decorator to decorate objects dynamically and unlimited during running;


The modifier mode dynamically attaches the responsibility to the object. To expand the functionality, the decorator provides an alternative solution that is more flexible than inheritance.


Let's start code implementation:


The first is the beverage class:

package decorator;public abstract class Beverage {String description = "Unknown Beverage";public String getDescription(){return description;}public abstract double cost();}

Then we design the modifier class, the condiment abstract class:

package decorator;public abstract class CondimentDecorator extends Beverage{public abstract String getDescription();}

Now we have a base class, so we can implement some drinks:

For example:

package decorator;public class Espresso extends Beverage{public Espresso(){description = "Espresso";}public double cost(){return 1.99;}}

For example, integrated coffee (Starbucks exclusive coffee ):

package decorator;public class HouseBlend extends Beverage{public HouseBlend(){description="House Blend Coffee";}public double cost(){return .89;}}

The following code implements the seasoning:

Three methods are implemented first,

Mocha:

package decorator;public class Mocha extends CondimentDecorator{Beverage beverage;public Mocha(Beverage beverage){this.beverage=beverage;}public String getDescription(){return beverage.getDescription()+", Mocha";}public double cost(){return .20+beverage.cost();}}

Soy:

package decorator;public class Soy extends CondimentDecorator{Beverage beverage;public Soy(Beverage beverage){this.beverage=beverage;}public String getDescription(){return beverage.getDescription()+", Soy";}public double cost(){return .30+beverage.cost();}}

Whip:

package decorator;public class Whip extends CondimentDecorator{Beverage beverage;public Whip(Beverage beverage){this.beverage=beverage;}public String getDescription(){return beverage.getDescription()+", Whip";}public double cost(){return .40+beverage.cost();}}

The following is the main class of the call:

package decorator;public class Starbucks {public static void main(String[] arga){Beverage beverage=new Espresso();System.out.println(beverage.getDescription()+" $"+beverage.cost());beverage = new Mocha(beverage);//beverage = new Mocha(beverage);beverage = new Whip(beverage);beverage = new Soy(beverage);System.out.println(beverage.getDescription()+" $"+beverage.cost());}}


Design Pattern-modifier Pattern

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.