Java Static proxy parsing

Source: Internet
Author: User

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

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.