Design mode 11--proxy mode

Source: Internet
Author: User

Design Pattern Directory:

Design pattern-The observer pattern

Design Pattern 2--Decorator mode

Design pattern 3--iterators and Composite modes (iterators)

Design pattern 4--iterators and combined patterns (combination)

Design mode 5--Factory mode

Design mode 6--Single-piece mode

Design mode 7--Command mode

Design mode 8--adapter and appearance mode

Design pattern 9--Template Method mode

Design mode 10--State mode

Design mode 11--proxy mode

Overview

Basic concept of an agent model

Two

Reference

Basic concept of an agent model

The proxy mode provides an alias or placeholder for another object to control access to the object.

The proxy mode is used to create a delegate that allows the delegate object to control access to certain objects, which can be objects that are remote, create expensive objects, or objects that require security control.

Several agent-controlled access methods:

    • Remote Agent Control access to remote objects (see the previous blog Java RMI instance)
    • Virtual Agent Control access to create expensive resources
    • Protection Agents Control access to resources based on permissions

Remote Agent

    • Subject, for Realsubject and proxy interface, by implementing the same interface, proxy in the place where Realsubject appears to replace it
    • Realsubject is the real object, it is the proxy and control access to the object.
    • Proxy holds the Realsubject reference. In some cases, proxy will also be responsible for the creation and destruction of Realsubject objects. Both the client and the Realsubject must pass the proxy. Because proxy and realsbuject implement the same interface (Subject), So any place that uses realsubject can be replaced by proxy. Proxy also controls access to Realsubject, and in some cases we may need such control. These conditions include Realsubject is a remote object, Realsubject creation overhead, or realsubject need to be protected.
Source

Abstract Object Role

 Package Cn.telling.rmi; /**  *   @author  */publicabstractclass  abstractobject    {//  Operation Public    abstractvoid  operation ();}

Target object Role

 PackageCn.telling.rmi;/*** * @ClassName: realobject TODO *@authorXingle * @date 2015-9-29 a.m. 10:52:49*/ Public classRealobjectextendsAbstractobject {/*** * @Description: TODO *@authorXingle * @data 2015-9-29 a.m. 10:53:05*/@Override Public voidoperation () {//Some operationsSYSTEM.OUT.PRINTLN ("Some operations"); }}

Proxy Object Role

 PackageCn.telling.rmi;/*** * @ClassName: Proxyobject * TODO *@authorXingle * @date 2015-9-29 a.m. 10:54:31*/ Public classProxyobjectextendsabstractobject{realobject Realobject=NewRealobject (); /*** * @Description: TODO *@authorXingle * @data 2015-9-29 a.m. 10:54:43*/@Override Public voidoperation () {SYSTEM.OUT.PRINTLN ("Invoke target object before operation");        Realobject.operation (); System.out.println ("Invoke target object after operation"); }}

Client

 Package Cn.telling.rmi; /**  *   @author  */Publicclass  Client        {publicstatic void   Main (string[] args) {        new  proxyobject ();        Obj.operation ();    }}

Execution Result:

Virtual agent of two-agent mode

Reference:

    1. "Design pattern" Learning Note 15: Proxy pattern

    2. "Design pattern" Learning Note 16: Virtual proxy for Proxy mode (implementing CD cover Loader)

    3. The agent model of Java and pattern

Design mode 11--proxy 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.