当前位置:网站首页>多线程——静态代理模式
多线程——静态代理模式
2022-07-25 09:27:00 【看小虫子】
静态代理模式的总结
真实对象和代理对象都要实现同一接口
代理对象要代理真实角色
好处:1,代理对象可以左很多真实对象做不了的事情
2,真实对象专注做自己的事情
通过runnable接口启动线程是用了静态代理的方法
package StaticProxy;
public class Demo01 {
public static void main(String[] args) {
You you=new You();
you.HappyMarry();
WeddingCompany we=new WeddingCompany(you);
we.HappyMarry();
}
}
interface Marry{
void HappyMarry();
}
//真实角色
class You implements Marry{
@Override
public void HappyMarry() {
System.out.println("开心的,超开心");
}
}
//代理角色
class WeddingCompany implements Marry{
//代理谁--》真实目标角色
private Marry target;
public WeddingCompany(Marry target) {
this.target = target;
}
@Override
public void HappyMarry() {
before();
this.target.HappyMarry();//这是真实对象
after();
}
private void after() {
System.out.println("结婚之后,收尾款");
}
private void before() {
System.out.println("结婚前布置现场");
}
}
在这里,婚庆公司相当于(Thread)
you相当于要调用线程的对象
调用的方法以及run()就是最后实现的方法
边栏推荐
猜你喜欢

小程序分享功能

nodejs版本升级或切换的常用方式

CCF 201509-4 Expressway

链表相关(设计链表及环链表问题)

Common methods of nodejs version upgrade or switching

数据库MySQL详解

DHCP的配置(以华为eNSP为例)

Copy the old project into a web project

TensorFlow raw_ RNN - implement the seq2seq mode to take the output of the previous time as the input of the next time

GCD详解
随机推荐
腾讯云之错误[100007] this env is not enable anonymous login
Record some JS tool functions
微信小程序跳转其他小程序
贪吃蛇小游戏
JSONObject解析json格式的终极总结
Nodejs initial experience
数论--负进制转换
静态路由的配置(以华为eNSP为例)
mysql历史数据补充新数据
Ubuntu20.04系统下安装MySQL数据库5.7.29版本
复现 SSL_Anti-spoofing, 使用 wav2vec 2.0 和数据增强的自动说话人认证的欺骗攻击与深度伪造检测
记录一些JS工具函数
The way of code neatness -- hit the pain point directly
[tensorflow2 installation] tensorflow2.3-cpu installation pit avoidance guide!!!
几个常用的网络诊断命令
一文学会,三款黑客必备的抓包工具教学
Round to the nearest
安装 oh my zsh
Pnpm Brief
NPM详解