The main feature of the Java static Proxy is that the proxy class and the delegate class implement the same interface. When invoking an interface method, an instance factory object is provided externally.
advantages and disadvantages of static proxy classes
Advantages: The business class only needs to focus on the business logic itself, ensuring the reusability of the business class. This is the common advantage of the agent.
Disadvantages:
1) An interface of the proxy object only serves one type of object, if there are many methods to proxy, it is bound to proxy for each method, static agent is not competent when the program size is slightly larger.
2) If the interface adds a method, all the proxy classes need to implement this method in addition to implementing the method for all implementation classes. Increases the complexity of code maintenance.
1 Public Interface Impluserdao {2 Public void Add (); // define Add interface 3 Public void Delete (); // defining the Delete interface 4 }
Impluserdao Interface
1 Public classUserdaoimplImplementsimpluserdao{2 3 /*(Non-javadoc)4 * @see Cn.org.fupin.impluserdao#add ()5 */6 Public voidAdd () {7 //TODO auto-generated Method Stub8System.out.println ("Userdaoimpl.add ()");9 }Ten One /*(Non-javadoc) A * @see cn.org.fupin.impluserdao#delete () - */ - Public voidDelete () { the //TODO auto-generated Method Stub -System.out.println ("Userdaoimpl.delete ()"); - } - + -}Userdaoimpl Delegate Class
1 Public classUserdaoproxyImplementsimpluserdao{2 3 PrivateImpluserdao Impluserdao;4 5 Publicuserdaoproxy (Impluserdao Impluserdao) {6 7 This. Impluserdao =Impluserdao;8 }9 Ten Public voidAdd () { One //TODO auto-generated Method Stub A Impluserdao.add (); - } - the /*(Non-javadoc) - * @see cn.org.fupin.impluserdao#delete () - */ - Public voidDelete () { + //TODO auto-generated Method Stub - Impluserdao.delete (); + } A at}Userdaoproxy proxy class
1 Public class userdaofactory {2 3 4 Public Static Impluserdao getuserinstance () {5 return New Userdaoproxy (new Userdaoimpl ()); 6 }7 8 }
Userdaofactory Factory class
1 Public class Client {2 Public Static void Main (string[] args) {3 Impluserdao Impluserdao = userdaofactory.getuserinstance (); 4 Impluserdao.add (); 5 impluserdao.delete (); 6 }7 }
Client Test class
Java static proxy parsing