Java的装饰器设计模式示例
装饰器设计模式允许我们向对象动态添加功能和行为,而不会影响同一类中其他现有对象的行为。我们使用继承来扩展类的行为。这发生在编译时,并且该类的所有实例都获得扩展行为。
- 装饰器模式允许用户在不改变其结构的情况下向现有对象添加新功能。因此,原始类没有变化。
- 装饰器设计模式是一种结构模式,它为现有类提供包装器。
- 装饰器设计模式使用抽象类或接口与组合来实现包装器。
- 装饰器设计模式创建装饰器类,这些类包装原始类并通过保持类方法的签名不变来提供附加功能。
- 装饰器设计模式最常用于应用单一职责原则,因为我们将功能划分为具有独特关注领域的类。
- 装饰器设计模式在结构上几乎类似于责任链模式。
Key points to remember
- Decorator design pattern is useful in providing runtime modification abilities and hence more flexible. Its easy to maintain and extend when the amount of choices are more.
- The disadvantage of decorator design pattern is that it uses plenty of similar kind of objects (decorators)
- Decorator pattern is used a lot in Java IO classes, like FileReader, BufferedReader, etc.
程序:
- 创建一个接口。
- 创建实现相同接口的具体类。
- 创建一个实现上述相同接口的抽象装饰器类。
- 创建一个扩展上述抽象装饰器类的具体装饰器类。
- 现在使用上面创建的具体装饰器类来装饰界面对象。
- 最后,验证输出
执行:
我们将创建一个 Shape 接口和实现 Shape 接口的具体类。然后我们将创建一个抽象装饰器类 ShapeDecorator 实现 Shape 接口并将 Shape 对象作为其实例变量。
- 'Shape' 是接口的名称
- “Rectangle”类和“Circle”类将是实现“Shape”接口的具体类。
- “ShapeDecorator”是我们的抽象装饰器类,实现了相同的“Shape”接口。
- RedShapeDecorator 是一个实现 ShapeDecorator 的具体类。
- DecoratorPatternDemo,我们的演示类将使用 RedShapeDecorator 来装饰 Shape 对象。
第 1 步:创建一个名为“Shape”的界面
例子
Java
// Interface named Shape
// Shape.java
public interface Shape {
void draw();
}
Java
// Class 1
// Class 1 will be implementing the Shape interface
// Rectangle.java
public class Rectangle implements Shape {
// Overriding the method
@Override public void draw()
{
// /Print statement to execute when
// draw() method of this class is called
// later on in the main() method
System.out.println("Shape: Rectangle");
}
}
Java
// Circle.java
public class Circle implements Shape {
@Override
public void draw()
{
System.out.println("Shape: Circle");
}
}
Java
// Class 2
// Abstract class
// ShapeDecorator.java
public abstract class ShapeDecorator implements Shape {
// Protected variable
protected Shape decoratedShape;
// Method 1
// Abstract class method
public ShapeDecorator(Shape decoratedShape)
{
// This keywordd refers to current object itself
this.decoratedShape = decoratedShape;
}
// Method 2 - draw()
// Outside abstract class
public void draw() { decoratedShape.draw(); }
}
Java
// Class 3
// Concrete class extending the abstract class
// RedShapeDecorator.java
public class RedShapeDecorator extends ShapeDecorator {
public RedShapeDecorator(Shape decoratedShape)
{
super(decoratedShape);
}
@Override public void draw()
{
decoratedShape.draw();
setRedBorder(decoratedShape);
}
private void setRedBorder(Shape decoratedShape)
{
// Display message whenever function is called
System.out.println("Border Color: Red");
}
}
Java
// DecoratorPatternDemo.java
// Class
// Main class
public class DecoratorPatternDemo {
// Main driver method
public static void main(String[] args)
{
// Creating an object of Shape interface
// inside the main() method
Shape circle = new Circle();
Shape redCircle
= new RedShapeDecorator(new Circle());
Shape redRectangle
= new RedShapeDecorator(new Rectangle());
// Display message
System.out.println("Circle with normal border");
// Calling the draw method over the
// object calls as created in
// above classes
// Call 1
circle.draw();
// Display message
System.out.println("\nCircle of red border");
// Call 2
redCircle.draw();
// Display messsage
System.out.println("\nRectangle of red border");
// Call 3
redRectangle.draw();
}
}
第 2 步:创建实现相同接口的具体类。长方形。 Java和圆。 Java如下
例子
Java
// Class 1
// Class 1 will be implementing the Shape interface
// Rectangle.java
public class Rectangle implements Shape {
// Overriding the method
@Override public void draw()
{
// /Print statement to execute when
// draw() method of this class is called
// later on in the main() method
System.out.println("Shape: Rectangle");
}
}
Java
// Circle.java
public class Circle implements Shape {
@Override
public void draw()
{
System.out.println("Shape: Circle");
}
}
第 3 步:创建一个实现 Shape 接口的抽象装饰器类。
例子
Java
// Class 2
// Abstract class
// ShapeDecorator.java
public abstract class ShapeDecorator implements Shape {
// Protected variable
protected Shape decoratedShape;
// Method 1
// Abstract class method
public ShapeDecorator(Shape decoratedShape)
{
// This keywordd refers to current object itself
this.decoratedShape = decoratedShape;
}
// Method 2 - draw()
// Outside abstract class
public void draw() { decoratedShape.draw(); }
}
第 4 步:创建一个扩展 ShapeDecorator 类的具体装饰器类。
例子
Java
// Class 3
// Concrete class extending the abstract class
// RedShapeDecorator.java
public class RedShapeDecorator extends ShapeDecorator {
public RedShapeDecorator(Shape decoratedShape)
{
super(decoratedShape);
}
@Override public void draw()
{
decoratedShape.draw();
setRedBorder(decoratedShape);
}
private void setRedBorder(Shape decoratedShape)
{
// Display message whenever function is called
System.out.println("Border Color: Red");
}
}
第 5 步:使用 RedShapeDecorator 来装饰 Shape 对象。
例子
Java
// DecoratorPatternDemo.java
// Class
// Main class
public class DecoratorPatternDemo {
// Main driver method
public static void main(String[] args)
{
// Creating an object of Shape interface
// inside the main() method
Shape circle = new Circle();
Shape redCircle
= new RedShapeDecorator(new Circle());
Shape redRectangle
= new RedShapeDecorator(new Rectangle());
// Display message
System.out.println("Circle with normal border");
// Calling the draw method over the
// object calls as created in
// above classes
// Call 1
circle.draw();
// Display message
System.out.println("\nCircle of red border");
// Call 2
redCircle.draw();
// Display messsage
System.out.println("\nRectangle of red border");
// Call 3
redRectangle.draw();
}
}
步骤 6:验证输出
输出:
Circle with normal border
Shape: Circle
Circle of red border
Shape: Circle
Border Color: Red
Rectangle of red border
Shape: Rectangle
输出说明:
Glancing at the decorator design pattern one can conclude out that this is often a decent choice in the following cases where
- When we wish to add, enhance or perhaps remove the behavior or state of objects
- When we just want to modify the functionality of a single object of the class and leave others unchanged