The proxy pattern for C # design mode

Source: Internet
Author: User

Introduction

Agent This word, everyone in the real world has been frequent contact, such as railway station Agent ticket, because the presence of these agents, we do not have to go to the railway station at the ticket office can check or pick up the train ticket. The proxy point itself is incapable of producing tickets, What we enjoy in the agency is actually the service at the station ticket office, at the same time we can enjoy the service at the station ticket office, such as the agent Point has a vending machine, we can also enjoy shopping services. That's the advantage of the agency.

There is also a pattern in design mode that is closely linked to the definition of a proxy, and I'll show it in the most concise code.

1    /// <summary> 2     /// Business Abstraction 3     /// </summary> 4      Public Interface Isubject 5     {6         void  getticket (); 7     }
Business Abstraction
1     /// <summary>2     ///the real Logical object3     /// </summary>4      Public classRealsubject:isubject5     {6          Public voidGetTicket ()7         {8Console.WriteLine ("..... Take the ticket action .....");9         }Ten}
Real Business
1    /// <summary>2     ///agents for real business objects3     /// </summary>4     classProxysubject:isubject5     {6         Private StaticIsubject _subject =NULL;7 8         voidProxysubject_init ()9         {Ten             if(_subject = =NULL) One             { A_subject =NewRealsubject (); -             } -         } the  -          Public voidGetTicket () -         { -             if(_subject = =NULL) +             { - proxysubject_init (); +             } A             //AOP: Logs, caches, permissions, delays, exceptions at _subject. GetTicket (); -         } -}
Agency Business
1     class Program2     {3         Static voidMain (string[] args)4         {5             varProxy =NewProxysubject ();6 Proxy. GetTicket ();7 Console.readkey ();8         }9}
Print

Observing the above code, we find that it has the following characteristics

1 The upper end of the call is not a real business object, but one of its agents, the real business logic is isolated

2 within the agent, we can also define additional logic without affecting the actual business logic

This is a proxy mode.

When we don't want or can't call a business logic object directly, we can add a proxy on top of it to block specific business logic or add additional business

Proxy Mode

Because the agent itself is a relatively concise idea, so the class diagram of the proxy mode is also relatively simple, as follows:

There are three types of roles in proxy mode:

abstract role of subject (Isubject): abstraction of real and proxy topics

Real Theme Role (Realsubject): real-world themes that provide the most fundamental business logic

the agent role of the subject (Proxysubject): A real-world agent that can reference real-world themes while adding some additional features

Note: Providing business logic is a real topic, not a proxy, the proxy is just a reference and can add some extra functionality

Intersect adorner mode

In design mode, there is a design pattern called adorner mode, which can dynamically add functionality to objects,

If you understand both of these patterns, you will find that they have a lot in common, as well as concrete objects and helper objects that are dependent on abstract methods.

For example, we want to add a log function to an object: we can choose to add a proxy for it, or we can choose to add an adorner to implement, this time we may be confused, whether to add a proxy or an adorner

For their differences, from the scene they set out to compare is the best, from the start scene, the adorner mode is focused on the original object function expansion, and the agent mode is focused on the original object control or shielding, personally think this is their biggest difference

Applicable scenarios

When we don't want or can't call a business logic object directly, we can add a proxy on top of it to block specific business logic or add additional business

Advantages:

1 reduces the coupling degree of the system, which is a common advantage of the design pattern;

2 can block real business logic while adding additional functionality

Disadvantages

Using proxy mode, you need to increase the agent class, it will inevitably increase the complexity of the system

From: Blog Park-Halfway solo

Original address: https://www.cnblogs.com/banluduxing/p/9267705.html

This article is from http://www.cnblogs.com/banluduxing reprint please specify the source.

The proxy pattern for C # design mode

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.