The difference between Java proxy mode and decorator mode

Source: Internet
Author: User

Decorative mode: The function of extending objects transparently to the client is an alternative to the inheritance relationship;
Proxy mode: Provides a proxy object for an object, and has a proxy object to control the reference to the original object;


The adornment mode should be enhanced for the decorated object, and the proxy mode exerts control over the object of the agent, and does not provide the enhancement of the object itself.

The implementation mechanism is exactly the same, and you can see that their instance code repeats a lot. But semantically speaking, the function of the two is the opposite, and one important function of the pattern is to simplify the understanding of your program by other programmers,

You write the decorations in one place, and everyone knows that it is in addition to the function that you write the agent that everyone knows is in the limit,

The code is probably the same, but if you call them decorations, others will be confused.

--------------------------------------------------------------

When learning AOP, the textbook said that the use of dynamic agents, but in the impression that the agent mode has always been control access what, how to dynamically increase the behavior, dynamic increase behavior is not the adorner mode? So I found a lot of information, trying to figure out what the difference is between the two. The results found this article in English is very clear, translation, for reference.

First, let's take a look at the following two UML class diagrams, which describe the basic implementation of the adorner pattern and the proxy pattern, respectively.

These two graphs may confuse us. The two design patterns look very much alike. For adorner mode, the decorator (decorator) and the decorator (decoratee) all implement the same interface. For proxy mode, both the proxy class and the real class are implemented with the same interface. In addition, regardless of which pattern we use, it is easy to add a custom method before or after the method of the real object.

In fact, however, there are still a lot of differences between adorner mode and proxy mode. The adorner pattern focuses on dynamically adding methods on an object, whereas the proxy mode focuses on controlling access to objects. In other words, with proxy mode, the proxy class can hide specific information about an object from its clients. Therefore, when using proxy mode, we often create an instance of an object in a proxy class. Also, when we use adorner mode, it is common practice to pass the original object as a parameter to the decorator's constructor.

We can summarize these differences in another sentence: Using proxy mode, the relationship between proxies and real objects is usually determined at compile time, and decorators can be constructed recursively at run time.

Proxy mode:

Proxy mode
public class Proxy implements subject{

Private Subject Subject;
Public Proxy () {
Relationships are determined at compile time
Subject = new Realsubject ();
}
public void DoAction () {
....
Subject.doaction ();
....
}
}
Agent's customer
public class client{
public static void Main (string[] args) {
The customer does not know that the agent has delegated another object
Subject Subject = new Proxy ();
...
}
}

Decoration Mode:

Adorner mode
public class Decorator implements component{
Private Component Component;
Public Decorator (Component Component) {
This.component = Component
}
public void operation () {
....
Component.operation ();
....
}
}
Adorners for customers
public class client{
public static void Main (string[] args) {
The customer specifies which class the decorator needs to decorate.
Component Component = new Decorator (new Concretecomponent ());
...
}
}
This article turns from http://www.cnblogs.com/onlywujun/archive/2013/03/28/2985887.html thanks to the author

The difference between Java proxy mode and decorator 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.