Talking about static agent of-java design mode

Source: Internet
Author: User

Proxy pattern: When an object does not want or cannot directly reference another object, the proxy object can act as an intermediary between the client and the target object. Static proxy is specific to implement the need for proxy object interface, overwrite the method when personalized operation.

A static proxy is the implementation class for an interface that is implemented separately for the agent.

Not much to say, on 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 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 ();

}

}

Summarize:

Static agent has a disadvantage, that is, when the method in the interface changes (new, the deletion of the necessary implementation method), all the proxy class must make the corresponding changes (assuming there are multiple proxy class case), undoubtedly increase the code complexity, reusability is low, this time can have dynamic agent.

Talking about static agent of-java design 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.