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