Design Pattern (Java)-Coffe of Decorator Pattern Examples

definition

On the basis of not changing the original object, the function is attached to the object. Provides a more flexible alternative than inheritance (extend the original object function)

Types of

Structure type

Applicable scene

  1. Extend the functionality of a class or add additional responsibilities to a class
  2. Dynamically add functions to an object, or dynamically cancel functions.

advantage

  • A powerful supplement to inheritance, which is more flexible than inheritance, and extends functions to an object without changing the original object. (Inheritance in the extension function is static and must be determined at compile time, and the decorator can be determined at runtime, and the decorator is also based on inheritance)
  • Different effects can be achieved by using different decoration types and the permutation and combination of these types.
  • Comply with the opening and closing principle

Disadvantage

  • There will be more code, more classes, increasing the complexity of the program.
  • In dynamic decoration, multi-layer decoration will be more complicated. (Using inheritance to expand functions will increase the number of classes. Using the decorator pattern will not increase the number of classes as much as inheritance, but it will increase the number of objects. When the number of objects increases to a certain level, it will undoubtedly greatly increase us. Difficulty of code debugging).

Instance

Cafe order items:

1) Coffee types: Espresso, ShortBlack (concentrated), LongBlack, Decaf (sugar-free).

2) Seasoning: Milk, Soy, Chocolate.

Code

Note: Some coffees and spices have not been realized, and they are the same as realized coffee and spices.
Key point: Calling Beverage beverage

Abstract class

public abstract class Beverage {

    public String description="Unknown Beverage";

    public String getDescription() {
        return description;
    }

    public abstract double cost();
}

Coffee

public class Espresso extends Beverage {

    public Espresso(){
        description="Espresso";
    }

    @Override
    public double cost() {
        return 1.99;
    }
}
public class HouseBlend extends Beverage {

    public HouseBlend(){
        description="HouseBlend";
    }

    @Override
    public double cost() {
        return 1.89;
    }
}

Abstract class-decorator (seasoning)

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

Specific decorator-seasoning

public class Mocha extends CondimentDecorator {

    public Beverage beverage;

    public Mocha(Beverage beverage){
        this.beverage=beverage;
    }

    @Override
    public String getDescription() {
        return beverage.getDescription()+", Mocha";
    }

    @Override
    public double cost() {
        return 0.20+beverage.cost();
    }
}

Coffee order (main)

public class StarbuzzCoffee {
    public static void main(String args[]){
        Beverage beverage=new Espresso();
        //Single origin coffee
        System.out.println("Single Origin Coffee");
        System.out.println(beverage.getDescription()+"$"+beverage.cost());
        beverage=new Mocha(beverage);
        //Join Mocha's coffee
        System.out.println("Join Mocha's Coffee");
        System.out.println(beverage.getDescription()+"$"+beverage.cost());
    }
}

operation result

Intelligent Recommendation

JAVA design pattern of decorator pattern

The Decorator Pattern allows you to add new functionality to an existing object without changing its structure. This type of design pattern is a structural pattern, which is used as a wrapper for exis...

Java Design Pattern --------Decorator Pattern

Note: The content of this blog is from the Head First design mode, and only organizes and summarizes the articles. 1. The problem leads to We need to make a new order system for a coffee shop. The pre...

Java design pattern (decorator pattern)

1. The UML class diagram is as follows 2、Decorator.java 3、Person.java 4、AbstractDecorator.java 5、ShoesDecorator.java 6、TShirtDecorator.java 7. Use operation result: Wore a pair of shoes wore a T-Shirt...

Java design pattern----------decorator pattern

1 Introduction The decorator pattern is a structural design pattern. The purpose of using this mode is to expand the class more flexibly without affecting the structure of the original class. Some stu...

java-design pattern-decorator pattern

The key design of the decorator pattern is the component interface, and then both decorators and specific roles implement this interface. Next, use the decorator mode to realize the situation where th...

More Recommendation

Design pattern java【decorator pattern】

What is the decorator mode? Dynamically attach responsibilities to the object. If you want to extend the functionality, decorators provide a more flexible alternative than inheritance When to use deco...

Classification task 8 (fanwai)-parameter setting

Put the parameters I used in this part...

Springboot Excel View (POI)

Springboot Excel View (POI)   application:Export data using the view, save it to the Excel file Description:You can download data directly to the Excel file directly.     **************...

andriod simple installation of XposedInstaller

XposedInstaller andriod version 5.0 or less andriod version 5.0 or higher, you can go to the official website to download 1. sdk correspondence between 6.0 Android 5.0(Lollipop )——SDK21 An...

[CDH] Sqoop version of Hadoop, Hive and Sqoop pseudo-distributed environment to build

1, CDH version of pseudo-distributed Hadoop environment to build (1) Since the / opt is the owner of the root, become root, new directory cdh-5.3.6 in / opt:opt]# mkdir cdh-5.3.6 Change cdh-5.3.6 and ...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top