Design pattern--the similarities and differences between agent mode and decoration mode

Source: Internet
Author: User

The characteristics of the two types of models

Decoration Mode:

Dynamically extends access to objects without changing the interface.
Dynamic inheritance, which enables a class to have the ability to change behavior at run time.
The decorative pattern, which is highlighted by the run-time increase behavior, is different from inheritance, and inheritance is an increase in behavior at compile time .
Emphasis: Enhanced

Proxy mode:

Controls access to the object without changing the interface.
1. From the encapsulation point of view, is to solve the class and class calls each other and the resulting coupling relationship, can be said to be another layer of interface reference.
For example: In the Class A->B proxy->c class, all the behavior of Class C is hidden in B. That is, the caller does not know what to access and what object is being proxied.
2. From a reuse perspective, when different classes call a complex class, a new class is created for the entire complex class simply because of minor changes.
For example: Class A->c class 1;b class->c Class 2.
can become a class->ca proxy class->c class, Class B->CB proxy class-C class.
Proxy mode is the encapsulation between classes and (in some ways) multiplexing.
  Emphasis: Limitations

Give an example to illustrate the problem

Here's an example to illustrate the difference between the two. When we're making coffee , first you have to make a cup of coffee, and then you can consider whether to add side sugar or cow 's milk .

Then the coffee can be made from sugar or milk or sugar and milk, into sweetened coffee, coffee with milk, sweetened with milk and coffee.

But it's still a cup of coffee, but its properties are expanded.

The next step is to simulate the process by decorating mode and proxy mode respectively.

1. Decoration mode

Test code:

1Cafe Cafe =NewConcretecafe ();2  3Cafe Milkcafe =NewMilkdecorator (CAFE);4 Milkcafe.getcafe ();5System.out.println ("----------------------------------------");6Cafe Sugarcafe =NewSugardecorator (CAFE);7 Sugarcafe.getcafe ();8System.out.println ("----------------------------------------");9Cafe Sugarmilkcafe =NewMilkdecorator (NewSugardecorator (NewConcretecafe ()));Ten Sugarmilkcafe.getcafe (); OneSystem.out.println ("----------------------------------------"); ACafe Milksugarcafe =NewSugardecorator (NewMilkdecorator (Cafe)); - Milksugarcafe.getcafe (); -System.out.println ("----------------------------------------");

Just like using an IO stream:

1 New BufferedReader ( new InputStreamReader (new fileinputstream (file)); // character Stream 2 New DataInputStream ( new Bufferedinputstream (new fileinputstream (file)); // Byte stream

The functionality of the class is enhanced.

2. Proxy mode

Test code:

1 New Proxy (); 2 cafe.getcafe ();

Now it's clear to see:

1. The decoration mode allows the user to visually see which features are enhanced, while the proxy mode completely restricts the user.

2. For decorative mode, the decorator (Decorator) and the decorator (Cafe) all implement the same interface.

3. For proxy mode, both the proxy class and the real class are implemented with the same interface.

4. In addition, no matter which pattern we use, it is easy to add a custom method to the actual object before or after the method.

Comparison of decoration mode and inheritance

Obviously, the adornment mode can dynamically extend the behavior of the object.

For example, an object has 30 behaviors, but in the first stage it uses 1-20 behavior, and the second stage uses 11-30 behaviors, so at this time, we can define only 11-20 behavior.

When you run the first stage, you can add the 1-10 behavior to "decorate 1"

In the second stage of operation, the "Decoration 1" can be removed, the 21-30 can be "decorate 2" to add.

However, inheritance increases behavior at compile time.

Advantages and disadvantages of decoration mode

Advantages:

1. Decoration mode can provide more flexibility than inheritance.
2. It is possible to extend the functionality of an object in a dynamic manner by selecting different adorners at run time to achieve different behavior.
3. Many different combinations of behaviors can be created by using different decorative classes and arranging combinations of these decorations. You can decorate the same object with more specific decorative classes to get more powerful objects.
4. The concrete component class and the concrete adornment class can change independently, the user may add the new concrete component class and the concrete adornment class according to the need, in the use and then the combination, the original code does not need to change, conforms to "the opening and closing principle".

Disadvantages:

1. There will be a lot of small objects (specific decoration classes), which increases the complexity of the system.
2. This more flexible than inheritance features, but also means that the decoration mode is more error-prone than inheritance, troubleshooting is also very difficult, for many decorative objects, debugging the search for errors may need to step-by-step troubleshooting, more cumbersome.

Reference blog:

UML Class Diagram

The difference between the cosmetic mode and the proxy mode

Design pattern--the difference between decoration mode and agent mode

The beauty of Java--design pattern

Decorator mode

Design pattern--the similarities and differences between agent mode and decoration mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.