Design Patterns ------ proxy mode

Proxy mode to provide an agent for other objects, to control access to the object. The so-called agents, is to represent real objects.

In some cases, an object is not suitable or not there is a direct reference to the object, and the object can play the role of intermediary agent between the client and the target object.

For sample. NetEase Blizzard agent in mainland China, Blizzard Why not Chinese direct operations, have to do as an agent by Netease? This is due to the Blizzard in the United States. Assuming that allows gamers to connect to the game server through the network of the United States, it would be a horrible situation to unimaginable.

1. Role:

Abstract thematic roles (Subject)

Acting thematic roles (Proxy) 

Real thematic roles (RealSubject)


Schematic:


The real object and the proxy object implements the same interface to access the proxy class and then access the object really want to access.

2. pros and cons 

Advantages:

Between the client proxy object plays the role of an intermediary and a target object, the target object protected; caller can coordinate with the caller , to a certain extent reduces the coupling of the system .. Enhanced scalability, the roles and responsibilities clear.

Disadvantages:

With the addition of the agent. The request will cause the processing speed slows down, and realize that some agency model is more complex.

3. Application:

(1) Remote Agent:

As an object to provide local representatives in different address spaces, to hide the fact that there are different address spaces of an object.

(2) Alerts:

According to the need to create overhead is very large objects through which to store very long time need to instantiate real objects. This only makes the object will not actually be created when the need.

(3) Security Agent:
used to control the access privileges of the real object. Able to provide different levels of permissions to different users.

(4) Smart guidelines:

When you call a real object, to provide some additional operations. Let's call this object a number of record and so on.

4. demonstrates:

Requirements: suitors through a proxy to the favorite girl gifts.

Schematic:


Proxy Interface:

 interface GiveGift  //送礼物接口
    {
        void GiveDolls();
        void GiveFlowers();
        void GiveChocolate();
    }

The pursued categories:

 class SchoolGirl
    {
        private string name;
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
    }

Suitors categories:

 class Pursuit : GiveGift  //追求者实现“送礼物”接口
    {
        SchoolGirl mm;
        public Pursuit(SchoolGirl mm)
        {
            this.mm = mm;
        }
        public void GiveDolls()
        {
            Console.WriteLine(mm.Name + " 送你洋娃娃");
        }

        ......
    }
Acting categories:
class Proxy : GiveGift  //代理类实现“送礼物”接口
    {
        Pursuit gg;
        public Proxy(SchoolGirl mm)
        {
            gg = new Pursuit(mm);
        }


        public void GiveDolls()
        {
            gg.GiveDolls();  //在实现方法中调用追求者类的相关方法
        }

       ......
    }

client:

 static void Main(string[] args)
        {
            SchoolGirl jiaojiao = new SchoolGirl();
            jiaojiao.Name = "李娇娇";

            Proxy daili = new Proxy(jiaojiao);

            daili.GiveDolls();
            daili.GiveFlowers();
            daili.GiveChocolate();

            Console.Read();
        }
Renderings:



代理模式的本质就是迂回战术。通过加入一个中间层来解决这个问题。它是软件系统中非经常见的一个模式。代理一般实现的功能都是连接到server并进行数据下载和数据翻译以及本土化处理等操作,之后代理再将这些数据传送给本地的客户去使用。代理模式的应用使得client的压力大大降低,提升了客户的数目。


Guess you like

Origin www.cnblogs.com/ldxsuanfa/p/10943212.html