Proxy mode vs appearance mode vs adapter Mode

Source: Internet
Author: User
Proxy mode vs appearance mode vs adapter Mode After learning the design model for more than a month, let's make some comparisons and conclusions on some of the more confusing models. If there are any incorrect ones, I hope you can correct them. First, let's look at the definition of the proxy mode: provide a proxy for other objects to control access to this object. The following is its UML diagram: The subject class defines the common interfaces of realsubject and proxy, so that porxy can be used wherever realsubject is used. Why the proxy mode? In what scenarios does it apply? 1. remote proxy provides a local representation of an object in different address spaces. In this way, the fact that an object exists in different address spaces can be hidden. 2. Virtual proxy is an object with high overhead as needed. It stores real objects that take a long time for instantiation. For example, when a Web page is opened, you can use a virtual proxy to replace images in the real web page so that you can quickly open the web page, while the images are opened one by one, the virtual proxy stores the path and size of the real image. 3. Security Proxy is used to control the access permissions of real objects. 4. intelligent guidance refers to the proxy to handle other things when calling real objects. For example, the reference count of C ++ smart pointers and kernel objects. But what about the appearance model? Let's look at its definition: it provides a consistent interface for a group of interfaces in the subsystem. This mode defines a high-level interface, which makes the subsystem easier to use. The following is its UML diagram: Apply the appearance mode: 1. In the initial stage of design, we consciously separate two different layers and establish the appearance between the layers. In this way, we can provide a simple interface for complicated subsystems to reduce coupling.
2. In the development stage, subsystems often become more and more complex due to constant reconstruction and evolution. Increasing the appearance can provide a simple interface to reduce the dependencies between them.
3. When maintaining a legacy large system, it may be very difficult to maintain and expand, and a new appearance class is developed for the new system to provide a legacy system with a rough or highly complex design.CodeAllows the new system to interact with the appearance object, and the complex work of the appearance and legacy code.
Finally, let's take a look at the adapter mode. It is defined to convert an interface of a class to another interface that the customer wants. The adapter mode allows the classes that cannot work together due to interface incompatibility to work together. The following is its UML diagram:
The adapter mode applies to: when the data and behavior of the system are correct, but the interface does not match, we should consider using the adapter to make an existing object outside the control scope match a certain interface. The adapter mode is mainly used to reuse some existing classes, but the interfaces are inconsistent with the requirements of the Reuse Environment. Summary: The three models share the same effect that they act as an intermediate layer between the user and the actually used class or system, this function allows users to call real classes indirectly. Apart from their differences, they have different application scenarios and different essential ideas. The main difference between proxy and appearance is that the proxy object represents a single object and the appearance object represents a sub-system. The proxy client object cannot directly access the object, A proxy provides independent access to the target object, while a common appearance object provides a simplified and common interface for calling the functions of each component of the subsystem. Proxy is a representative of the original object, and other operations that need to deal with this object are negotiated with this representative. The adapter does not need to create a representative. It only needs to combine the original classes for specific purposes. Both the appearance and the adapter are encapsulation of the existing system. The new interface defined by the appearance, while the adapter is to reuse an original interface, the adapter is to make the two existing interfaces work together, the appearance is to provide a more convenient access interface for the existing system. If it is hard to say that the appearance is adaptive, the adapter can be used to adapt to the object, and the appearance is used to adapt to the entire subsystem. That is to say, the appearance is more specific to objects. The proxy mode provides the same interface as the real class. It is intended to use the proxy class to process real classes and implement some functions of specific services or real classes, The facade mode focuses on simplified interfaces, while the adapter mode focuses on conversion interfaces.

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.