Proxy mode,
Agent Mode 1. Related Concepts:
Head First design mode:
Proxy mode provides a replacement or placeholder for another object to control access to this object
Main role: Control Object Access
-Extended Target object functions: for example, actors (target objects) have the acting function. Finding a broker (proxy) will provide additional paid functions, which are actually proxy functions, rather than the actor's functions.
-Restrict the functions of the target object. For example, if the agent is not satisfied with the charges, the agent only plays a play and partially limits the functions of the actor.
Ii. Static Proxy:
The compilation period already exists. Generally, the interface needs to be defined first, and the proxy object implement the same interface together.
1. Interface Definition:
Public interface Play {// singing void sing (int count); // performing void show ();}
2. Actors (proxies ):
Public class Actor implements Play {@ Override public void sing (int count) {System. out. print ("sang" + count + "shouge") ;}@ Override public void show () {System. out. print ("performing performances ");}}
3. Broker (proxy object ):
Public class Agent implements Play {private Play player; private long money; public void setMoney (long money) {this. money = money;}/*** @ param player * @ param money charges */public Agent (Play player, long money) {this. player = player; this. money = money ;}@ Override public void sing (int count) {player. sing (count) ;}@ Override public void show () {if (money> 100) {player. show ();} else {System. out. println ("baibai... ");}}}
4. test:
public class PlayTest { public static void main(String[] args){ Actor actor = new Actor(); Agent agent = new Agent(actor, 50); agent.sing(2); agent.show(); agent.setMoney(200); agent.show(); }}
Result:
The proxy object controls the functions of the target object through its own logical processing.
3. Dynamic Proxy:
Dynamic usually refers to the status at runtime, Which is differentiated from the static state at compilation, that is, generating a proxy object at runtime helps us with some logic processing. It mainly uses the reflection technology to obtain the class loader and create instances.
1. How to generate a dynamic proxy class:
The Proxy class and InvocationHandler interface in the java. lang. reflect package provide the ability to generate dynamic Proxy classes.
(1) Proxy class:
The Proxy class is an operation class dedicated to Proxy. You can use this class to dynamically generate an implementation class for one or more interfaces. This class provides the following operation methods:
public static Object newProxyInstance(ClassLoader loader, Class
[] interfaces, InvocationHandler h) throws IllegalArgumentException{ ... }
Parameter description:
-ClassLoader loader: Class loader
-Class <> [] interfaces: All interfaces
-InvocationHandler h: subclass that implements the InvocationHandler Interface
(2) InvocationHandler interface:
public interface InvocationHandler { public Object invoke(Object proxy, Method method, Object[] args) throws Throwable;}
Parameter description:
-Object proxy: the proxy Object.
-Object [] args: method to be called
-Object [] args: parameters required for method call
2. Dynamic proxy demo:
(1) Dynamic Proxy:
Public class ActorProxy implements InvocationHandler {private Play player; public ActorProxy (Play player) {this. player = player ;}@ Override public Object invoke (Object proxy, Method method, Object [] args) throws Throwable {if ("show ". equals (method. getName () {System. out. println ("agent processing show .... "); return method. invoke (player, null);} else if ("sing ". equals (method. getName () {System. out. println ("proxy processing sing .... "); return method. invoke (player, 2) ;}return null ;}}
The proxy class implements the InvocationHandler interface. In the invoke method, the player (the proxy object) is processed logically.
(2) test:
public class ProxyTest { public static void main(String[] args) { Play actor = new Actor(); Play proxy = (Play) Proxy.newProxyInstance(actor.getClass().getClassLoader() , actor.getClass().getInterfaces(), new ActorProxy(actor)); proxy.show(); proxy.sing(3); }}
You can call Proxy. newProxyInstance to generate a Proxy object.
Result:
A dynamic proxy can dynamically create a class at runtime to implement one or more interfaces. It can dynamically add methods and modify actions for objects obtained through the class without modifying the original class.