当前位置:网站首页>Static agent + dynamic agent
Static agent + dynamic agent
2022-07-25 23:34:00 【Sun Shaoping】
Static proxy :
package com.god.se.reflection.proxy;
public class StaticProxyTest {
public static void main(String[] args) {
ClothFactory nikeClothFactory = new NikeClothFactory();
ClothFactory proxyClothFactory = new ProxyClothFactory(nikeClothFactory);
proxyClothFactory.produceCloth();
Star tylor = new Tylor();
Star proxyAgent = new ProxyAgent(tylor);
proxyAgent.sing();
}
}
interface ClothFactory{
void produceCloth();
}
// The principal
class NikeClothFactory implements ClothFactory{
@Override
public void produceCloth() {
System.out.println(" Make clothes ");
}
}
// The agent
class ProxyClothFactory implements ClothFactory{
private ClothFactory clothFactory;
public ProxyClothFactory(ClothFactory clothFactory){
this.clothFactory=clothFactory;
}
@Override
public void produceCloth() {
System.out.println(" Prepare the cloth ");
clothFactory.produceCloth();
System.out.println(" Sell clothes ");
}
}
interface Star{
void sing();
}
class Tylor implements Star{
@Override
public void sing() {
System.out.println("Tylor is sing....");
}
}
// agent :Agent
class ProxyAgent implements Star{
private Star star;
public ProxyAgent(Star star){
this.star=star;
}
@Override
public void sing() {
System.out.println(" to Tylor Look for performances and makeup ");
star.sing();
System.out.println(" help Tylor Charge money for singing , to Tylor Call a car ");
}
}
A dynamic proxy :
package com.god.se.reflection.proxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class DynamicProxyTest {
public static void main(String[] args) {
HuMan superMan = new SuperMan();
HuMan proxy = (HuMan) ProxyFactory.getProxy(superMan);
proxy.eat();
proxy.getBelief();
Tylor tylor = new Tylor();
Star proxy1 = (Star) ProxyFactory.getProxy(tylor);
proxy1.sing();
}
}
interface HuMan{
void eat();
void getBelief();
}
class SuperMan implements HuMan{
@Override
public void eat() {
System.out.println("superman is eat");
}
@Override
public void getBelief() {
System.out.println("superman's belief is fly ");
}
}
class ProxyFactory{
public static Object getProxy(Object object) {
MyInvocationHandler handler = new MyInvocationHandler();
handler.bind(object);
Object proxyInstance = Proxy.newProxyInstance(object.getClass().getClassLoader(), object.getClass().getInterfaces(), handler);
return proxyInstance;
}
}
class MyInvocationHandler implements InvocationHandler {
private Object object;
public void bind(Object obj){
this.object=obj;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println(" Agent start ");
Object returnVal = method.invoke(object, args);
System.out.println(" End of agency ");
return returnVal;
}
}
边栏推荐
- 类和对象(2)(6个默认成员函数)
- POI特效 市场调研
- [QNX hypervisor 2.2 user manual]9.6 GDB
- Wrote a little webapi knowledge points from 0 to 1
- About priority queues
- Learning exploration-3d rotation card
- Apple CMS V10 template /mxone Pro adaptive film and television website template
- 谷粒学苑P98踩坑 e.GlobalExceptionHandler : null
- MES系统设备管理概述(下)
- About the foundation of fetch
猜你喜欢

S4/HANA ME21N创建PO 输出控制消息按钮丢失解决方法(切换EDI 输出模式BRF+至NAST模式)

LeetCode 0919. 完全二叉树插入器:完全二叉树的数组表示
![[Muduo] EventLoop event cycle](/img/80/824c7061d58796d454be0c438e257c.png)
[Muduo] EventLoop event cycle

Classes and objects (3)

Grain Academy p98 trample pit e.globalexceptionhandler: null

意向不到的Dubug妙招

ABAP 代码中读取会计科目的字段状态(隐藏、可选、必输)

Family relationship calculator wechat applet source code

Numerical learning iota, accumulate

图的遍历-DFS,BFS(代码详解)
随机推荐
反射之类加载过程
Function definition and call
S4/HANA MM & SD EDI基于NAST的集成配置(ORDERS, ORDRSP, DESADV, INVOIC)
动态内存管理
POI特效 市场调研
Constructors and prototypes
模拟实现string类常用接口
图的遍历-DFS,BFS(代码详解)
[QNX Hypervisor 2.2用户手册]9.8 load
Three board axe! Help you become an excellent software engineer
Anti shake and throttling
[Database Foundation] summary of MySQL Foundation
物理防火墙是什么?有什么作用?
Recursion of function (use recursion to find the factorial of 1-N) (use recursion to find Fibonacci sequence) (use recursion to traverse data)
EasyExcel实用技巧
Es5 new method
推荐系统——An Embedding Learning Framework for Numerical Features in CTR Prediction
ETL tool (data synchronization) II
LeetCode 0919. 完全二叉树插入器:完全二叉树的数组表示
日期类的实现