package Thread.Demo02;
//静态代理模式总结:真实对象和代理对象都要实现同一接口,代理对象要代理真实角色(通过传真实参数)
public class StaticProxy {
public static void main(String[] args) {
// new Thread(new Runnable() {
// @Override
// public void run() {
//
// }
// }).start();
new Thread(()-> System.out.println("I LOVE YOU")).start();
new WeddingCompany(new You()).HappyMarry();
// You you = new You();
// WeddingCompany weddingCompany = new WeddingCompany(you);
// weddingCompany.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 before(){
System.out.println("结婚之前");
}
private void after(){
System.out.println("结婚之后");
}
}
静态代理模式
最新推荐文章于 2024-08-23 16:54:51 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)