Discussion-static proxy in Java design mode; discussion-java Design Mode

Source: Internet
Author: User

Discussion-static proxy in Java design mode; discussion-java Design Mode

Proxy pattern: when an object does not want or cannot directly reference another object, the Proxy object can play a mediation role between the client and the target object. Static proxy is the proxy object interface required for specific implementation. It performs personalized operations when overwriting methods.

Static Proxy is used to implement a class of an interface for Proxy ).

Let's just move on to the code.

Package com. ant;

Public interface Hello {

Public void say ();

}

Package com. ant;

Public class HelloImpl implements Hello {

@ Override

Public void say (){

System. out. println ("hellw world! ");

}

}

Package com. ant;

 

Public class StaticProxy implements Hello {

Private Hello;

@ Override

Public void say (){

System. out. println ("staticProxy welcome ");

Hello. say ();

}

Public StaticProxy (Hello hello ){

This. hello = hello;

}

}

Package com. ant;

 

Public class StaticProxyTest {

Public static void main (String [] args ){

Hello hello = new HelloImpl ();

StaticProxy staticProxy = new StaticProxy (hello );

StaticProxy. say ();

}

}

Summary:

One drawback of static proxy is that when methods in the interface are changed (when necessary implementation methods are added or deleted ), all proxy classes must be changed accordingly (assuming there are multiple proxy classes). This will undoubtedly increase Code complexity and reduce reusability. In this case, dynamic proxies can be created.

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.