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

How to master the Java adapter pattern

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

Share

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

Today, I would like to share with you the relevant knowledge about how to master the Java adapter pattern. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

1. What is the adapter pattern?

In fact, you should be familiar with the word adapter. We know that Apple's iPhone no longer provides a charging plug, and there is only one data cable in the box, so we cannot use this data cable alone to charge on the 220V plug board, so we need to buy a new adapter to connect the plug board to the phone. This is the adapter.

In fact, it may also appear in the software design that the components with certain business functions that need to be developed already exist in the existing component library, but they are not compatible with the interface specifications of the current system, and if the cost of redeveloping these components is very high, these problems can be well solved by using the adapter pattern.

2. Definition of adapter pattern

Adapter pattern: converts the interface of one class into another that the customer wants, so that classes that can't work together because of interface incompatibility can work together.

The adapter pattern can be divided into two types: class structure pattern and object structure pattern. The former has a higher degree of coupling between classes than the latter, and requires programmers to understand the internal structure of related components in the existing component library, so there are relatively few applications.

3. Advantages and disadvantages of adapter patterns.

Advantages:

The client can call the target interface transparently through the adapter

Reuse existing classes, programmers do not need to modify the original code and reuse existing adaptor classes

The target class and the adaptor class are decoupled, and the problem of inconsistent interface between the target class and the adaptor class is solved.

Conform to the principle of opening and closing in many business scenarios

Disadvantages:

The adapter writing process needs to be fully considered in the light of the business scenario, which may increase the complexity of the system.

Increase the difficulty of reading the code, reduce the readability of the code, too much use of adapters will make the system code messy

4. The structure and implementation of adapter pattern.

(the following structure is referenced from the Internet)

The structure of the pattern:

Target interface: the interface expected by the current system business, which can be an abstract class or interface

Adaptee class: it is a component interface in an existing component library that is accessed and adapted

Adapter class: it is a converter that converts the adaptor interface to the target interface by inheriting or referencing the adaptor's object, allowing customers to access the adaptor in the format of the target interface

Structure of the class adapter pattern:

The structure of the object adapter pattern:

5. Code implementation of adapter pattern

A. use combination to implement the adapter:

We now have a three-phase plug for a laptop, but we now need to use a two-phase plug to charge, so we need to install a two-phase adapter for this plug:

Three-phase socket interface:

/ * three-phase socket interface * / public interface ThreePlugIf {/ / public void powerWithThree powered by three-phase current ();}

GB two-phase socket:

/ * * GB two-phase socket * / public class GBTowPlug {public void powerWithTwo () {System.out.println ("using two-phase current");}}

Notebook:

/ * * Notebook * / public class NoteBook {private ThreePlugIf plug; public NoteBook (ThreePlugIf plug) {this.plug = plug;} / / charge public void charge () {plug.powerWithThree ();}} using the socket

Three-phase to two-phase socket adapter:

/ * three-phase to two-phase socket adapter * / public class TwoPlugAdapter implements ThreePlugIf {private GBTowPlug plug; public TwoPlugAdapter (GBTowPlug plug) {this.plug = plug;} @ Override public void powerWithThree () {System.out.println ("through conversion"); plug.powerWithTwo ();}}

Test class:

Public class Test {public static void main (String [] args) {GBTowPlug two = new GBTowPlug (); ThreePlugIf three = new TwoPlugAdapter (two); NoteBook nb = new NoteBook (three); nb.charge ();}}

Output result:

By conversion

Power supply using two-phase current

A combined adapter is called an object adapter.

Features:

Combine the "adapted" into the adapter class as an object to modify the target interface and wrap it to the adaptor.

B. use inheritance to implement the adapter:

Inherit the adapter:

/ * * socket adapter using inheritance mode * / public class TwoPlugAdapterExtends extends GBTowPlug implements ThreePlugIf {@ Override public void powerWithThree () {System.out.println ("with inheritance adapter"); this.powerWithTwo ();}}

Test class:

Public class Test {public static void main (String [] args) {GBTowPlug two = new GBTowPlug (); ThreePlugIf three = new TwoPlugAdapter (two); NoteBook nb = new NoteBook (three); nb.charge (); / / use inheritance method three = new TwoPlugAdapterExtends (); nb = new NoteBook (three); nb.charge ();}}

Output:

By conversion

Power supply using two-phase current

With the help of inheritance adapter

Power supply using two-phase current

The inheritance method is called class adapter.

Features:

Through multiple inheritance of incompatible interfaces, the matching of the target interface is realized, and the adaptation is realized for a single class.

6. Application scenario of adapter pattern

The previously developed system has classes that meet the functional requirements of the new system, but its interface is not consistent with that of the new system.

Use components provided by third parties, but the component interface definition is different from the interface definition required by yourself

These are all the contents of the article "how to master Java adapter patterns". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

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

Development

Wechat

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

12
Report