Proxy Mode vs Decorating mode

Source: Internet
Author: User

There is a great similarity between the proxy mode and the adornment mode, and the class diagram (almost) is the same. The following sections explain the proxy mode and the decoration mode respectively.

1. Agent mode

Generally famous runners will have their own agents, if you want to contact the athlete's competition, you can contact his agent directly. The class diagram looks like this:

The Irunner interface is as follows:

 Public Interface irunner {    publicvoid  run ();}

The runner class is as follows:

 Public class Implements irunner {    @Override    publicvoid  run () {        System.out.println ("athlete is running ...");    }}

The Runneragent proxy classes are as follows:

 Public classRunneragentImplementsIrunner {PrivateIrunner Runner;  PublicRunneragent (Irunner runner) { This. Runner =runner; } @Override Public voidrun () {Random rand=NewRandom (); if(Rand.nextboolean ()) {System.out.println ("Agent arranges athletes to run ...");        Runner.run (); }        Else{System.out.println ("Agents have things, do not arrange athletes to run ..."); }    }}

The test scenario is as follows:

 Public class Main {    publicstaticvoid  main (string[] args) {        new Runner ();         New runneragent (runner);                System.out.println ("someone asked the agent to let the athlete run ...");        Agent.run ();    }}

The output is:

Or

2. Decoration mode

In the case of proxy mode, how do you use decorative mode? The decoration mode is to strengthen the function of the class, such as increasing running speed, installing a power unit, etc. The class diagram looks like this:

is not the same as the proxy mode of the class diagram, in fact, the same, but the implementation of the intention is the same, first look at the code:

 Public classRunneragentImplementsIrunner {PrivateIrunner Runner;  PublicRunneragent (Irunner runner) { This. Runner =runner; } @Override Public voidrun () {Random rand=NewRandom (); if(Rand.nextboolean ()) {System.out.println ("Agent arranges athletes to run ...");        Runner.run (); }        Else{System.out.println ("Agents have things, do not arrange athletes to run ..."); }    }}

The test scenario is as follows:

 Public class Main {    publicstaticvoid  main (string[] args) {        new Runner ();         New Runnerwithjet (runner);                System.out.println ("Someone is asking for an enhanced version of the athlete to run ...");        Superrunner.run ();    }}

The output results are as follows:

3, the comparison between the two

As you can see from the example above, the proxy pattern is very similar to the decorative pattern, even the code is similar. The main difference between the two is: in proxy mode, the proxy class has control over the object being proxied and decides whether to execute it or not. In the decoration mode, the decoration class has no control over the proxy object, but only adds a layer of decoration to enhance the function of the decorated object.

The use of proxy mode to extreme development is AOP, which is the technology that you must use to develop the spring architecture (the first AOP program for Spring Learning), which is the use of proxy and reflection technology. Agent mode in the development of Java abound, is very familiar with the pattern, the application is very extensive, and the decoration mode is a more formal mode, in the actual application of less contact, but there are many framework projects using decorative mode, for example, in the JDK java.io.* package in a large number of use of decorative mode, Code similar to the following:

New New FileOutputStream ("Test.txt"))

This is a typical application of decorative mode, using DataOutputStream to encapsulate a fileoutputstream to facilitate output stream processing.

Resources:

1, "Zen of design Mode", the model of the large PK section

2 . The similarities and differences between agency mode and decoration mode

Proxy Mode vs Decorating 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.