多線程靜態(tài)代理模式的代碼
/*
?* 靜態(tài)代理
?* 1.真實(shí)角色2.代理角色都需要實(shí)現(xiàn)同一個(gè)接口
?*/
public class StaticProxy {
?? ?public static void main(String[] args) {
?? ??? ?new WeddingCompany(new You()).happyMarry();
?? ?}
}
interface Marry{
?? ?void happyMarry();
}
//真實(shí)角色
class You implements Marry{
?? ?@Override
?? ?public void happyMarry() {
?? ??? ?System.out.println("新郎和新年結(jié)婚了");
?? ??? ?
?? ?}
?? ?
}
class WeddingCompany implements Marry {
?? ?//代理角色
?? ?private Marry target;
?? ?
?? ?public WeddingCompany(Marry target) {
?? ??? ?this.target = target;
?? ?}
?? ?@Override
?? ?public void happyMarry() {
?? ??? ?ready();
?? ??? ?this.target.happyMarry();
?? ??? ?after();
?? ??? ?
?? ?}
?? ?private void ready() {
?? ??? ?System.out.println("準(zhǔn)備婚房");
?? ?}
?? ?private void after() {
?? ??? ?System.out.println("鬧洞房");
?? ?}
?? ?
}
標(biāo)簽: