Java design mode-proxy mode static proxy

Source: Internet
Author: User

Java design mode-proxy mode static proxy

Concept

Provide an alias or placeholder for another object to provide access to the object, use proxy mode to create a representative object, allow access to an object on behalf of the object, be a remote object, create an expensive object, or an object that requires security control

  • Remote agent control access to remote objects
  • Virtual Agent control access to create expensive resources
  • Protection agents control access to resources based on permissions

    Look at the following class diagram:

    Take a closer look at the class diagram above, first of all subject it provides the interface for Realsubject and proxy, by implementing the same interface, proxy in the place where the realsubject appears to replace it, this and adapter mode is a big difference.

    Realsubject is really the object of doing things, it is the proxy agent control access to the object, Proxy holds realsubject reference, in some cases proxy will also be responsible for realsubject object creation and destruction. Both the client and Realsubject must pass proxy. Because proxy and realsubject implement the same interface subject, so any use of Realsubject place, you can use proxy instead. Proxy also controls access to Realsubject, and in some cases we may need: rights protection, remote access, create overhead control.

    Proxy Mode Classification

    The proxy mode is divided into

  • Static Proxy-The source code is automatically generated by the programmer or a specific tool to compile it again. The. class file for the proxy class already exists before the program runs. The class diagram above is a good indication of this relationship,

  • Dynamic proxies-Dynamically created using the reflection mechanism while the program is running. Dynamic proxies add Invocationhandler to the agent Proxysubject and Realsubject, which is a communication indirection that increases spirituality and will introduce dynamic proxies in the next article

    An example of a static proxy

    Crime everyone must be unfamiliar, some rich people look at that does not pleasing to the eye, want to find gangs help kill, gangs help them do some bad things. The boss here becomes the Realsubject, the mob becomes the (proxy), here the real and Proxy is only for the killing is who the order (that is, the behind is that)

      • First define a common interface, so that the realsubject appear in the local proxy can appear
    package ProxyMode;/* * 抽象接口,对应类图中的Subject *  */public interface Subject {    public void SujectShow();}
      • Then define a realsubject, the real behind the scenes.
     PackageProxymode; Public  class realsubject implements Subject{    @Override     Public voidSujectshow () {//TODO auto-generated method stubSystem.out.println ("I'm the one who ordered the killings, I'm behind the scenes!"    by---"+getclass ()); }}
      • Then define a proxy class, gangster, get the money, but not the guy behind it.
     PackageProxymode;ImportProxy. Realesubject; Public  class proxysubject implements Subject{    PrivateSubject Realsubject;//proxy classHave a quote from the boss.    Public Subject Takecall ()//Contact {return new Realsubject () by phone; } public void before () {System.out.println ("I am just a proxy class, before I do things, I declare that the next thing is not my concern, I am only being instructed!"    by---"+getclass ()); } public void After () {System.out.println ("as it has not happened before, I am just a passer-by, I am not involved in the above-mentioned things, I was instructed by people!")    by---"+getclass ());  } @Override public void Sujectshow () {//TODO auto-generated method Stub Object o=takecall ();            The proxy class received a call if (checked (o))//Check whether the phone is the boss of the call {before ();            this.realsubject= (Subject) o;            Realsubject.sujectshow ();        After (); } else {System.out.println ("Sorry, you don't have enough authority, I can't help you!")        "); }}} Boolean checked (Object o)/permission check, which is not the year that anyone can impersonate the boss's {if (o instanceof realsubject) return Tru        E    return false; }}
      • Test
    package  Proxymode; public  class  proxytest  { public  static  void  Main (string[] args) {proxysubject proxy=new< /span> Proxysubject (); Proxy. Sujectshow (); }}

    Execution Result:

    我只是一个代理类,在做事情之前我先声明,接下来的事情跟我无关,我只是受人指使!By---class ProxyMode.ProxySubject杀人是我指使的,我是幕后黑手!By---class ProxyMode.RealSubject正如事情还没有发生之前讲的一样,我只是个路人,上面做的事情跟我无关,我是受人指使的! By---class ProxyMode.ProxySubject
  • Java design mode-proxy mode static proxy

    Related Article

    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.