Java 装饰器设计模式示例
大家好,如果想了解 Java 中的装饰器设计模式,那么你来对地方了。由于设计模式在构建软件时非常重要,在任何核心 Java 面试中也同样重要,因此清楚地了解 Java 中的各种设计模式总是好的。 在本文中,我们将探索和学习 Java 中的装饰器设计模式,这是一个突出的 Java 核心设计模式,我们可以在 JDK 本身中看到很多它的示例。
JDK 在 IO 包中使用装饰器模式,它为各种场景装饰了 Reader 和 Writer 类,例如 BufferedReader
和 BufferedWriter
是 Java 中装饰器设计模式的示例。
从设 Java 中的类或接口。 现在让我们转到 Java 中的装饰器模式。
顺便说一句,为了最好地理解设计模式,我们需要制定一些场景、示例等。
Java 中的装饰器设计模式
在本篇文章中,我们将看到:
- Java中的装饰器模式是什么?
- 什么时候在 Java 中使用装饰器模式?
- 如何在 Java 中使用装饰器模式?
- 装饰器设计模式示例
- Java中装饰器模式的优缺点
什么是 Java 中的装饰器设计模式?
- 装饰器设计模式用于在运行时或动态地增强特定对象的功能。
- 同时,同一类的其他实例不会受此影响,因此单个对象获得新行为。
- 基本上我们通过装饰器对象包装原始对象。
- 装饰器设计模式基于抽象类,我们从该类派生具体实现,
- 是一种结构型设计模式,应用最广泛。
装饰器设计模式解决了什么问题?
现在的问题是为什么会出现这种模式,现有系统有什么问题需要解决? 所以答案是,如果有人想在运行时向单个对象添加某些功能或更改特定对象的状态,那是不可能的。
可能的是,我们可以在设计时借助继承或使用子类为该类的所有对象提供特定行为,但是装饰器模式使我们可以在运行时为同一类的单个对象提供特定行为或状态。 这不会影响 Java 中同一类的其他对象。
何时在 Java 中使用装饰器模式
当子类化变得不切实际时,我们需要大量不同的可能性来制作独立的对象,或者我们可以说我们有一个对象的组合数量。
其次,当我们想在运行时向单个对象而不是所有对象添加功能时,我们使用装饰器设计模式。
装饰器设计模式的代码示例:
为了更好地理解装饰器设计模式的概念,让我们看一个在 Java 中使用装饰器模式的代码示例。 我们还可以查看 JDK 内部并找到使用装饰器模式的类和包。
public abstract class Currency {
String description = "Unknown currency";
public String getCurrencyDescription() {
return description;
}
public abstract double cost(double value);
}
// Concrete Component
public class Rupee extends Currency {
double value;
public Rupee() {
description = "indian rupees";
}
public double cost(double v) {
value = v;
return value;
}
}
//Another Concrete Component
public class Dollar extends Currency {
double value;
public Dollar() {
description = "Dollar”;
}
public double cost(double v) {
value = v;
return value;
}
}
// Decorator
public abstract class Decorator extends Currency {
public abstract String getDescription();
}
// Concrete Decorator
public class USDDecorator extends Decorator {
Currency currency;
public USDDecorator(Currency currency) {
this.currency = currency;
}
public String getDescription() {
return currency.getDescription() + " ,its US Dollar";
}
}
//Another Concrete Decorator
public class SGDDecorator extends Decorator {
Currency currency;
public SGDDecorator(Currency currency) {
this.currency = currency;
}
public String getDescription() {
return currency.getDescription() + " ,its singapore Dollar";
}
}
public class CurrencyCheck {
public static void main(String[] args) {
// without adding decorators
Currency curr = new Dollar();
System.out.println(curr.getDescription() + " dollar. " + curr.cost(2.0));
//adding decorators
Currency curr2 = new USDDecorator(new Dollar());
System.out.println(curr2.getDescription() + " dollar. " + curr2.cost(4.0));
Currency curr3 = new SGDDecorator(new Dollar());
System.out.println(curr3.getDescription() + " dollar. " + curr3.cost(4.0));
}
}
代码说明:
我们可以用以下术语来理解这一点:
-
组件接口:在我们的示例中,货币接口是一个单独使用的组件,或者我们需要一个装饰器。
-
具体组件:它实现组件,我们动态地向这个对象添加新的行为。 美元和卢比是货币的具体实现。
-
Decorator:Decorator 包含一个 HAS 简单的关系,我们可以说它有一个实例变量,该变量保存对它们实现它们将要装饰的相同组件的组件的引用。 这里的 Decorator 是一个扩展货币的抽象类。
-
Concrete Decorator:它是 Decorator 的一个实现 所以 USD Dollar 和 SGD Dollar 是 Decorator 的实现,包含组件接口或它们要装饰的东西的实例变量。
这是一个很好的 UML 图:
Java中装饰器设计模式的优势
简而言之,我们看到了使用装饰器设计模式的主要优势是什么。
- Decorator Pattern比继承灵活,因为继承在编译时增加了责任,并且会在运行时增加。
- 装饰者模式增强或修改对象功能
装饰者模式的缺点
在 Java 中使用装饰器模式的主要缺点是代码维护可能是一个问题,因为它提供了很多类似的小对象(每个装饰器)。
这就是 Java 中装饰器设计模式的全部内容。 要掌握装饰器模式,我建议查看 JDK 库本身并找出装饰了哪些类,为什么要装饰它们。 另外,考虑一个继承不切实际的场景,你看起来更灵活,并尝试在那里使用 Java 中的装饰器模式。
相关文章
Do you understand JavaScript closures?
发布时间:2025/02/21 浏览次数:108 分类:JavaScript
-
The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.
Do you know about the hidden traps in variables in JavaScript?
发布时间:2025/02/21 浏览次数:178 分类:JavaScript
-
Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av
How much do you know about the Prototype Chain?
发布时间:2025/02/21 浏览次数:150 分类:JavaScript
-
The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start
如何在 JavaScript 中合并两个数组而不出现重复的情况
发布时间:2024/03/23 浏览次数:86 分类:JavaScript
-
本教程介绍了如何在 JavaScript 中合并两个数组,以及如何删除任何重复的数组。