PS One sentence: Eventually choose Csdn to organize the publication of the knowledge points of these years, the article parallel migration to CSDN. Because CSDN also support markdown grammar, Ah!
"Craftsman Joshui Http://blog.csdn.net/yanbober" read the previous article "Design pattern (structural type) of the enjoyment of meta-mode (Flyweight pattern)" http://blog.csdn.net/yanbober/ article/details/45477551
Overview
Proxy mode is one of the commonly used structural design patterns, which can be accessed indirectly through a proxy object when there is no direct access to an object or access to an object, and in order to ensure the transparency of the client's use, the real object to be accessed and the proxy object need to implement the same interface. According to the use of proxy mode, the proxy mode can be divided into various types, such as protection agent, remote agent, virtual agent, buffer agent, etc., which are applied to different occasions to meet the different needs of users.
Core
Concept: provides a proxy or placeholder for an object, and the proxy object Controls access to the original object.
Agent mode structure important Core module:
Subject (abstract theme role)
It declares a common interface between real-world themes and proxy topics, so that you can use a proxy theme wherever you use real-world themes, and clients often need to program on abstract topic roles.
Proxy (Agent theme role)
It contains a reference to a real topic, so that you can manipulate real-world objects at any time, and provide an interface in the proxy theme role that is the same as a real-world theme, so that you can replace real-world themes at any time, and the agent-themed role can control the use of real-world themes. Responsible for creating and deleting real topic objects when needed, and constraining the use of real subject objects. In general, in the proxy theme role, the client needs to perform other operations before or after invoking the referenced real-world topic operation, rather than simply invoking the action in the real-world subject object.
Realsubject (real theme character)
It defines the real object represented by the proxy role, implements real business operations in the real-world theme role, and the client can indirectly invoke the actions defined in the real-world theme role through the proxy topic role.
Proxy Mode classification:
The proxy mode can be divided into many kinds according to its purpose and implementation mode, in which several proxy modes are briefly described as follows:
Remote Proxy
Provides a local proxy object for an object in a different address space, which can be in the same host, but also in another host, the remote agent is also known as Ambassador (Ambassador).
Virtual Proxy
If you need to create an object that consumes a large resource, first create an object that consumes relatively little, and the real object is created only when it is needed.
Protection agent (Protect proxy)
Control access to an object, which gives different users different levels of permissions to use.
Buffer Agent (cache proxy)
Provides temporary storage space for the results of a target operation so that multiple clients can share the results.
Smart Reference Agent (Smart Reference proxy)
When an object is referenced, it provides some extra action, such as recording the number of times an object is called.
Usage Scenarios
There are many types of proxy modes, and different types of proxy modes have different advantages and disadvantages, and they are applied to different situations:
-
A remote agent can be used when a client object needs to access an object in the remote host.
-
When you need to use an object that consumes less resources to represent an object that consumes more resources, you can reduce overhead and run time by using virtual proxies, such as when an object takes a long time to finish loading.
-
A buffer agent can be used when it is necessary to provide a temporary storage space for one of the frequently accessed operations results for multiple clients to share access to these results. By using a buffer proxy, the system does not have to re-perform the operation every time the client accesses, just get the result of the operation directly from the temporary buffer.
-
You can use a protection agent when you need to control access to an object and provide different levels of access for different users.
-
You can use smart reference proxies when you need to provide some extra action for an object's access (reference).
Program Ape Instance
A simple example:
As a piece structure is a UML diagram of a proxy mode:
Here is a simple example of a getting Started proxy pattern that follows several core modules of the proxy model. We use the program ape to buy a Hong Kong version of the MacBook as an example to illustrate the proxy mode.
PackageYanbober.github.io;//Abstract Object roleInterface Imacbook {voidBuyit ();}//target object roleClass Hangkangmacbook implements Imacbook {@Override Public void Buyit() {System.out.println ("This computer was from hangkang!"); }}//Agent Object roleClass Proxymacbook implements Imacbook {@Override Public void Buyit() {Hangkangmacbook Mac =NewHangkangmacbook (); Mac.buyit (); }}//Client Public class Main { Public Static void Main(string[] args) {Imacbook MacBook =NewProxymacbook (); Macbook.buyit (); }}
An example of a proxy mode used in Android:
In Android, the proxy mode is also widely used, for example, the Activitymanagerproxy class is a proxy, it is the agent of Activitymanagernative, That is to say Activitymanagerproxy is the proxy class above, and activitymanagernative is equivalent to Realsubject class, they all have a common interface Iactivitymanager. There is also an important class here: Activitymanager, which corresponds to the client in the class diagram of the proxy pattern. In this class, you can see a large number of getxxx functions, which are called to the Getdefault () method of the Activitymanagernative class, and the method obtains a common singleton iactivitymanager reference. The implementation in the proxy is then invoked through polymorphism.
sum up a
Agent Mode Advantages:
- The ability to reconcile the caller and the callee reduces the coupling of the system to some extent.
- The client can program the abstract topic role, add and replace the proxy class without modifying the source code, in line with the open and closed principle, the system has a good flexibility and scalability.
- The remote agent provides an implementation mechanism for access to two different address space objects, which can move objects and operations that consume more resources to a better-performing computer and improve the overall efficiency of the system.
- Virtual agents represent a resource-intensive object through an object that consumes less resources, which can save the operating overhead of the system to some extent.
- The buffering agent provides temporary cache storage for the results of an operation so that these results can be shared in subsequent use, optimizing system performance and reducing execution time.
- The protection agent can control access to an object and provide different levels of usage rights for different users.
Proxy Mode Disadvantages:
- Because proxy objects are added between the client and the real topic, some types of proxy patterns can cause requests to be processed more slowly, such as protection agents.
- Implementing the proxy pattern requires additional work, and some proxy patterns are more complex to implement, such as remote agents.
"Craftsman Joshui Http://blog.csdn.net/yanbober" Continue reading "to be continued ... 》
Proxy pattern of design pattern (structure type)