Design pattern Learning (vi) proxy mode

Source: Internet
Author: User

Core role:

--Control access to objects through proxies

You can control access to a (certain Class) object in detail, before calling this method to do pre-processing, call this method after the processing (that is, the micro implementation of AOP!) )

--the core implementation mechanism of AOP (Aspect oriented programming aspect-oriented programming)!

Core roles:
    • Abstract roles:

--Public external methods for defining proxy roles and real roles

    • Real characters

--Implement abstract roles, define the business logic to be implemented by the real role, for proxy role invocation

-Focus on the real business logic

    • Agent role

--the implementation of abstract role, is the real role of the agent, through the real role of the business logic method to implement the abstract method, and can attach their own operations

--Put unified process control into the agent role for processing

Application Scenarios:

Classification and implementation:

--static proxy (statically defined proxy class)

 PackageCom.lp.staticProxy; Public InterfaceStar {/*Interview*/    voidconfer (); /*sign a contract*/    voidsingcontract (); /*Order*/    voidBookticket (); /*singing*/    voidSing (); /*Collect Money*/    voidCollectmoney (); }
Interface
 PackageCom.lp.staticProxy; Public classRealstarImplementsStar {@Override Public voidconfer () {System.out.println ("Realstar Confer"); } @Override Public voidsingcontract () {System.out.println ("Realstar singcontract"); } @Override Public voidBookticket () {System.out.println ("Realstar Bookticket"); } @Override Public voidSing () {System.out.println ("Realstar (Jay Chou) sing"); } @Override Public voidCollectmoney () {System.out.println ("Realstar Collectmoney"); }}
Real characters
 PackageCom.lp.staticProxy; Public classProxystarImplementsStar {PrivateStar Star;  PublicProxystar (Star Star) {Super();  This. Star =Star; } @Override Public voidconfer () {System.out.println ("Proxystar Confer"); } @Override Public voidsingcontract () {System.out.println ("Proxystar singcontract"); } @Override Public voidBookticket () {System.out.println ("Proxystar Bookticket"); } @Override Public voidSing () { This. star.sing (); } @Override Public voidCollectmoney () {System.out.println ("Proxystar Collectmoney"); }}
Agent Role
 Package Com.lp.staticProxy;  Public class clinet {    publicstaticvoid  main (string[] arg) {        new  Realstar ();         New Proxystar (real);                Proxy.confer ();        Proxy.singcontract ();        Proxy.bookticket ();        Proxy.sing ();        Proxy.collectmoney ();    }}
ClientResults:

--Dynamic proxy (dynamically generated proxy class)

Advantages:

--All of the methods in the abstract role (interface) Declaration are transferred to the calling processor in a centralized approach, so that we can handle a multitude of methods more flexibly and uniformly

The dynamic agent that comes with the JDK

--Java.lang.reflect.Proxy

Role: Dynamically generate proxy classes and objects

--Java.lang.reflect.InvocationHandler (processor interface)

Role: 1: Proxy access to real roles can be implemented by the Invoke method

2: Specify the corresponding Processor object each time the proxy class object is generated through proxy

Code:

 PackageCom.lp.dynamicProxy; Public InterfaceStar {/*Interview*/    voidconfer (); /*sign a contract*/    voidsingcontract (); /*Order*/    voidBookticket (); /*singing*/    voidSing (); /*Collect Money*/    voidCollectmoney (); }
Object Interface
 PackageCom.lp.dynamicProxy; Public classRealstarImplementsStar {@Override Public voidconfer () {System.out.println ("Realstar Confer"); } @Override Public voidsingcontract () {System.out.println ("Realstar singcontract"); } @Override Public voidBookticket () {System.out.println ("Realstar Bookticket"); } @Override Public voidSing () {System.out.println ("Realstar (Jay Chou) sing"); } @Override Public voidCollectmoney () {System.out.println ("Realstar Collectmoney"); }}
Real Objects
 PackageCom.lp.dynamicProxy;ImportJava.lang.reflect.InvocationHandler;ImportJava.lang.reflect.Method; Public classStarhandlerImplementsInvocationhandler {Star Realstar;  PublicStarhandler (Star realstar) {Super();  This. Realstar =Realstar; } @Override Publicobject Invoke (Object proxy, Method method, object[] args)throwsThrowable {Object Object=NULL; if(Method.getname (). Equals ("Sing") {System.out.println (Reservation); System.out.println (Payment);            Method.invoke (Realstar, args); System.out.println ("Listen to the song"); }                return NULL; }}
Agent Control Side
 PackageCom.lp.dynamicProxy;ImportJava.lang.reflect.Proxy; Public classClient { Public Static voidMain (string[] args) {Star Realstar=NewRealstar (); Starhandler Handler=NewStarhandler (Realstar); Star Proxy= (Star) proxy.newproxyinstance (Classloader.getsystemclassloader (),NewClass[]{star.class},handler); Proxy.confer ();//These are not called real characters.Proxy.singcontract ();//These are not called real characters.Proxy.bookticket ();//These are not called real characters.proxy.sing (); Proxy.collectmoney ();//These are not called real characters.    }}
Client

Results:

Design pattern Learning (vi) 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.