Agent Mode
Brief Introduction
Proxy mode definition: Provides a proxy for the proxy object to control the access rights of the object. For example, we want to find a star in the business activities can not directly contact me, is the need to contact the broker. Proxy mode general class diagram
Getting started case a
For example, I want to find Ying Bao to perform a TV series, Ying Bao peacetime work is very busy, if anything to their own treatment, it will appear very tired. So only the appearance of star brokers, then the broker is actually the agent of the PO. Agent Ying Bao deal with some things, you need to find Ying PO must be through the broker.
So the problem is, brokers always have to know the skills of the PO. Can not casually pick up some activities, Ying Bao will not then be equivalent to trouble. has not shared the pressure of Ying Bao. In the Java code, the broker (agent) and the PO (the agent) need to have a public interface. Code
Package com.rabbit.pattern.proxy;
/**
* Agent and recognized by the agent of the skill class
* Created by the VIP on 2018/3/1.
* * Public
interface Skill {
/**
* Sing
* @param name *
/void Sing (String name);
/**
* Performance
* @param name
/void Perform (String name);
/**
*
@param name
/void variety (String name);
Package com.rabbit.pattern.proxy;
/**
* Ying Bao
* Created by the VIP on 2018/3/1.
*
/public class Yingbao implements Skill {
@Override public
void Sing (String name) {
System.out.println ("Ying Bao sang a song [" + Name +] ");
}
@Override public
Void Perform (String name) {
System.out.println ("Ying Bao starred [+ name +]");
@Override public
void variety (String name) {
System.out.println ("+ name +"] variety show);
}
Package com.rabbit.pattern.proxy;
/**
* Ying Bao agent
* Created by the VIP on 2018/3/1.
* * Public
class Yingbaoproxy implements Skill {
//save by instance of the agent
private Skill YB;
Public Yingbaoproxy (Skill Skill) {
this.yb = Skill;
}
The agent actually let the po do things
@Override public
void Sing (String name) {
yb.sing (name);
}
@Override public
Void Perform (String name) {
yb.perform (name);
}
@Override public
void variety (String name) {
yb.variety (name);
}
}
Package com.rabbit.pattern.proxy;
/**
* Created by the VIP on 2018/3/1
. *
/public class Demo {public
static void Main (string[] args) {
Yingbaoproxy ybp = new Yingbaoproxy (New Yin Gbao ());
Ybp.sing ("Beijing Beijing");
Ybp.perform ("Lu Zhen legend");
Ybp.variety ("Day Up");
}
Agent Mode pros and cons
Advantages
Clear Responsibilities
The proxy object is only responsible for its own actual business logic and does not care about other non-self responsibilities. and other transactions can be handled through the proxy class. High Scalability
Regardless of how the proxy object changes, as long as the proxy class and the proxy class have implemented a unified interface, are different to modify the proxy class, and even if the extension of the new proxy class, proxy class can also be used, as long as the creation of proxy classes in the corresponding proxy class object. Intelligent
This is mainly reflected in the dynamic agent, the following will explain the dynamic agent. If you are interested in learning about spring's AOP, you are using a dynamic proxy. Disadvantage
Unreasonable use of proxy mode may result in slower request processing and lower concurrent volume. Implementing a proxy pattern requires additional work, which can result in resource depletion if complex proxy patterns can degrade system performance. Agent Mode
In the Getting Started case, the proxy class lets the caller specify the object to proxy by using a constructor, which is actually a more extensive invocation, because the proxy class can proxy any class that implements the skill. Specifying proxy classes
Or use the example of getting started case one, but the proxy class specifies the proxy class, and other proxy classes cannot be executed if they are forced to invoke. Train of thought: first by the proxy class gets the specified proxy object to access.
Package com.rabbit.pattern.proxy;
/**
* Agent and recognized by the agent of the skill class
* Created by the VIP on 2018/3/1.
* * Public
interface Skill {
/**
* Sing
* @param name *
/void Sing (String name);
/**
* Performance
* @param name
/void Perform (String name);
/**
*
@param name
/void variety (String name);
Package com.rabbit.pattern.proxy;
/**
* Ying Bao
* Created by the VIP on 2018/3/1.
* * Public
class Yingbao implements Skill {
private yingbaoproxy ybp;//PO designated agent
//Get the specified Agent object
public Skill Getproxt () {
ybp = new Yingbaoproxy (this);
return ybp;
}
@Override public
void Sing (String name) {
if (ybp = = null) {
System.out.println ("Please use the specified proxy class");
} else {
System.out.println ("Ying Bao sang a song [" + Name +] ");
}
@Override public
Void Perform (String name) {
if (ybp = = null) {
System.out.println ("Please use the specified proxy class");
} else {
System.out.println ("Ying Bao starred [" + Name +] ");
}
@Override public
void variety (String name) {
if (ybp = = null) {
System.out.println ("Please use the specified proxy class");
} else {
System.out.println ("po [" + name +] variety show);}}
Package com.rabbit.pattern.proxy;
/**
* Ying Bao agent
* Created by the VIP on 2018/3/1.
* * Public
class Yingbaoproxy implements Skill {
//save by instance of the agent
private Skill YB;
Public Yingbaoproxy (Skill Skill) {
this.yb = Skill;
}
The agent actually let the po do things
@Override public
void Sing (String name) {
yb.sing (name);
}
@Override public
Void Perform (String name) {
yb.perform (name);
}
@Override public
void variety (String name) {
yb.variety (name);
}
}
Package com.rabbit.pattern.proxy;
/**
* Created by the VIP on 2018/3/1
. */Public
class Demo {public
static void Main (string[] args) {
Skill ybp = new Yingbao (). GETPROXT ();
Ybp.sing ("Beijing Beijing");
Ybp.perform ("Lu Zhen legend");
Ybp.variety ("Day Up");
}
Dynamic Proxy
Before introducing a dynamic proxy, we first understand the 2 classes that need to be used.
1) Interface Invocationhandler: Only one method Object:invoke (Object Obj,method method,object[] args) is defined in this interface. In practice, the first parameter obj generally refers to the proxy class, which is the proxy method, args the parameter array of the method. This abstract method is dynamically implemented in the proxy class.
2 Proxy: This class is a dynamic proxy class, which is similar to the proxysubject in the previous example, which mainly contains the following contents:
Protected Proxy (Invocationhandler H): constructor, which is estimated to be used to assign values to the internal H.
Static class Getproxyclass (ClassLoader loader,class[]interfaces): Gets a proxy class where loader is a class loader and interfaces is an array of all the interfaces owned by the real class.
Static Object newproxyinstance (classloaderloader,class[] interfaces,invocationhandler h): Returns an instance of the proxy class, The returned proxy class can be used as a proxy class (you can use the method declared in the subject interface of the proxy class).
Package com.rabbit.pattern.proxy;
/**
* Agent and recognized by the agent of the skill class
* Created by the VIP on 2018/3/1.
* * Public
interface Skill {
/**
* Sing
* @param name *
/void Sing (String name);
/**
* Performance
* @param name
/void Perform (String name);
/**
*
@param name
/void variety (String name);
Package com.rabbit.pattern.proxy;
/**
* Ying Bao
* Created by the VIP on 2018/3/1.
*
/public class Yingbao implements Skill {
@Override public
void Sing (String name) {
System.out.println ("Ying Bao sang a song [" + Name +] ");
}
@Override public
Void Perform (String name) {
System.out.println ("Ying Bao starred [+ name +]");
@Override public
void variety (String name) {
System.out.println ("+ name +"] variety show);
}
Package com.rabbit.pattern.proxy;
Import Java.lang.reflect.InvocationHandler;
Import Java.lang.reflect.Method;
/**
* Scheduling processing class, the dynamic proxy object is created using the
* Created by the VIP on 2018/3/1.
* * Public
class Myinvocationhandler implements Invocationhandler {
private Object obj;//by proxy class instance public
Myinvocationhandler (Object obj) {
this.obj = obj;
}
@Override Public
object Invoke (Object proxy, Method method, object[] args) throws Throwable {
SYSTEM.OUT.PRINTLN ("The public logic to be handled by the proxy object can be written uniformly here");
if (Method.getname (). Equalsignorecase ("Sing")) {
System.out.println ("I'm not in the business of singing, I'm Sorry");
return null;
}
Object invoke = Method.invoke (obj, args);
return invoke;
}
Package com.rabbit.pattern.proxy;
Import Java.lang.reflect.Proxy;
/**
* Created by the VIP on 2018/3/1
. *
/public class Demo {public
static void Main (string[] args) throws Exception {
Skill YB = new Yingbao ();
//Create a Schedule object
myinvocationhandler MiH = new Myinvocationhandler (YB);
The way dynamic proxies create objects
Skill s = (Skill) proxy.newproxyinstance (Yb.getclass (). getClassLoader (), New Class[]{skill.class} , MIH);
S.sing ("small Lucky");
S.perform ("Lu Zhen legend");
S.variety ("Day Up");
}
the benefits of dynamic proxies
You may find that dynamic proxies require additional code during use, such as "Create a Dispatch object" and "create objects by Dynamic Proxy" code, but this can continue to be encapsulated. The advantage of dynamic proxies is that objects are dynamically represented only when they need to be used, where a Yingbaoskill class is missing, and for subclasses that extend the skill class, there is no need to create a new class. decorator mode and agent mode
Decorator mode: http://blog.csdn.net/sinat_32366329/article/details/79392905 decoration Person
1 The decorator mode is mainly to enhance the function of the decorated person class, can be said to be an inherited alternative.
2 hides the specific implementation of the decorated person class, for the call callers says only the decorator class is known. Agent
1 agent mode is mainly to enhance the rights of the proxy class control.
2 Hidden by the proxy class specific implementation, for the call callers said only know proxy class.
The decorator pattern and the agent pattern are implemented in a similar way, but why do the same mechanisms distinguish 2 design patterns? It can be said that the principle of single duty should not be destroyed. Because the specific responsibility of 2 modes is different, if all is called the decorator pattern or all call the proxy mode then the developer can not differentiate the concrete function. It's easier to understand how to distinguish 2 different responsibilities.