Structual Pattern: Adapter
什么是Adapter?
Convert the existing interfaces to a new interface to achieve compatibility and reusability of the unrelated classes in one application. Also known as Wrapper pattern.
Example:
To reuse classes and make new class compatible with existing ones. For example, A clean system is already designed, you want to add more job in, the Extra interface uses adapter pattern to plug in the existing system.
interface Clean {
public void makeClean();
}
class Office implements Clean{
public void makeClean() {
System.out.println("Clean Office");
}
}
class Workshop implements Clean{
public void makeClean() {
System.out.println("Clean Workshop");
}
}
interface Extra extends Clean{
public void takeCare();
}
class Facility implements Extra{
public void makeClean() {
System.out.println("Clean Facility");
}
public void takeCare() {
System.out.println("Care has been taken");
}
}
In order to reuse Workshop and Office classes,
we create an adapter interface Extra and add new job takeCare in the system.
class Test {
static void Jobs (Extra job) {
if (job instanceof Clean)
((Clean)job).makeClean();
if (job instanceof Extra)
((Extra)job).takeCare();
}
public static void main(String[] args) {
Extra e = new Facility();
Jobs(e);
Clean c1 = new Office();
Clean c2 = new Workshop();
c1.makeClean();
c2.makeClean();
e.makeClean();
}
}
原文:
http://www.javacamp.org/designPattern/
分享到:
相关推荐
Design Patterns Explained: A New Perspective on Object-Oriented Design By Alan Shalloway, James R. Trott Publisher : Addison Wesley Pub Date : July 09, 2001 ISBN : 0-201-...
Learn how to implement design patterns in Java: each pattern in Java Design Patterns is a complete implementation and the output is generated using Eclipse, making the code accessible to all....
Learn how to implement design patterns in Java: each pattern in Java Design Patterns is a complete implementation and the output is generated using Eclipse, making the code accessible to all....
《Design Patterns Explained: A New Perspective on Object Oriented Design》这本书提供了一种新的视角来理解和应用这些模式。以下是关于设计模式的一些关键知识点: 1. **工厂模式**:工厂模式是一种创建型设计...
Microservices Patterns teaches enterprise developers and architects how to build applications with the microservice architecture. Rather than simply advocating for the use the microservice ...
C# Design Patterns: A Tutorial is a practical guide to writing C# programs using the most common patterns. This tutorial begins with clear and concise introductions to C#, object-oriented ...
It starts with a general introduction to all types of programming patterns and goes on to describe 10 of the most popular design patterns in detail: Singleton, Iterator, Adapter, Decorator, State, ...
设计模式:可复用面向对象软件的基础(中文)Design Patterns:Elements of Reusable Object-Oriented software
Design Patterns in Java(2nd) 英文epub 第2版 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
设计模式(结构型):外观模式 外观模式(Facade Pattern)是一种常用的软件设计模式,在面向对象...例如,Java标准库中的Swing组件的高层抽象,就是使用外观模式将底层的AWT组件封装起来,以提供更简单的接口给用户。
本书《Design Patterns for Embedded Systems in C》作为嵌入式软件工程工具箱,作者Bruce Powell Douglass博士基于丰富的嵌入式软件工程经验,详细介绍了在嵌入式系统编程中使用C语言时可以运用的设计模式。...
"Machine Learning Design Patterns" 《Machine Learning Design Patterns》是一本关于机器学习设计模式的书籍,作者是Valliappa Lakshmanan, Sara Robinson和Michael Munn。该书的主要内容是解决数据准备、模型...
design pattern in C# language
Design Patterns in Modern C++: Reusable Approaches for Object-Oriented Software Design English | PDF| 2018 | 312 Pages | ISBN : 1484236025Design Patterns in Modern C++: Reusable Approaches for Object...
Microservices Patterns: With Examples in Java EPUB 版本
It also serves as a practical guide to using design patterns to create sophisticated, robust Java programs., , This book presents the 23 patterns cataloged in the flagship book Design Patterns by ...
在Java的软件架构设计模式中,我们探讨的核心是构建高效、可维护...在阅读《Software Architecture Design Patterns in Java》这本书时,每个章节都应结合提供的代码示例进行深入学习,以便更好地掌握这些模式的精髓。
本资料《Design Patterns Java Companion》由James W. Cooper撰写于1998年10月2日,版权归属于作者本人。书中全面地介绍了23种常用的设计模式,并通过Java底层类进行深入解析。 #### 设计模式定义及其来源 设计...
Data Structures And Algorithms With Object-oriented Design Patterns In Java.chm