This article brings you relevant knowledge about java, which mainly introduces issues related to design patterns, and mainly talks about the adapter pattern. The adapter pattern is mainly used to convert the interface of a class Convert it into the target class format desired by the client, so that originally incompatible classes can work together, and decouple the target class and adapter class. I hope it will be helpful to everyone.
## Recommended study: "java video tutorial"
1. What is the adapter mode:The adapter mode is mainly used to convert the interface of a class into the target class format desired by the client, so that originally incompatible classes can work together and decouple the target class and adapter class; it also conforms to the "opening and closing principle" ”, you can add new adapter classes without modifying the original code; encapsulating the specific implementation in the adapter class is transparent to the client class and improves the reusability of the adapter, But the disadvantage is that the implementation process of replacing the adapter is more complicated.
Therefore, the adapter mode is more suitable for the following scenarios:1. Class adapter mode:
##Target interface (Target ): The interface that customers expect. The target can be a concrete or abstract class, or an interface.Running result:
- Class that needs to be adapted (Adaptee): The class that needs to be adapted or the adapter class.
- Adapter: Convert the original interface into the target interface by packaging an object that needs to be adapted.
// 已存在的、具有特殊功能、但不符合我们既有的标准接口的类 class Adaptee { public void specificRequest() { System.out.println("被适配类具有 特殊功能..."); } } // 目标接口,或称为标准接口 interface Target { public void request(); } // 具体目标类,只提供普通功能 class ConcreteTarget implements Target { public void request() { System.out.println("普通类 具有 普通功能..."); } } // 适配器类,继承了被适配类,同时实现标准接口 class Adapter extends Adaptee implements Target{ public void request() { super.specificRequest(); } } // 测试类public class Client { public static void main(String[] args) { // 使用普通功能类 Target concreteTarget = new ConcreteTarget(); concreteTarget.request(); // 使用特殊功能类,即适配类 Target adapter = new Adapter(); adapter.request(); } }Copy after login
普通类 具有 普通功能... 被适配类具有 特殊功能...
// 适配器类,直接关联被适配类,同时实现标准接口 class Adapter implements Target{ // 直接关联被适配类 private Adaptee adaptee; // 可以通过构造函数传入具体需要适配的被适配类对象 public Adapter (Adaptee adaptee) { this.adaptee = adaptee; } public void request() { // 这里是使用委托的方式完成特殊功能 this.adaptee.specificRequest(); } } // 测试类 public class Client { public static void main(String[] args) { // 使用普通功能类 Target concreteTarget = new ConcreteTarget(); concreteTarget.request(); // 使用特殊功能类,即适配类, // 需要先创建一个被适配类的对象作为参数 Target adapter = new Adapter(new Adaptee()); adapter.request(); } }
Sometimes there are multiple abstract methods in an interface we write. When we write the implementation class of the interface, they must be implemented. All methods of this interface, which is obviously sometimes wasteful, because not all methods are what we need, sometimes only some are needed. In order to solve this problem, we introduce the adapter mode of the interface, with the help of an abstract class , this abstract class implements the interface and all methods, and we do not deal with the original interface, only get in touch with the abstract class, so we write a class, inherit the abstract class, and rewrite the methods we need. . Take a look at the class diagram:
This is easy to understand. In actual development, we often encounter too many methods defined in this interface, so that sometimes we Not all are required in some implementation classes. Look at the code:
public interface Sourceable { public void method1(); public void method2(); }
Abstract class Wrapper2:
public abstract class Wrapper2 implements Sourceable{ public void method1(){} public void method2(){} } public class SourceSub1 extends Wrapper2 { public void method1(){ System.out.println("the sourceable interface's first Sub1!"); } } public class SourceSub2 extends Wrapper2 { public void method1(){ System.out.println("the sourceable interface's second Sub2!"); } }
public class WrapperTest { public static void main(String[] args) { Sourceable source1 = new SourceSub1(); Sourceable source2 = new SourceSub2(); source1.method1(); source1.method2(); source2.method1(); source2.method2(); } }
Running results:
the sourceable interface's first Sub1! the sourceable interface's second Sub2!
Recommended learning: "
java video tutorialThe above is the detailed content of Java Design Pattern Analysis Adapter Pattern (Detailed Example). For more information, please follow other related articles on the PHP Chinese website!