Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the adapter pattern?

2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

What is the adapter pattern? I believe that many inexperienced people are at a loss about this, so this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

In computer programming, the adapter pattern adapts the interface of a class to what the user expects, and an adaptation allows classes that usually cannot work together because of interface incompatibility to work together by wrapping the class's own interface in an existing class.

There are two types of adapter patterns:

Object adapter pattern

In this adapter pattern, the adapter holds an instance of the class it wraps. In this case, the adapter invokes the physical entity of the wrapped object.

Class adapter pattern

In this adapter mode, the adapter inherits from the implemented class (usually multiple inheritance).

Explanation:

Convert the interface of one class to another interface that the customer wants. The Adapter pattern allows classes that cannot work together because of interface incompatibility.

Basic concept

Customer: the object that needs to call our code.

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

Main content

(1) Class adapter:

When the customer defines his desired behavior in the interface, we can apply the adapter pattern, provide a class that implements the interface, and extend the existing class to achieve adaptation by creating subclasses.

The following is a UML diagram of the class adapter:

(2) object adapter:

Object adapters reduce poor coupling between code by combining them in addition to meeting the "user-expected interface". Object adaptation is recommended in your work. The following is a UML diagram of the object adapter:

(3) default adapter mode:

The default adapter pattern is a special adapter pattern, but this adapter is implemented by an abstract class, and all the methods specified in the target interface are implemented in the abstract class, but many methods are "mediocre" implementations, that is, these methods are empty methods. All concrete subclasses inherit this abstract class.

After reading the above, have you mastered what the adapter pattern is? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report