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

"Head First Design pattern": adapter pattern

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

Share

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

Adapter mode

Adapter pattern definition: converts the interface of one class into another interface expected by the customer, and the adapter allows classes with incompatible interfaces to work together. The object adapter class diagram is as follows:

Role description:

● Target, destination interfac

● Adapter, adapter

● Adaptee, adapted object

● Client, client

The process for the customer to use the adapter is as follows:

The ● client makes a request to the adapter through the method of calling the adapter through the target interface.

The ● adapter uses the adaptor interface to convert the request into one or more invocation interfaces of the adaptor.

The ● client receives the result of the call, but is not aware that the adapter is transforming.

Advantages of the adapter pattern:

● decouples the target class from the adaptor class and reuses the existing adaptor class by introducing an adapter class without modifying the original structure.

● increases the transparency and reusability of the class, encapsulates the specific business implementation process in the adaptor class, which is transparent to the client class, and improves the reusability of the adaptor. The same adaptor class can be reused in many different systems.

● has good flexibility and expansibility. By using configuration files, you can easily replace the adapter, or add a new adapter without modifying the original code, completely compound the opening and closing principle.

Disadvantages of the adapter pattern:

● can only adapt to one adaptor class at a time, not multiple adapters at the same time.

The abstract class of ● target can only be an interface, not a class, and its use has some limitations.

The usage scenario of the adapter pattern:

The ● system needs to use some existing classes, and the interfaces of these classes do not meet the needs of the system, and there is even no source code for these classes.

● creates a reusable class to work with classes that are not much related to each other, including classes that may be introduced in the future

Extend:

● two-way adapter, the two interfaces are converted to each other, omitting the class diagram, description

● class adapter, Java does not support multiple inheritance, so omit the class diagram and explain

Case 1: suppose you already have a software system and you want it to work with a new vendor class library, but the interface designed by this new vendor is different from that of the old vendor. How to achieve it is as follows:

The solution is to add an adapter (Adapter) class without changing the current code. The adapter is transparent to the user, as follows:

Case 2: there are duck interface (duck can fly, quack), turkey interface (can fly point, aunt call), suppose we lack duck object, use turkey to impersonate, the class diagram is as follows:

In case 3:JDK, the earlier version of the Collection collection implements an elements (). This method returns an enumeration containing every element in the collection. In the new version of JDK, Iterator is used instead, and Iterator also provides the ability to delete elements. Suppose there is a reserved code, which exposes the enumeration interface, but we want to use an iterator in the new code, and we use the adapter pattern to solve the problem, as follows:

Case 4: the InputStreamReader,OutputStreamReader in the IO framework in JDK uses the adapter pattern, as follows:

Description:

● InputStream,OutputStream is the adapted object.

● Reader,Writer is the destination interface.

● StreamEncoder is an intermediate class. When the byte type is converted to characters, it needs to be converted. This is the role of your class.

● InputStreamReader,OutputStreamReader is an adapter class.

Design principle: "least knowledge" principle: talk only to your close friends.

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