Static proxies in Java

Source: Internet
Author: User

Discover the meta-classes in Python and dynamic proxies in Java like, going to write two essays summarizing the proxy mechanism in Java

Proxy mode is simply to enhance the functionality of a class, in the actual development, most of the time we have to maintain the old code or decoupling reasons, can not change the original class, this time the proxy class comes in handy

So in the face of the design of the object, to enhance a class in addition to inheritance, we can also create a new proxy class to implement the interface of the proxy class, in the proxy class call the method of the proxy class, see the graph may be more intuitive

Directly below the code

/**Interface Definition*/ Public InterfaceTargetinterface {voidsay (); voideat ();}/**by proxy class*/ Public classProxytargetImplementsTargetinterface { Public voidsay () {System.out.println ( This); } @Override Public voideat () {System.out.println ("I am a proxy object eat"); } @Override PublicString toString () {return"I am the class to be represented."; }     Public Static voidMain (string[] args) {NewProxytarget (). Say (); }}/*** proxy class*/
public class Testproxy implements Targetinterface {

Private Targetinterface Targetinterface;

Public Testproxy (Targetinterface targetinterface) {
This.targetinterface = Targetinterface;
}

public void Say () {
Targetinterface.say ();
System.out.println ("I have become stronger");
}

@Override
public void Eat () {
Targetinterface.say ();
System.out.println ("I have become stronger");
}

public static void Main (string[] args) {//test code


Targetinterface Interfa = new Proxytarget ();
Testproxy testproxy = new Testproxy (INTERFA);
Testproxy.eat ();
}
}

Output

I'm going to be a proxy class, and I'm stronger.

So in the original class unchanged, we enhanced the original class, but static agent has a few drawbacks, if we want to achieve more analogy, each interface to write a proxy class, increase the burden, in order to solve this problem, the introduction of dynamic agent mechanism, dynamic generation agent class, the next essay introduction

Static proxies in Java

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.