The proxy mode is one of the common structural design modes. When it is difficult to directly access an object or access an object, you can use a proxy object for indirect access. To ensure the transparency of the client, the real object to be accessed must implement the same interface as the proxy object. Depending on the purpose of the proxy mode, the proxy mode can be divided into multiple types, such as protection proxy, remote proxy, virtual proxy, and buffer proxy. They are applied to different occasions, meet different user needs.
15.1 proxy mode Overview
In recent years, purchasing has gradually become an important branch of e-commerce. Purchasing is simply about finding someone to help you buy the goods you need. Of course, you may need to pay a certain fee to the person performing the purchase. Purchasing is usually divided into two types: one is because you cannot buy a certain item locally, or because the price of this item is more expensive than that of other regions, therefore, the consumer purchases this item in other regions or even abroad, and then delivers the goods by express delivery or directly carrying it back. There is also a kind of purchasing, because the consumer lacks the information about the product to be purchased, I can't determine the actual value and don't want to be slaughtered by sellers, so I have to entrust an intermediary to pay for it or buy it on behalf of them. Purchasing websites have emerged. They provide online purchasing services for consumers. If you are interested in the products on a foreign shopping website, you can log on to the purchasing website to fill out the purchase order and make payment, the purchasing website will help you purchase the product and send it to the consumer through the express delivery company. The product purchase process is 15-1:
Figure 15-1 goods purchase
In software development, there is also a design model that provides functions similar to those of purchasing websites. For some reason, the client does not want or cannot directly access an object. In this case, you can use a third party called "proxy" to achieve indirect access. The design mode corresponding to this solution is called the proxy mode.
Proxy mode is a widely used structural design mode with many forms of change, common proxy formats include remote proxy, protection proxy, virtual proxy, buffer proxy, and intelligent reference proxy. You will learn these different Proxy Forms later.
The proxy mode is defined as follows:
Proxy mode: provides a proxy or placeholder for an object, and the proxy object controls access to the original object. Proxy pattern: provide a surrogate or placeholder for another object to control access to it. |
Proxy mode is an object structure mode. A new proxy object is introduced in the proxy mode. The proxy object acts as an intermediary between the client object and the target object, it removes content and services that customers cannot see or adds additional services that customers need.
15.2 proxy mode structure and implementation
15.2.1 mode structure
The proxy mode has a simple structure. Its core is the proxy class. In order to allow the client to treat real objects and proxy objects in a consistent manner, the proxy mode introduces the abstraction layer, as shown in Figure 15-2:
Figure 15-2 proxy mode structure
Figure 15-2 shows that the proxy mode includes the following three roles:
(1) Subject (Abstract topic role ):It declares the common interface between a real topic and a proxy topic, so that a proxy topic can be used wherever a real topic is used. The client usually needs to program the abstract topic role.
(2) proxy (proxy topic role ):It contains a reference to a real topic, so that you can operate on real theme objects at any time. It provides the same interface as the real theme role in the proxy topic role, this allows you to replace real themes at any time. The proxy topic role can also control the use of real themes and create and delete real theme objects as needed, restrict the use of real theme objects. Generally, in a proxy topic role, the client needs to perform other operations before or after calling the referenced real topic operation, rather than simply calling the operations in the real topic object.
(3) realsubject (real topic role ):It defines the real objects represented by the proxy role and implements real business operations in the real theme role. The client can indirectly call the operations defined in the real theme role through the proxy topic role.
15.2.2 mode implementation
The structure of the proxy mode is relatively simple, but the actual use and implementation process is much more complicated, especially the design and implementation of the proxy class.
The abstract topic class declares the public methods of the real topic class and the proxy class. It can be an interface, abstract class, or a specific class. The client program the abstract topic class to treat the real and proxy themes in a consistent manner, the typical abstract topic code is as follows:
abstract class Subject{ public abstract void Request();}
The real theme class inherits the abstract theme class and provides specific implementation of business methods. The typical code is as follows:
Class realsubject: Subject {public override void request () {// specific implementation code of the Business Method }}
The proxy class is also a subclass of the abstract theme class. It maintains a reference to the real theme object and calls the business methods implemented in the real theme, you can add some new methods to expand or constrain functions based on the original business method during the call. The simplest proxy implementation code is as follows:
Class Proxy: Subject {private realsubject = new realsubject (); // maintain a reference to a real topic object public void prerequest (){...... } Public override void request () {prerequest (); realsubject. Request (); // call the method postrequest () for the real theme object;} public void postrequest (){...... }}
In the actual development process, the implementation of the proxy class is much more complex than the above Code. The proxy mode can be divided into many types based on its purpose and implementation method. The following describes several common proxy modes:
(1)Remote proxy):Provide a local proxy object for an object in a different address space. The different address space can be in the same host, but also in another host, remote proxy is also called ambassador ).
(2)Virtual Proxy: If you need to create an object that consumes a large amount of resources, first create an object that consumes a relatively small amount of resources, real objects are created only when necessary.
(3)Protect proxy):Controls access to an object and provides different levels of user permissions.
(4)Cache proxy):Provides a temporary storage space for the results of a target operation so that multiple clients can share the results.
(5)Smart reference proxy):When an object is referenced, additional operations are provided, such as recording the number of calls of the object.
In these common proxy modes, the design of some proxy classes is very complicated. For example, the remote proxy class encapsulates the underlying network communication and calls to remote objects, and its implementation is more complicated.
[Author: Liu Wei (Sunny) http://blog.csdn.net/lovelion]