Analysis of Java Design Patterns: How to Implement the Adapter Pattern
1. What is the adapter pattern:
The adapter pattern is mainly used to convert the interface of a class into what the client wants. The target class format allows originally incompatible classes to work together, decoupling the target class and the adapter class; it also conforms to the "opening and closing principle", and new adapter classes can be added 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. However, the disadvantage is that the implementation process of replacing the adapter is relatively complicated.
So, the adapter mode is more suitable for the following scenarios:
(1) The system needs to use existing classes, and the interfaces of these classes do not conform to the system interfaces.
(2) When using a third-party component, the component interface definition is different from your own definition. You do not want to modify your own interface, but you must use the functions of the third-party component interface.
The following two very vivid examples illustrate well what the adapter pattern is:
2. Three ways to implement the adapter pattern:
The adapter pattern is mainly divided into three categories: class adapter pattern, object adapter pattern, and interface adapter pattern.
1. Class adapter pattern:
Run result:
- ##Goal Interface (Target): The interface that customers expect. The target can be a concrete or abstract class, or an interface.
- Class that needs to be adapted (Adaptee): The class that needs to be adapted or the adapter class.
- Adapter: Converts 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
普通类 具有 普通功能... 被适配类具有 特殊功能...
2. Object adapter mode:
// 适配器类,直接关联被适配类,同时实现标准接口 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(); } }
3. Adapter mode of interface:
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:public interface Sourceable { public void method1(); public void method2(); }
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(); } }
the sourceable interface's first Sub1! the sourceable interface's second Sub2!
The above is the detailed content of Analysis of Java Design Patterns: How to Implement the Adapter Pattern. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Guide to Random Number Generator in Java. Here we discuss Functions in Java with examples and two different Generators with ther examples.

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.
