设计模式-静态代理

假装没事ソ 提交于 2019-12-24 03:39:42

1.静态代理实现要求

  1. 有真实的角色
  2. 有代理的对象
  3. 真实角色和代理对象实现同一个代理接口

2.静态代理事例

MarrayContent代理MarryPerson,MarryPerson只关注结婚其他事情都交给MarrayContent代理对象去做

public class StaticProxy {
   public static void main(String args[]){
       MarryPerson person = new MarryPerson();
       Marray content = new MarrayContent(person);
       content.marray();
   }
}

//代理接口
interface Marray{
    void marray();
}
//真实角色
class MarryPerson implements Marray{
    @Override
    public void marray() {
        System.err.println("结婚");
    }
}
//代理对象
class MarrayContent implements Marray{
    private MarryPerson marryPerson;
    public MarrayContent(MarryPerson marryPerson){
        this.marryPerson =marryPerson;
    }
    private void searchTarget(){
        System.err.println("寻找合适对象!");
    }

    private void detail(){
        System.err.println("婚后处理!");
    }
    @Override
    public void marray() {
        searchTarget();
        marryPerson.marray();
        detail();
    }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!