Home>Article> What does adapter mode mean?

What does adapter mode mean?

hzc
hzc Original
2020-06-29 16:09:51 3354browse

In computer programming, the adapter pattern adapts the interface of a class to what the user expects. An adaptation allows classes that usually cannot work together because of incompatible interfaces to work together. Your own interface is wrapped in an existing class.

What does adapter mode mean?

There are two types of adapter modes:

Object adapter mode

-- In this In the adapter pattern, the adapter holds an instance of the class it wraps. In this case, the adapter calls the physics entity of the wrapped object.

Class Adapter Pattern

-- In this adapter pattern, the adapter inherits from the implemented class (generally multiple inheritance).

Explanation:

Convert the interface of a class into another interface that the customer wants. The Adapter pattern enables classes that would otherwise not work together due to incompatible interfaces to work together.

Basic concepts

Customer: the object that needs to call our code.

The purpose of the Adapter pattern: retain the services provided by existing classes and provide interfaces to customers to meet customer expectations.

Main content

(1) Class adapter:

When the customer defines the behavior he expects in the interface, we can apply the adapter pattern and provide an implementation of the Interface classes, and extend existing classes to achieve adaptation by creating subclasses.

The following is the UML diagram of the class adapter:

(2) Object adapter:

Through combination, the object adapter not only meets the "user expectation interface" but also reduces the complexity between codes. Poor coupling. It is recommended to use "object adaptation" at work. The following is the UML diagram of the object adapter:

(3) Default adapter mode:

The default adapter mode is a special Adapter pattern, but this adapter is implemented by an abstract class, and all methods specified in the target interface must be implemented in the abstract class, but the implementation of many methods is "mediocre" implementation, that is, these methods They are all empty methods. All specific subclasses must inherit this abstract class.

The above is the detailed content of What does adapter mode mean?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn