Home >Java >javaTutorial >How to implement the adapter pattern of Java design pattern
The adapter pattern (Adapter) is defined as follows: Convert the interface of a class into another interface that the customer wants, so that the original Classes that cannot work together because of incompatible interfaces can work together. The adapter pattern is divided into two types: class structural pattern and object structural pattern. The former has a higher degree of coupling between classes than the latter, and requires programmers to understand the internal structure of relevant components in the existing component library, so it is relatively rarely used. some.
1. The client can transparently call the target interface through the adapter.
2. Existing classes are reused. Programmers do not need to modify the original code and reuse existing adapter classes.
3. Decouple the target class and the adapter class to solve the problem of inconsistent interfaces between the target class and the adapter class.
4. Comply with the opening and closing principle in many business scenarios.
1. The adapter writing process needs to be comprehensively considered in conjunction with the business scenario, which may increase the complexity of the system.
2. Increase the difficulty of code reading and reduce code readability. Excessive use of adapters will make the system code messy.
Class Adapter Pattern: It can be implemented using multiple inheritance. For example, C can define an adapter class to inherit both the business interface of the current system and the components that already exist in the existing component library. Interface; Java does not support multiple inheritance, but you can define an adapter class to implement the business interface of the current system while inheriting components that already exist in the existing component library.
Object adapter mode: You can introduce components that have been implemented in the existing component library into the adapter class, which also implements the business interface of the current system. Now let's introduce their basic structure.
Case: Hongmao uses fire crystals to trigger the Fire Dance Whirlwind Sword Qi
Target interface: Changhong Sword Qi
Adaptee type: Fire Dance Whirlwind Sword Qi
Adapter type: Fire Crystal
Hongmao can only activate the Changhong Sword Qi at present, but he wants to activate the fire Wu Xuanfeng Sword Qi
Can only use the power of fire crystal to transform Changhong Sword Qi into Fire Wu Xuanfeng Sword Qi
Note: I am here to adapt Huowu Xuanfeng Sword Qi to Changhong Jianqi
Declares an interface for Changhong Jianqi, including an abstract method to activate Changhong Jianqi
public interface Chang { void chang(); }
Fire Dance Whirlwind Sword Qi category, declares a method to activate the Fire Dance Whirlwind Sword Qi.
public class Huo { public void huo() { System.out.println("火舞旋风剑气"); } }
The fire crystal stone class inherits the Fire Dance Whirlwind Sword Qi class (purpose: you can use the methods in the class after inheriting), and implements the Changhong Sword Qi interface (purpose : Method to realize the activation of Changhong Sword Qi). Among the methods for activating the Changhong Sword Qi, the method for activating the Fire Dance Whirlwind Sword Qi is used.
public class JingShi extends Huo implements Chang { @Override public void chang() { huo(); } }
public class Demo { public static void main(String[] args) { Chang haha = new JingShi(); haha.chang(); } }
Declares an interface of Changhong Jianqi, including An abstract method to activate the Changhong Sword Qi
public interface Chang { void chang(); }
Fire Dance Whirlwind Sword Qi class declares a method to activate the Fire Dance Whirlwind Sword Qi.
public class Huo { public void huo() { System.out.println("火舞旋风剑气"); } }
The fire crystal stone class implements the Changhong Sword Qi interface, declares the properties of a Fire Dance Whirlwind Sword Qi, and implements the method of stimulating the Changhong Sword Qi. The method of activating the Fire Dance Whirlwind Sword Qi is included in the method of activating the Changhong Sword Qi.
public class JingShi implements Chang { private Huo huo; JingShi() { } JingShi(Huo huo) { this.huo = huo; } @Override public void chang() { huo.huo(); } }
New a Fire Dance Whirlwind Sword Qi object, new a Fire Crystal Stone object, and pass the Fire Dance Whirlwind Sword Qi object into the Fire Crystal Stone object.
public class Demo { public static void main(String[] args) { Huo huo = new Huo(); Chang haha = new JingShi(huo); haha.chang(); } }
The above is the detailed content of How to implement the adapter pattern of Java design pattern. For more information, please follow other related articles on the PHP Chinese website!