当前位置:网站首页>静态代理+动态代理
静态代理+动态代理
2022-07-25 23:32:00 【孙少平】
静态代理:
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();
}
//被代理人
class NikeClothFactory implements ClothFactory{
@Override
public void produceCloth() {
System.out.println("做衣服");
}
}
//代理人
class ProxyClothFactory implements ClothFactory{
private ClothFactory clothFactory;
public ProxyClothFactory(ClothFactory clothFactory){
this.clothFactory=clothFactory;
}
@Override
public void produceCloth() {
System.out.println("准备布料");
clothFactory.produceCloth();
System.out.println("卖衣服");
}
}
interface Star{
void sing();
}
class Tylor implements Star{
@Override
public void sing() {
System.out.println("Tylor is sing....");
}
}
//经纪人:Agent
class ProxyAgent implements Star{
private Star star;
public ProxyAgent(Star star){
this.star=star;
}
@Override
public void sing() {
System.out.println("给Tylor找演出和化妆");
star.sing();
System.out.println("帮Tylor收唱歌的钱,给Tylor叫车");
}
}
动态代理:
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("代理开始");
Object returnVal = method.invoke(object, args);
System.out.println("代理结束");
return returnVal;
}
}
边栏推荐
- WordPress removes the website publishing time
- From which dimensions can we judge the quality of code? How to have the ability to write high-quality code?
- Serialize data type
- Anti shake and throttling
- Classes and objects (2) (6 default member functions)
- Multimodal deep multi modal sets
- 加拿大EE通道
- BI 系统中为什么会有很多快照表?
- Strategy mode_
- Docker installation redis-5.0.12 (remote access)
猜你喜欢
![[nodejs] nodejs create a simple server](/img/00/7ab5629bf67777da21b41f44665880.png)
[nodejs] nodejs create a simple server

E-commerce RPA, a magic weapon to promote easy entry

Idea sets get and set templates to solve the naming problem of boolean type fields

数组中重复的数字

SAP Message No. VG202 IDoc E1EDK18 中付款条款已经转移:检查数据

意向不到的Dubug妙招

【MUDUO】EventLoop事件循环

XxE & XML external entity injection utilization and bypass

Cuteone: a onedrive multi network disk mounting program / with member / synchronization and other functions

多模态——Deep Multi-Modal Sets
随机推荐
Apple CMS V10 template /mxone Pro adaptive film and television website template
[Muduo] EventLoop event cycle
【代码案例】博客页面设计(附完整源码)
在应用中使用 Jetpack 库
Duplicate numbers in array
[nodejs] nodejs create a simple server
MES系统设备管理概述(下)
Rendering, filtering (filtering) and sorting of lists
Using jetpack libraries in applications
Source code of YY music wechat applet imitating Netease cloud music
[JUC] concurrent keyword volatile
【MUDUO】打包EventLoop和Thread
Anti shake and throttling
Learning exploration-3d rotation card
PyTorch的数据输入格式要求及转换
Data broker understanding
Take away applet with main version of traffic / repair to add main access function of traffic
762. Prime number calculation setting in binary representation
About priority queues
日期类的实现