In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to use the agent pattern in the C# design pattern. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
I. introduction
In the process of software development, some objects cannot or cannot be accessed directly because of network or other obstacles. If direct access to these objects brings unnecessary complexity to the system, you can add an intermediate layer between the client and the target object to let the proxy object replace the target object, and then the client only needs to access the proxy object. One solution for the proxy object to help us request the target object and return the result to the client is the proxy mode that we are going to introduce today.
Second, the detailed introduction of the agency model.
Proxy patterns can be divided into the following categories according to the purpose of use:
Remote agent: provides a local representative object for an object in a different address space. This different address space can be in this computer or in another computer. The most typical example is that a client invokes 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 needed.
Copy-on-Write agent: a type of virtual agent that delays replication (or cloning) until the client needs it to actually take action.
Protect or Access agent: controls access to an object and provides different levels of permissions to different users.
Firewall (Firewall) proxy: protects the target from malicious users.
Intelligent reference (Smart Reference) proxy: when an object is referenced, it provides some additional operations, such as recording the number of calls to the object.
Cache proxy: provides temporary storage space for the results of a target operation so that multiple clients can have these results.
Of all the above types of agent patterns, virtual agents, remote agents, intelligent reference agents and protection agents are the more common agent patterns. Let's take a look at the specific definition of the agent pattern.
2.1 definition
Proxy pattern-provides a proxy to 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 act as an intermediary between the client and the target object. For example, a shortcut to a computer desktop is a proxy object, and a shortcut is a proxy for the program it refers to.
2.2 Agent pattern implementation
After reading the description of the agency model, the following is an example in life to explain the agency model. In real life, if a colleague goes abroad or a friend goes abroad, we often drag this friend to help bring some electronics or cosmetics and other things. In this scene, the friend who goes abroad is an agent, and he (she) is an agent of his (her) friend. Since his friends can't go shopping abroad, but he can, so his friends all ask him to help bring some things. The following scenario is used to implement the proxy mode. The specific code is as follows:
/ / client calls class Client {static void Main (string [] args) {/ / create a proxy object and request Person proxy = new Friend (); proxy.BuyProduct (); Console.Read ();}} / / Abstract topic role public abstract class Person {public abstract void BuyProduct () } / / Real theme character public class RealBuyPerson: Person {public override void BuyProduct () {Console.WriteLine ("Buy me an IPhone and an Apple computer");}} / / Agent role public class Friend:Person {/ / reference real theme instance RealBuyPerson realSubject Public override void BuyProduct () {Console.WriteLine ("methods to access real entity objects through proxy classes"); if (realSubject = = null) {realSubject = new RealBuyPerson ();} this.PreBuyProduct (); / / call real theme method realSubject.BuyProduct () This.PostBuyProduct () } / / some operations performed by the agent role public void PreBuyProduct () {/ / you may not know that a friend asks this friend to bring things. First of all, this friend who goes abroad has to make a list of things for each friend to bring and wait for Console.WriteLine ("I'm afraid I'm confused. I need to make a list. Zhang San: bring a camera." Lisa: bring Iphone. ") } / / after shopping, the agent role needs to classify the purchased things according to the needs of each friend public void PostBuyProduct () {Console.WriteLine ("finally finished, now it's time to sort things out. The camera is Zhang San's. Iphone belongs to Li Si. ");}}
There are corresponding comments in the above code, which will not be explained here.
2.3 Class Diagram structure of Agent pattern
After looking at the implementation of the agent pattern, the following example is used to analyze the class diagram structure of the agent pattern. The specific class diagram is as follows:
In the above class diagram, there are three roles involved in the proxy pattern:
Abstract theme role (Person): declares a common interface between real themes and proxy themes, so that proxy themes can be used wherever real themes are used.
Proxy topic role (Friend): the proxy topic role contains references to the real topic, so you can manipulate the real topic object; the proxy topic role is responsible for creating the real topic object when needed; the proxy role usually performs some other operations before or after passing the client call to the real topic, rather than simply passing the call to the real topic object. For example, the PreBuyProduct and PostBuyProduct methods here are other actions performed by the proxy topic role.
Real topic role (RealBuyPerson): defines the real object that the proxy role represents.
Attachment: in the actual development process, when we add service references on the client side, we will add some additional classes in the client program, and the classes generated on the client side play the role of proxy topic. Our client also calls these proxy roles directly to access the operations provided by the remote service. This is a typical example of a remote agent.
Third, the advantages and disadvantages of agency model.
After a comprehensive analysis of the agent model, let's take a look at the pros and cons of this model:
Advantages:
The proxy mode can isolate the objects that are actually called, which reduces the coupling of the system to a certain extent.
The proxy object acts as an intermediary between the client and the target object, which can protect the target object. The proxy object can perform an additional operation, such as a permission check, before making a request to the target object.
Disadvantages:
Due to the addition of a proxy object between the client and the real topic, the processing speed of the request becomes slower
The implementation of the proxy class also requires extra work, which increases the implementation complexity of the system.
This is the end of the article on "how to use the proxy pattern in the C# design pattern". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.