Static proxy for Java

Source: Internet
Author: User

To solve this problem: in a number of modules to insert a function, for example, in different business processing modules, you need to check whether the user is logged in, assuming that the agent is not used, each add a module, you need to add a lot of code.

For example, in addition to inferring whether or not to log in, assuming that you need to add a logging function, then each module will need to add a function again.

Here we take advantage of static proxy capabilities to address this requirement.

Code structure



function function Interface

Package Itbuluoge.proxy;public interface Icompent {public void Bussiness1 ();p ublic void Bussiness2 ();p ublic void BUSSINESS3 ();}

proxy class

Package Itbuluoge.proxy;public class Proxycompent implements Icompent{private icompent ic;public proxycompent (ICompent IC) {this.ic=ic;} public void ValidateUser () {System.out.println ("Authenticate user ..."); public void Bussiness1 () {//TODO auto-generated method Stubvalidateuser (); Ic.bussiness1 ();} public void Bussiness2 () {//TODO auto-generated method Stubvalidateuser (); Ic.bussiness2 ();} public void Bussiness3 () {//TODO auto-generated method Stubvalidateuser (); Ic.bussiness3 ();}}

By proxy class

Package Itbuluoge.proxy;public class Compent implements Icompent{public void Bussiness1 () {//TODO auto-generated method s TUBSYSTEM.OUT.PRINTLN ("Business 1");} public void Bussiness2 () {//TODO auto-generated method StubSystem.out.println ("Business 2"); public void Bussiness3 () {//TODO auto-generated method StubSystem.out.println ("Business 3");}

Test class

Package Itbuluoge.proxy;public class Test {/** * @param args */public static void main (string[] args) {//TODO Auto-genera Ted Method Stubproxycompent myproxy=new proxycompent (New Compent ()); Myproxy.bussiness1 (); Myproxy.bussiness2 (); MYPROXY.BUSSINESS3 ();}}

Output results



We can see that if we need to add a common function in very many, we just need to add it to the proxy class. And it can be seen that the proxy class only has its own business logic, no matter what service, service is defined in the proxy class itself.



Static proxy for 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.