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 implement the Adapter pattern of Java Design pattern

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of "how to realize the adapter pattern of Java design pattern". The editor shows you the operation process through the actual case, the operation method is simple and fast, and the practicality is strong. I hope this article "how to realize the adapter pattern of Java design pattern" can help you solve the problem.

What is the adapter pattern?

The adapter pattern (Adapter) is defined as follows: the interface of one class is converted into another interface that the customer wants, so that those classes that cannot work together because the interfaces are not compatible 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.

Advantages

1. The client can call the target interface transparently through the adapter.

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

3. The target class and the adaptor class are decoupled to solve the problem of inconsistent interface between the target class and the adaptor class.

4. It conforms to the principle of opening and closing in many business scenarios.

Shortcoming

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

2. Increase the difficulty of reading the code and reduce the readability of the code. Too much use of adapters will make the system code messy.

Knowledge point

Class adapter pattern: it can be implemented by multiple inheritance methods. For example, C++ can define an adapter class to inherit both the business interface of the current system and the component interface that already exists in the existing component library; Java does not support multiple inheritance, but it can define an adapter class to implement the business interface of the current system while inheriting the components that already exist in the existing component library.

Object adapter pattern: the components that have been implemented in the existing component library can be introduced into the adapter class, which also implements the business interface of the current system. Now let's introduce their basic structure.

Adapter pattern implementation

Case: Hong Cat uses flint to promote the fire dance whirlwind sword

Target (Target) interface: Changhong sword gas

Adaptee category: fire dance whirlwind sword gas

Adapter (Adapter) class: flint

At present, Hong Cat can only promote Changhong sword, but he wants to promote fire dance whirlwind sword.

We can only use the power of flint to transform Changhong sword into fire dance whirlwind sword.

Note: I am here to make the fire dance whirlwind sword gas suitable for Changhong sword gas.

Class adapter Changhong sword gas

Declare a Changhong sword interface, including an abstract method that drives Changhong sword spirit

Public interface Chang {void chang ();} Fire Dance whirlwind Sword

The fire dance whirlwind sword gas class declares a way to promote the fire dance whirlwind sword gas.

Public class Huo {public void huo () {System.out.println ("Fire Dance whirlwind Jian Qi");} flint

The flint class inherits the fire dance whirlwind sword gas class (purpose: inheriting the method in the class), and realizes the Changhong sword gas interface (purpose: to realize the method of promoting Changhong sword gas). In the method of promoting Changhong sword gas, the method of promoting fire dance whirlwind sword gas is called.

Public class JingShi extends Huo implements Chang {@ Override public void chang () {huo ();}} Test public class Demo {public static void main (String [] args) {Chang = new JingShi (); .chang ();}}

Object adapter Changhong sword spirit

Declare a Changhong sword interface, including an abstract method that drives Changhong sword spirit

Public interface Chang {void chang ();} Fire Dance whirlwind Sword

The fire dance whirlwind sword gas class declares a way to promote the fire dance whirlwind sword gas.

Public class Huo {public void huo () {System.out.println ("Fire Dance whirlwind Jian Qi");} flint

Flint class, realized the Changhong sword gas interface, declared a fire dance whirlwind sword gas attribute, and realized the method of promoting Changhong sword gas. The method of promoting the fire dance whirlwind sword gas is put in the method of promoting Changhong sword gas.

Public class JingShi implements Chang {private Huo huo; JingShi () {} JingShi (Huo huo) {this.huo = huo;} @ Override public void chang () {huo.huo ();}} Test

New a fire dance whirlwind sword gas object, new a fire whirlwind sword gas object, and pass the fire dance whirlwind sword gas object into the fire crystal object.

Public class Demo {public static void main (String [] args) {Huo huo = new Huo (); Chang = new JingShi (huo); .chang ();}}

That's all for "how to implement the adapter pattern for the Java design pattern". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

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