本篇学习的是多线程的静态代理,以结婚为举例,婚庆公司为代理角色,自己为真实角色。
package lesson04;
public class StaticProxy {
public static void main(String[] args) {
new WeddingCompony(new You()).happyMarry();
new Thread(()-> System.out.println("111")).start();
}
}
interface Marry {
void happyMarry ();
}
//真角色:你自己结婚
class You implements Marry {
@Override
public void happyMarry() {
System.out.println("我结婚了,好高兴");
}
}
//代理角色:帮助你结婚
//代理角色可有做你做不了的事情,而你自己只需要做自己的事情即可
class WeddingCompony implements Marry {
//代理真实目标
private Marry target;
public WeddingCompony(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("结婚前布置婚礼");
}
}