Decorator mode (easy to understand)

Starbucks Coffee Order Item (Cafe):

  1. Coffee type/single-product coffee: Espresso (Italian espresso), ShortBlack, LongBlack (American coffee), Decaf (non-caused coffee)

  2. Seasoning: Milk, Soy (soy milk), Chocolate

  3. It is required to have good scalability, easy modification and easy maintenance when expanding new coffee types

  4. Use OO to calculate the cost of different types of coffee: customers can order single-origin coffee, or single-origin coffee + spices combination.

Solution 1-Solve the Starbucks Coffee Order Project

Insert picture description here

Solution 1-Analysis of solving Starbucks coffee order problems

  1. Drink is an abstract class, representing a drink

  2. des is the description of coffee, such as the name of the coffee

  3. The cost() method is to calculate the cost, and an abstract method is made in the Drink class.

  4. Decaf is single-origin coffee, inherits Drink, and realizes cost

  5. Espress && Milk is a single-origin coffee + spices, this combination is many

  6. Question: With this design, there will be many types. When we add a single-origin coffee or a new seasoning, the number of types will double,Class explosion

Solution 2-Resolve Starbucks coffee orders (good points)

  1. In the previous analysis, solution 1 is because the combination of coffee single product + seasoning will double the category, so it can be improved and the seasoning is built into the Drink category, so that the number of categories will not be excessive. Thereby improving the maintainability of the project (pictured)
    Insert picture description here

Option 2-Solve the problem analysis of Starbucks coffee orders

  1. Option 2 can control the number of classes, so as not to cause too many classes

  2. When adding or deleting seasoning types, the amount of code maintenance is very large

  3. Considering that the user can add multiple sauces, you can return hasMilk to a corresponding int

  4. Consider using decorator mode

Decorator pattern definition

  1. Decorator mode:Dynamically attach new features to objects. In terms of object function extension, it is more flexible than inheritance,The decorator mode also embodies the principle of opening and closing (ocp)
  2. The principle of dynamically attaching new functions to objects and ocp mentioned here will be reflected in the form of code in the following application examples.

Decorator pattern principle

  1. The decorator mode is like packing a courier.
    Main body: e.g. ceramics, clothes (Component) // Decorated person
    Package: e.g. newspaper filling, plastic foam, cardboard, wood board (Decorator)

  2. Component body: For example, similar to the previous Drink

  3. ConcreteComponent and Decorator
    ConcreteComponent: the specific main body,
    such as the previous single-product coffee

  4. Decorator: The decorator, such as the spices.
    In the pictureComponent 与ConcreteComponentIn the meantime, if there are many ConcreteComponent classes, you can also design a buffer layer to extract the common parts, and one class for the abstraction layer.
    Insert picture description here

Decorator mode resolves Starbucks coffee orders

Insert picture description here

Order in decorator mode: LongBlack with 2 pieces of chocolate + 1 piece of milk

Insert picture description here
Description

  1. Milk contains LongBlack

  2. A Chocolate contains (Milk+LongBlack)

  3. A Chocolate contains (Chocolate+Milk+LongBlack)

  4. In this way, no matter what form of single product coffee + seasoning combination, it can be easily combined and maintained through recursive methods.

Source code

slightly

Source code analysis of decorator pattern in JDK application

Java's IO structure, FilterInputStream is a decorator
Insert picture description here

public abstract class InputStream implements Closeable{
    
    } //是一个抽象类,即Component
public class FilterInputStream extends InputStream {
    
     //是一个装饰者类Decorator
protected volatile InputStream in //被装饰的对象}
class DataInputStream extends FilterInputStream implements DataInput {
    
     //FilterInputStream 子类,也继承了被装饰的对象in }

Description

  1. InputStream is an abstract class, similar to the Drink we mentioned earlier

  2. FileInputStream is a subclass of InputStream, similar to our previous DeCaf, LongBlack

  3. FilterInputStream is a subclass of InputStream: similar to our previous Decorator decorator

  4. DataInputStream is a subclass of FilterInputStream, the specific modifier, similar to the previous Milk, Soy, etc.

  5. The FilterInputStream class has protected volatile InputStream in; that is, the decorated person

  6. The analysis shows that in the io system of jdk, the decorator mode is used

The difference between bridge mode and decorator mode

The first argument is that the
decorator pattern is used to dynamically add responsibilities. The original function must be executed. What is important to the user is the added responsibilities.

The bridging mode is used to separate abstraction and implementation, that is, to classify attributes from different dimensions and bridge them in an aggregate manner to reduce coupling.

The second argument
1. The separation in the bridging mode actually refers to the separation of structure and implementation (when the structure and implementation may change) or the separation of attributes and attribute-based behaviors; while decorators just enclose attribute-based behaviors into independent the type.

2. The behavior in bridging is a horizontal behavior, and the behaviors are not related to each other; while the behavior in the decorator mode is superimposable, and the result shown is a whole, a result of a combination of individual behaviors

The third argument

the difference:

1. There is no primary and secondary difference between the decorator and the decorate, the bridged and the bridged are equal, and there is no need to inherit from the same parent class. (That is, the bridge can be interchanged)

2. Bridge modeDo not use the same interface; Decoration modeDecorate with the same interface, The interface is defined in the parent class.

Same point:

1. Both can handle proliferation

to sum up:

Bridge mode: abstract class (abstract interface)
decoration mode abstract parent class (abstract subclass)

to sum up

There is an abstract consciousness: those with the same characteristics will be abstracted. For
example, as mentioned above, they are abstracted into beverages and flavors,
and then based on suitable design patterns

Abstract parent class Drink
abstract subclass Decorator

The decorator pattern above is actually:
abstract subclass (abstract parent class)

Guess you like

Origin blog.csdn.net/weixin_46168350/article/details/111054323