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)
Structure type
- Extend the functionality of a class or add additional responsibilities to a class
- Dynamically add functions to an object, or dynamically cancel functions.
- 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
- 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).
Cafe order items:
1) Coffee types: Espresso, ShortBlack (concentrated), LongBlack, Decaf (sugar-free).
2) Seasoning: Milk, Soy, Chocolate.
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

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...
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...
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...
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...
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...
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...
Put the parameters I used in this part...
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. **************...
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...
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 ...