找回密码
 立即注册
软件设计/软件工程 2022-05-02 213 0star收藏 版权: . 保留作者信息 . 禁止商业使用 . 禁止修改作品
代理情况在我们的现实生活中无处不在!你在淘宝买东西,你用支付宝平台支付,卖家找物流公司发货,你找朋友帮你取包裹。在这个过程中,支付宝、物流公司、你的朋友都扮演着“第三方”的角色。为了帮助您完成物品的购买,这里的第三方可以称为代理。

所谓代理,是通过引用一个新对象来实现对真实对象的操作,或者把这个新对象当作真实对象的替身。动机。



       一、模式定义
       代理模式是为对象提供代理,代理对象控制对原始对象的引用。

       在代理模式下,“第三方”代理主要充当中介,连接客户端和目标对象。

       2.图案结构
       下图是代理模式的UML结构图



       代理模式下的三种角色如下:

       主题:抽象角色。声明真实对象和代理对象的公共接口。

       代理:代理角色。代理对象实现了与真实对象相同的接口,因此可以随时代理真实对象。代理角色包含对真实对象的引用,所以她可以对真实对象进行操作,也可以附加其他操作,相当于封装了真实对象。

       RealSubject:真实人物。它代表真实的对象,我们最终将引用的对象

       3.模型实现
       我上大学的时候追过女生!有一天,你看到一个美丽的女人,一见钟情,并在心里发誓要她做你的女朋友。但是如果要这样直上去,可能就很突兀了。所以你使用迂回政策,先和她的室友相处,然后通过她的室友给她礼物,然后……

       首先出现的是一个漂亮的女孩:BeautifulGirl.java
public class BeautifulGirl {
    String name;
   
    public BeautifulGirl(String name){
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
   
}
然后是抽象主题,礼物接口:GiveGift.java
public interface GiveGift {
    /**
     * 送花
     */
    void giveFlowers();
   
    /**
     * 送巧克力
     */
    void giveChocolate();
   
    /**
     * 送书
     */
    void giveBook();
}
你男孩:you.java
public class You implements GiveGift {
    BeautifulGirl mm ;     //美女
   
    public You(BeautifulGirl mm){
        this.mm = mm;
    }
   

    public void giveBook() {
        System.out.println(mm.getName() +",送你一本书....");
    }

    public void giveChocolate() {
        System.out.println(mm.getName() + ",送你一盒巧克力....");
    }

    public void giveFlowers() {
        System.out.println(mm.getName() + ",送你一束花....");
    }

}
她最好的室友:HerChum.java
public class HerChum implements GiveGift{

    You you;
   
    public HerChum(BeautifulGirl mm){
        you = new You(mm);
    }
   
    public void giveBook() {
        you.giveBook();
    }

    public void giveChocolate() {
        you.giveChocolate();
    }

    public void giveFlowers() {
        you.giveFlowers();
    }

}
  客户端:Client.java
public class Client {
    public static void main(String[] args) {
        BeautifulGirl mm = new BeautifulGirl("小屁孩...");
        
        HerChum chum = new HerChum(mm);
        
        chum.giveBook();
        chum.giveChocolate();
        chum.giveFlowers();
    }
}
运行结果

       女巫……,我给你一本书……

       女巫……我给你一盒巧克力……

       女巫……,送你一束花……

       恩,礼物已经送出去了,能不能做到就看你的魅力了! ! ! !

       四、模型的优缺点
       优势
       1、代理模式可以协调调用者和被调用者,在一定程度上降低了系统的耦合度。

       2.代理对象可以在客户端和目标对象之间起到中介作用,起到保护目标对象的作用。

       缺点
       1、由于客户端和真实主体之间增加了代理对象,某些类型的代理模式可能会导致请求处理速度变慢。

       2.代理模式的实现需要额外的工作,有些代理模式的实现非常复杂。

       五、模式适用场景
       1.远程代理:为不同地址空间中的对象提供本地表示。这隐藏了一个对象存在于不同地址空间的事实。

       2、虚拟代理:用小对象代理大对象。这样,可以减少系统的开销。

       3. 保护代理:用于控制对真实对象的访问。

       六、模式总结
       1. 代理模式是通过引用代理对象来访问真实对象,其中代理对象充当连接客户端和真实对象的中介。

