Static proxy of design mode proxy mode

Source: Internet
Author: User

Static proxy to figure out three relationships: Client class proxy class delegate class

The behavior of the proxy class is similar and stronger than the delegate class (the implementation method has 1. Inheritance, 2. The proxy class implements a common interface with the delegate Class (preferred))

/**
* Complete log operation with static proxy mode
* @author Administrator
*
*/
public class Useraction {
Private Userdao Userdao = Userdaofac.newuserdao ();
public void Add () {
System.out.println ("========useraction========");
Userdao.add ();
}
public static void Main (string[] args) {
New Useraction (). Add ();
}
}

Public interface Userdao {//interface
public void Add ();
}

public class Userdaoimpl implements userdao{delegate class
@Override
public void Add () {
System.out.println ("=====userdao======");
SYSTEM.OUT.PRINTLN ("Completion log operation!!!"); /Intrusive Implantable code
}
}

public class Userdaofac {//Factory
public static Userdao Newuserdao () {
return new Userpowerproxy (new Userlogproxy (New Userdaoimpl ()));
}
}

public class Userlogproxy implements Userdao {//proxy class
Private Userdao Userdao;
Construction injection
Public Userlogproxy (Userdao Userdao) {
Super ();
This.userdao = Userdao;
}
@Override
public void Add () {
Userdao.add ();//The work of the entrusted class.
SYSTEM.OUT.PRINTLN ("added log operations");
}
}

public class Userpowerproxy implements Userdao {//proxy class
Private Userdao Userdao;
Construction injection
Public Userpowerproxy (Userdao Userdao) {
Super ();
This.userdao = Userdao;
}
@Override
public void Add () {
System.out.println ("Add a permission to judge");
Userdao.add ();
}
}

Static proxy of design mode proxy mode

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.