Soft Test Road--PK Adapter mode

Source: Internet
Author: User

First, the adapter mode 1. Concept

transforms the interface of one class into another interface that the customer wants. A d a P T e r mode makes it possible for those classes that would otherwise not work together because of incompatible interfaces to work together.

2. Object Adapter

Using object combinations to modify the interface wrapper by the adapter, any subclass of the ligand, can be used with adapters.

Bind the client and interface together, not the implementation.

We can use a number of adapters, each of which is responsible for converting different background classes.

3. Class Adapter

Class adapter, you need multiple inheritance to implement it, which is not possible in Java and C #. But when you use multiple inheritance languages, you may still encounter such requirements.
Class diagram for class adapters:




  PS: Object adapters and class adapters use two different adaptation methods, namely, combination and inheritance.

4. Applicable Scenarios

(1) When we use third-party class libraries , or third-party APIs, we use adapter conversions to meet the needs of existing systems.
(2) when our old system was integrated with the new system, we found that the data of the old system could not meet the requirements of the new system, then we might need the adapter to complete the call requirement.
(3) We use data synchronization between different databases . (I'm just analyzing what happens when the program is implemented.) There are many other ways of [database synchronization].


Second, PK decorative mode 1. Concept

The ability to dynamically extend an object without having to change the original class file and use inheritance. It is by creating a wrapper object, that is, decorating to wrap the real object.

2. Class Diagram


3. function

Remove the adornment function from the class to simplify the original class. Effectively separates the core functions of the class from the decorative functional areas.

4. Comparison

(1) About new responsibilities

adapters can also add new responsibilities at the time of conversion, but the primary purpose is not here. The decorator mode is mainly for the decorator to add new responsibilities.
(2) about the original interface

The adapter mode uses the new interface to invoke the original interface, which is not visible or unavailable to the new system. The decorator mode uses the original interface intact, and the object that is decorated by the system is also used through the original interface. (The adorner mode that adds a new interface can be considered a variant--"translucent" decorator)
(3) about the object of its package

An adapter is a detail (that is, that class or that interface) that knows what is being adapted. The decorator knows only what its interface is, and its specific type (base class or other derived class) is only known during runtime

PS: decorator mode is used to add some new features while maintaining the interface unchanged. Adapter mode focuses on the transformation of the interface

Three, PK appearance mode 1. Concept

Provides a consistent interface for a set of interfaces in a subsystem, defining a high-level interface that makes this subsystem easier to use.

2. Class Diagram


3. function

the appearance does not encapsulate the class of the subsystem, the appearance simply provides a simplified interface
Appearance just provides you more direct operation, not the original subsystem block up, if you need the subsystem class of higher level function, still can use the original subsystem.
You can create a number of appearances for a subsystem.

The appearance mode not only simplifies the interface, but also decouples the customer from the subsystem of the component
For example, if you want to upgrade your subsystem (your home theater), if your customer code was written for the appearance rather than the subsystem, now you don't need to change the customer code, just modify the appearance code.

4. Comparison

Adapter mode, mainly adjusts the adaptation objects to suit the needs of consumers . So as to achieve the purpose of decoupling between the consumer and the adaptive ligand.
The main feature of the appearance mode is to simplify the interface and reduce the client's coupling to the appearance components . Because if the client changes, the subsystem of the component changes without affecting the client. In addition, when encapsulating components, it is appropriate to add some of the rules you want in the Appearance class.


Summary : This article introduces the basics of adapter patterns, decorative patterns and appearance patterns, and compares them according to their respective characteristics. The adapter pattern uses a combination of implementations to wrap an object to change its interface; the decorative pattern uses the open closure principle to wrap an object in order to add new behaviors and responsibilities; The appearance pattern uses the least-knowledge principle to wrap a group of objects to simplify its interface. These three each have characteristics, we should pay attention to when using.



Soft Test Road--PK Adapter 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.