Decorator pattern of design patterns

Starbucks Coffee Order Items
Starbucks Coffee Order Items (Cafe):
1) Coffee type/single origin coffee: Espresso (Italian espresso), ShortBlack, LongBlack (American
coffee), Decaf (decaf)
2) Seasoning: Milk, Soy (soy milk), Chocolate
3) When expanding new types of coffee, it is required to have good expansibility, easy modification and easy maintenance
4) Use OO's to calculate the cost of different types of coffee: customers can order single-origin coffee, or single-origin coffee
Brown + seasoning combination.






Decorator pattern definition
Decorator pattern definition
1) Decorator Pattern: Dynamically attach new functionality to an object. It is more efficient than inheritance in terms of object function extension
Flexible, the decorator pattern also embodies the open-closed principle (ocp)
2) The principle of dynamically attaching new functions to objects and ocp mentioned here will be replaced by the following application examples.
The code is reflected in the form, please pay attention to the experience.
Decorator pattern principle
1) The decorator pattern is like packing a courier
 Main body: such as: ceramics, clothes (Component) // the person to be decorated
 Packaging: such as: newspaper filling, plastic foam, cardboard, wooden board (Decorator)
2) Component
Main body: For example, similar to the previous Drink
3) ConcreteComponent and Decorator
ConcreteComponent: concrete body,
Such as the previous single product coffee
Decorator: Decorator, such as each seasoning.
4) Between the Component and ConcreteComponent as shown in the figure, if
There are many ConcreteComponent classes, you can also design a buffer layer to extract the common parts,
abstraction layer a class

public abstract class Drink {

    public String des; // describe
    private float price = 0.0f;
    public String getDes() {
        return des;
    public void setDes(String des) {
        this.des = des;
    public float getPrice() {
        return price;
    public void setPrice(float price) {
        this.price = price;

    //An abstract method for calculating fees
    //subclass to implement
    public abstract float cost();

public class Coffee  extends Drink {

    public float cost() {
        return super.getPrice();
public class DeCaf extends Coffee {

    public DeCaf() {
        setDes(" decaf ");
public class Espresso extends Coffee {
    public Espresso() {
        setDes(" Italian coffee ");
public class LongBlack extends Coffee {

    public LongBlack() {
        setDes(" longblack ");
public class ShortBlack extends Coffee{
    public ShortBlack() {
        setDes(" shortblack ");
public class Soy extends Decorator{

    public Soy(Drink obj) {
        setDes(" soy milk  ");

public class Milk extends Decorator {

    public Milk(Drink obj) {
        setDes(" milk ");

//specific Decorator, here is the condiment
public class Chocolate extends Decorator {

    public Chocolate(Drink obj) {
        setDes(" chocolate ");
        setPrice(3.0f); // price of condiments



public class Decorator extends Drink {
    private Drink obj;

    public Decorator(Drink obj) { //combination
        this.obj = obj;

    public float cost() {
        // getPrice own price
        return super.getPrice() + obj.cost();

    public String getDes() {
        // obj.getDes() Output the information of the decorated person
        return des + " " + getPrice() + " && " + obj.getDes();


public class CoffeeBar {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        // Order in Decorator Mode: 2 Chocolates+a serving of milk LongBlack

        // 1. order one LongBlack
        Drink order = new LongBlack();
        System.out.println("Fee 1=" + order.cost());
        System.out.println("describe=" + order.getDes());

        // 2. order add a milk
        order = new Milk(order);

        System.out.println("order Add a serving of milk =" + order.cost());
        System.out.println("order Add a portion of milk Description = " + order.getDes());

        // 3. order add a chocolate

        order = new Chocolate(order);

        System.out.println("order Add 1 milk Add 1 chocolate =" + order.cost());
        System.out.println("order Add one part milk Add one part chocolate Description = " + order.getDes());

        // 3. order add a chocolate

        order = new Chocolate(order);

        System.out.println("order Add 1 part milk Add 2 parts chocolate =" + order.cost());
        System.out.println("order Add 1 part milk Add 2 parts chocolate Description = " + order.getDes());
        Drink order2 = new DeCaf();
        System.out.println("order2 decaf coffee =" + order2.cost());
        System.out.println("order2 Decaf Coffee Description = " + order2.getDes());
        order2 = new Milk(order2);
        System.out.println("order2 Decaffeinated coffee with one serving of milk =" + order2.cost());
        System.out.println("order2 Decaffeinated coffee with one serving of milk Description = " + order2.getDes());



print result:



public class Decorator {

    public static void main(String[] args) throws Exception{
        // TODO Auto-generated method stub
        //1. InputStream is an abstract class, Similar to what we said before Drink
        //2. FileInputStream Yes  InputStream subclass, similar to our previous DeCaf, LongBlack
        //3. FilterInputStream  Yes  InputStream Subclass: similar to our previous Decorator groomer
        //4. DataInputStream Yes FilterInputStream Subclasses, specific modifiers, similar to the previous Milk, Soy Wait
        //5. FilterInputStream class has  protected volatile InputStream in; including the decorated
        //6. analysis results in jdk of io In the system, the decorator pattern is used
        DataInputStream dis = new DataInputStream(new FileInputStream("d:\\abc.txt"));



Excerpted from the Notes of the Great God Han Shunping


Posted by Zilvermeeuw on Wed, 01 Jun 2022 07:05:07 +0530