       2.代理模式主要用于远程代理、虚拟代理和保护代理。保护代理可以控制访问权限。

(Agency is everywhere in our real life! When you buy things on Taobao, you use the Alipay platform to pay. The seller looks for a logistics company to deliver the goods, and you look for a friend to help you pick up the package. In this process, Alipay, logistics companies and your friends all play the role of a "third party". In order to help you complete the purchase of goods, the third party here can be called an agent.
The so-called proxy refers to the operation of a real object by referring to a new object, or treating the new object as a substitute for the real object. Motivation.
1、 Schema definition
Proxy mode is to provide a proxy for the object, and the proxy object controls the reference to the original object.
In the proxy mode, the "third party" proxy mainly acts as an intermediary to connect the client and the target object.
2. Pattern structure
The following figure is the UML structure diagram of the agent pattern
The three roles in agent mode are as follows:
Topic: Abstract roles. Declare public interfaces for real and proxy objects.
Agent: agent role. The proxy object implements the same interface as the real object, so it can proxy the real object at any time. The proxy role contains a reference to the real object, so she can operate on the real object or attach other operations, which is equivalent to encapsulating the real object.
Realsubject: real person. It represents the real object that we will eventually reference
3. Model implementation
I chased girls when I was in college! One day, you see a beautiful woman, fall in love at first sight, and swear in your heart that she will be your girlfriend. But if you want to go straight up like this, it may be very abrupt. So you use a circuitous policy, first get along with her roommate, then give her gifts through her roommate, and then
The first is a beautiful girl: beautiful girl java
public class BeautifulGirl {
String name;
public BeautifulGirl(String name){
this. name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this. name = name;
}
}
Then comes the abstract theme, gift interface: givegift java
public interface GiveGift {
/**
*Send flowers
*/
void giveFlowers();
/**
*Send chocolate
*/
void giveChocolate();
/**
*Book delivery
*/
void giveBook();
}
Your boy: you java
public class You implements GiveGift {
BeautifulGirl mm ;     // beauty
public You(BeautifulGirl mm){
this. mm = mm;
}
public void giveBook() {
System. out. Println (mm. Getname() + ", give you a Book...);
}
public void giveChocolate() {
System. out. Println (mm. Getname() + ", give you a box of chocolates...);
}
public void giveFlowers() {
System. out. Println (mm. Getname() + ", send you a bunch of flowers...);
}
}
Her best roommate: herchum java
public class HerChum implements GiveGift{
You you;
public HerChum(BeautifulGirl mm){
you = new You(mm);
}
public void giveBook() {
you. giveBook();
}
public void giveChocolate() {
you. giveChocolate();
}
public void giveFlowers() {
you. giveFlowers();
}
}
Client: client java
public class Client {
public static void main(String[] args) {
Beautiful girl mm = new beautiful girl;
HerChum chum = new HerChum(mm);
chum. giveBook();
chum. giveChocolate();
chum. giveFlowers();
}
}
Operation results
Witch... I'll give you a Book
Witch... I'll give you a box of chocolates
Witch... Give you a bunch of flowers
Well, the gift has been sent out. Whether you can do it depends on your charm!
4、 Advantages and disadvantages of the model
advantage
1. The degree of coupling between the agent and the callee can be reduced to a certain extent.
2. The proxy object can act as an intermediary between the client and the target object and protect the target object.
shortcoming
1. Due to the addition of proxy objects between the client and the real subject, some types of proxy patterns may slow down the processing of requests.
2. The implementation of agent mode requires additional work, and the implementation of some agent modes is very complex.
5、 Mode applicable scenario
1. Remote proxy: provide local representation for objects in different address spaces. This hides the fact that an object exists in different address spaces.
2. Virtual proxy: proxy large objects with small objects. In this way, the overhead of the system can be reduced.
3. Protection agent: used to control access to real objects.
6、 Model summary
1. Proxy mode refers to accessing the real object by referring to the proxy object, in which the proxy object acts as an intermediary connecting the client and the real object.
2. Agent mode is mainly used for remote agent, virtual agent and protection agent. The protection agent can control access rights.
)





上一篇:ASP字符处理
下一篇:在 ECLIPSE 中导入 DTD 和 XSD 文件以使 XML 自动提示