Design Pattern-decorator pattern, design pattern Decoration

Source: Internet
Author: User

Design Pattern-decorator pattern, design pattern Decoration

The decorator mode mainly embodies the principle of "opening to expansion and closing to modification" in the OO principle.

The modifier mode has the following features:

1. the decorator and the decorated object have the same super-type;

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

3 since the decorator and the decorated object have the same super-type, you can use the decorated object to replace the original object (wrapped;

4. the decorator Can add his/her own actions before and after the actions of the entrusted decorator to achieve a specific purpose;

5. objects can be decorated at any time, so they can be modified dynamically and unlimited at runtime by your favorite decorators.

Decorator mode definition:

Dynamically attaches the responsibility to the object. To expand the functionality, the decorator provides a more flexible alternative solution than inheritance.

Use Cases: Starbucks coffee order system, a coffee (for example, integrated, deep-baked, low-caffeine, concentrated) can be added with a variety of spices (milk, Moka, soy milk, milk bubble ), for example, if a customer wants a cup of double Moka soy milk coffee or milk Moka coffee, in this scenario, the basic coffee belongs to the decorator, and various spices belong to the decorator.

According to the characteristics of the decorator (the decorator and the object to be decorated have the same super type), first define an abstract Beverage class, which is the base class for drinks and spices, both drinks and spices inherit this abstract class.

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

 

The base class has a beverage description attribute, an abstract method for calculating the price, and a getDescription method for obtaining the description.

When declaring an abstract class for all spices, that is, the modifier class, note that this is the seasoning!

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

CondimentDecorator inherits from Beverage to meet the condition that the decorator and the object to be decorated have the same super type.

Let's take coffee concentrate as an example.

Public class Espresso extends Beverage {// inherits from Beverage because Espresso is a Beverage public Espresso () {// TODO Auto-generated constructor stub description = "Espresso "; // set the beverage description. The constructor is used here, or you can use the setter method. } @ Override public double cost () {// TODO Auto-generated method stub return 1.99; // return the Espresso price }}

The specific implementation of various spices

Moka's

Public class Mocha extends CondimentDecorator {// inherits from CondimentDecorator to ensure the same super type. Beverage beverage; // record drinks. Public Mocha (Beverage beverage) {// TODO Auto-generated constructor stub this. beverage = beverage;} @ Override public String getDescription () {// TODO Auto-generated method stub return beverage. getDescription () + ", Mocha"; // complete beverage description. } @ Override public double cost () {// TODO Auto-generated method stub return. 20 + beverage. cost (); // return the price of coffee drinks with Moka }}

Soy milk

public class Soy extends CondimentDecorator {    Beverage beverage;    public Soy(Beverage beverage) {        // TODO Auto-generated constructor stub        this.beverage = beverage;    }    @Override    public String getDescription() {        // TODO Auto-generated method stub        return beverage.getDescription() + ",soy";    }    @Override    public double cost() {        // TODO Auto-generated method stub        return .15 + beverage.cost();    }}

Milk bubble

package com.design.patterns.decorator;public class Whip extends CondimentDecorator {    Beverage beverage;    public Whip(Beverage beverage) {        // TODO Auto-generated constructor stub        this.beverage = beverage;    }    @Override    public String getDescription() {        // TODO Auto-generated method stub        return beverage.getDescription() + ",whip";    }    @Override    public double cost() {        // TODO Auto-generated method stub        return .10 + beverage.cost();    }}

Test the following code:

Public class StarbuzzCoffee {public static void main (String [] args) {// TODO Auto-generated method stub Beverage beverage = new Espresso (); // set a cup of Espresso, no seasoning is required. System. out. println (beverage. getDescription () + "$" + beverage. cost (); // print description and price }}

Print results

Espresso $1.99

Another cup of coffee with Moka milk

Public class StarbuzzCoffee {public static void main (String [] args) {// TODO Auto-generated method stub Beverage beverage = new Espresso (); // espresso beverage = new Mocha (beverage); // use Moka to decorate it beverage = new Whip (beverage); // use milk bubbles to decorate it System. out. println (beverage. getDescription () + "$" + beverage. cost (); // print description and price }}

Print results

Espresso,Mocha,whip $2.29

 

From the above example, we can see that the decorator mode uses combination and delegation to dynamically add new behaviors at runtime. You can wrap an object with countless decorator; the shortcomings of the decorator are also obvious, that is, the use of the decorator mode will lead to many small objects in the design, if the excessive use, will make the program very complex, therefore, in actual use, you can use a combination of the modifier mode and the factory mode.

  

  

 

Related Article

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.