首页 > 其他 > 详细

设计模式-静态代理

时间:2018-02-23 15:11:51      阅读:172      评论:0      收藏:0      [点我收藏+]

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();
    }
}

设计模式-静态代理

原文:https://www.cnblogs.com/mf001/p/8462075.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!