当前位置:网站首页>Decorator mode of structural mode

Decorator mode of structural mode

2022-06-22 19:34:00 OldZhangYH

Source code

Decorator mode

Decorator mode can be used without changing the existing object structure , Dynamically add some responsibilities to the object ( Just add some extra functions ).

structure

  1. Abstract component class (Component): Define an abstract class or interface to specify the object that needs additional functions .
  2. Concrete component class (ConcreteComponent): Implement abstract components , Add some responsibilities to the character by decorating it .
  3. Abstract decorator (Decorator): Inherit or implement abstract interfaces , And contains instances of specific components , You can extend the function of a specific component through its subclasses .
  4. Concrete decoration (ConcreteDecorator): Concrete methods for implementing abstract decoration classes , And add responsibilities to specific component classes .

Case study

Order at a fast food restaurant , We can order a fried rice , Then add an egg , Add a beef . Ordering a fried rice is a concrete construction class , Add an egg , Adding beef is an extra duty .
Of course, we can also use inheritance , Let an egg fried rice inherit fried rice , Beef fried rice inherits fried rice . But there may be too many such classes , And the scalability is not good .
The class diagram is as follows , The key point is ExtFood abstract class , He inherited FastFood Also cited FastFood.

public class test {
    public static void main(String[] args) {
        Rice rice=new Rice();
        System.out.println(rice.getName()+":" +rice.cost());
        Egg egg=new Egg(rice);
        System.out.println(egg.getName()+":" +egg.cost());
        Beef beef=new Beef(egg);
        System.out.println(beef.getName()+":" +beef.cost());
    }
}

Output :
Fried rice :10.0
Fried rice with eggs :11.0
Fried rice with egg and beef :16.0

advantage

  • Decorator pattern can bring more flexible extension functions than inheritance , It's more convenient to use . By combining different specific decoration classes, we can obtain diversified results with different functions . The principle of opening and closing is obeyed . Inheritance is a static additional responsibility ( It's dead ), Decorator is a dynamic additional responsibility .
  • Concrete decoration classes and concrete component classes can be extended independently of each other without coupling .( Ham sausage can be added 、 Fried noodles or something ). Decorator pattern can dynamically extend the functions of implementation classes .

Use scenarios

  1. When it is not possible or convenient to use inheritance to extend the system .
    • For example, there are a large number of independent extensions in the system , In this way, in order to support their group, there will be many subclasses .
    • final class
  2. Without affecting other objects , Dynamic 、 Add responsibilities to individual objects transparently .
  3. When the function of an object needs to be added and removed dynamically .( The eggs are sold out , Just delete the eggs )

JDK The case in

IO The wrapper class in the stream uses the decorator pattern .BufferedXXXXX.
BufferedWriter The class diagram , The key on the right is both inheritance and aggregation .

The difference between and agent mode

  • The same thing

    • To implement the same target object as the target class .
    • Declare the target object in the class .
    • You can extend the target method without modifying the target class .
  • Difference

    • The decorator is to enhance the target object , Proxy mode is to protect and hide objects
    • The target object declared in the decorator is transmitted from the outside , The proxy pattern is created in the class .
原网站

版权声明
本文为[OldZhangYH]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206221758470342.html