C # design Pattern Series: Proxy mode (proxies Pattren)

Source: Internet
Author: User

First, Introduction

In the software development process, some objects sometimes due to network or other obstacles, so that can not be enough or no direct access to these objects, if the direct access to the object to the system brings unnecessary complexity, this time can be added between the client and the target object of the middle layer, the proxy object to replace the target object, Then the client only needs to access the proxy object, the proxy object to help us to request the target object and return the results to the client, such a solution is today to introduce the proxy pattern

Second, the agent model of the detailed introduction

The proxy mode can be divided into the following categories according to the purpose of use:

    • Remote proxy: Provides a local representative object for an object in a different address space, either in this computer or on another computer, such as a client invoking a Web service or a WCF service.
    • Virtual Proxy: Create a resource-intensive object as needed so that the object is actually created only when it is needed.
    • Take action only when it is necessary.
    • Protect (Protect or access) proxies: Control access to an object, providing different levels of usage rights to different users.
    • Firewall (Firewall) Proxy: protects the target from being approached by a malicious user.
    • Smart Reference Proxy: provides some extra action when an object is referenced, such as the number of times a call to this object is recorded.
    • Cache Proxy: provides temporary storage space for the results of a target operation so that multiple clients can make these results.

2.1 Definitions

Proxy mode--is to provide a proxy for an object, and the proxy object controls the reference to the original object, in some cases a client does not want or cannot directly reference an object, and the proxy object can mediate between the client and the target object.

2.2 Structure diagram of proxy mode

2.3 Composition of the pattern

    • Abstract theme Role (Subject): Declares a common interface for real topics and proxies, so that you can use a proxy theme anywhere you use a real theme
    • Proxy theme role: The proxy theme role contains references to real topics that allow you to manipulate real objects, and the proxy role is responsible for creating real objects when needed.
    • Real-World theme Role (Realsubject): Defines the real object that the proxy role represents.

For example, during the development of WCF or webservice, when we add a service reference to the client, we add some extra classes in the customer program, and the client-generated class acts as a proxy-themed role, and our clients directly invoke these proxy roles to access the operations provided by the remote service. This is a typical example of a remote agent.

2.4 Code for Proxy mode

The subject class, which defines the common interface of the realsubject and proxy, so that you can use the proxy in any place where you use Realsubject

   Public Abstract   class Subject    {public        abstractvoid Request ();    }
View Code

The Realsubject class, which defines the real object for all delegates of the proxy

   Public   class Realsubject:subject    {public        overridevoid Request ()        {            Console.WriteLine (" Real Object ");}    }
View Code

proxy class, which maintains a reference that allows the agent to access the entity and provides an interface that is the same as the subject interface, so that the proxy can be used instead of the entity

     Public class Proxy:subject    {        protected realsubject realsubject;         Public Override void Request ()        {            if (realsubject==null)            {                new realsubject ();            }            Realsubject. Request ();        }    }
View Code

Client Calls

   class Program    {        staticvoid Main (string[] args)        {            new Proxy ();            Proxy. Request ();            Console.read ();        }    }
View CodeThree, the advantages and disadvantages of the agent model

After a thorough analysis of the agent pattern, let's look at the pros and cons of this pattern:

Advantages:

    1. The proxy mode can be used to isolate the calling object, and to some extent, the coupling degree of the system is reduced;
    2. The proxy object acts as a mediator between the client and the target object, which can be used to protect the target object. The proxy object can perform an additional operation, such as permission checking, before making a request to the target object.

Disadvantages:

    1. Because a proxy object is added between the client and the real topic, the processing speed of the request becomes slower
    2. Implementing the proxy class also requires additional work, which increases the complexity of the system's implementation.

C # design Pattern Series: Proxy mode (proxies Pattren)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.