zoukankan      html  css  js  c++  java
  • Chapter 7 代理模式

    代理模式:为其它对象提供一种代理以控制对这个对象的访问。

    代码:

    package xiao;

    class Girl{
    private String name;
    public void setName(String name){
    this.name = name;
    }
    }
    interface GiveGift{
    public void giveDolls();
    public void giveFlowers();
    public void giveChocolate();
    }
    class Pursuit implements GiveGift{
    private Girl girl;
    public Pursuit(Girl girl){
    this.girl = girl;
    }
    public void giveDolls(){
    System.out.println("give she Dolls!");
    }
    public void giveFlowers(){
    System.out.println("give she Flowers!");
    }
    public void giveChocolate(){
    System.out.println("give she Chocolate!");
    }
    }
    class Proxy implements GiveGift{
    private Pursuit pursuit;
    public Proxy(Girl girl){
    pursuit = new Pursuit(girl);
    }
    public void giveDolls(){
    pursuit.giveDolls();
    }
    public void giveFlowers(){
    pursuit.giveFlowers();
    }
    public void giveChocolate(){
    pursuit.giveChocolate();
    }
    }
    public class Hello {

    public static void main(String[] args) throws Exception{
    Girl girl = new Girl();
    girl.setName("jiaojiao");
    Proxy daili = new Proxy(girl);
    daili.giveDolls();
    daili.giveFlowers();
    daili.giveChocolate();
    }
    }

    这是通过男孩A想追女孩C,而男孩A不认识C,但是男孩B认识C,A通过B来给C送礼物的代码。

    代理模式的用途:

    第一:远程代理,为一个对象在不同的地址空间提供局部代表。这样可以隐藏一个对象存在于不同地址空间的事实。

    第二:虚拟代理,根据需要创建开销很大的对象。通过它来存放实例化需要很长时间的真实对象。

    第三:安全代理,用来控制真实对象访问时的权限。

    第四:智能指引,是指当调用真实的对象时,代理处理另外一些事。

  • 相关阅读:
    三联生活周刊:女游戏设计师之死
    HTML
    营运社区所需要的基本心理学常识
    对C++下struct 和 类默认继承的认识
    什么是列表?
    什么是个人网站?
    DevExpress ASPxListBox can't get selected items after postback
    ListItemEventHandler does not fire on the prospective list
    SPF和SharePoint Server的区别
    什么是网站?
  • 原文地址:https://www.cnblogs.com/tuifeideyouran/p/3733543.html
Copyright © 2011-2022 走看看