Because now only began to learn Java, so now only learn the static proxy mode, the latter will continue to update the dynamic design mode to learn to communicate.
The following is my understanding of the static proxy mode. For reference only, I hope to be the same as I study people have help, if there are mistakes, welcome to correct. Lest I mislead others.
InterfaceHousebuilder {//first, the business interface is defined, and the interface is used for proxy and implementation. There is also the user's use. Public voidBuildhouse ();//provides an abstract business method.}//a specific class (a class that really has such a function) to implement a business interfaceclassWankehousebuilderImplementsHousebuilder {@Override Public voidBuildhouse () {System.out.println ("My house builders, I can help you build your house."); }}//The proxy class also implements the business interface, which is simply an indirect invocation of the proxy class object to complete the event. Public classStaticproxyImplementsHousebuilder {//as an agent, it is also necessary to provide such a function, in fact, only through him to invoke the person who really has such a function to complete the matter. //The object being represented by the class Privatehousebuilder Target; PublicStaticproxy () {target=NewWankehousebuilder ();//Create the object of the specific class (the person who really has the business function)} @Override Public voidBuildhouse () {System.out.println ("Agent start ..."); Target.buildhouse ();//indirect to invoke the method of the specific class System.out.println ("Agent End ..."); } }classProxytest { Public Static voidMain (string[] args) {housebuilder HB=NewStaticproxy (); //to the user, the user can see only the agent, so here we create proxy objects. //and consider it as an object that can implement this business, assign the address of the object to the reference of the business interface, and realize polymorphism. //proxy objects are masked by proxy objects, and we directly access the proxy through the proxy to access a//some work.Hb.buildhouse ();//because the proxy class implements (overrides) the abstract method of the interface. So you can call it directly (a virtual method call). }}
Java Proxy design pattern