博客
关于我
装饰器模式
阅读量:209 次
发布时间:2019-02-28

本文共 2211 字,大约阅读时间需要 7 分钟。

装饰器模式(Decorator Pattern)是一种常用的设计模式,主要用于在不修改原有类的结构的情况下,向对象动态添加新的功能。这种模式通过包装原有对象,实现对功能的扩展,使得类之间的耦合度降低,系统更加灵活和可维护。

1. 模式概述

装饰器模式的核心思想是通过创建一个装饰类,包围原有的类,向其添加新的功能。这种方式可以在不改变原有类结构的情况下,增加新的行为。装饰器模式的主要应用场景包括功能扩展、动态添加职责以及需要对现有功能进行额外处理的情况。

2. 实现步骤

要实现装饰器模式,可以按照以下步骤进行:

2.1 创建接口

首先,定义一个通用接口Shape,用于规范所有图形的基本行为。

public interface Shape {
void draw();
}

2.2 实现接口的具体类

接下来,实现接口Shape的具体类,如RectangleCircle

public class Rectangle implements Shape {
@Override
public void draw() {
System.out.println("Shape: Rectangle");
}
}
public class Circle implements Shape {
@Override
public void draw() {
System.out.println("Shape: Circle");
}
}

2.3 创建抽象装饰类

然后,创建一个抽象装饰类ShapeDecorator,继承Shape接口,并包含一个引用用于装饰的目标对象。

public abstract class ShapeDecorator implements Shape {
protected Shape decoratedShape;
public ShapeDecorator(Shape decoratedShape) {
this.decoratedShape = decoratedShape;
}
public void draw() {
decoratedShape.draw();
}
}

2.4 实现具体装饰类

基于ShapeDecorator,创建具体的装饰类,如RedShapeDecorator,实现额外的功能。

public class RedShapeDecorator extends ShapeDecorator {
public RedShapeDecorator(Shape decoratedShape) {
super(decoratedShape);
}
@Override
public void draw() {
decoratedShape.draw();
setRedBorder(decoratedShape);
}
private void setRedBorder(Shape decoratedShape) {
System.out.println("Border Color: Red");
}
}

2.5 使用装饰器

最后,使用RedShapeDecorator来装饰具体的图形对象。

public class DecoratorPatternDemo {
public static void main(String[] args) {
Shape circle = new Circle();
Shape redCircle = new RedShapeDecorator(circle);
Shape rectangle = new Rectangle();
Shape redRectangle = new RedShapeDecorator(rectangle);
System.out.println("Circle with normal border");
circle.draw();
System.out.println("\nCircle of red border");
redCircle.draw();
System.out.println("\nRectangle of red border");
redRectangle.draw();
}
}

3. 优点与缺点

  • 优点:装饰器模式能够在不修改原有类的情况下,动态地增加功能。装饰类和被装饰类可以独立开发和维护,系统结构更加灵活。
  • 缺点:当需要多次装饰时,可能会导致逻辑复杂度增加,难以调试和维护。

4. 应用场景

  • 需要在不修改现有类的情况下扩展功能。
  • 需要对现有功能进行额外处理或包装。

通过以上步骤,可以清晰地看到装饰器模式的应用场景和实现方式。这种模式在软件设计中广泛应用,能够有效提升系统的灵活性和可维护性。

转载地址:http://nfes.baihongyu.com/

你可能感兴趣的文章
MYSQL:基础——触发器
查看>>
Mysql:连接报错“closing inbound before receiving peer‘s close_notify”
查看>>
mysqlbinlog报错unknown variable ‘default-character-set=utf8mb4‘
查看>>
mysqldump 参数--lock-tables浅析
查看>>
mysqldump 导出中文乱码
查看>>
mysqldump 导出数据库中每张表的前n条
查看>>
mysqldump: Got error: 1044: Access denied for user ‘xx’@’xx’ to database ‘xx’ when using LOCK TABLES
查看>>
Mysqldump参数大全(参数来源于mysql5.5.19源码)
查看>>
mysqldump备份时忽略某些表
查看>>
mysqldump实现数据备份及灾难恢复
查看>>
mysqldump数据库备份无法进行操作只能查询 --single-transaction
查看>>
mysqldump的一些用法
查看>>
mysqli
查看>>
MySQLIntegrityConstraintViolationException异常处理
查看>>
mysqlreport分析工具详解
查看>>
MySQLSyntaxErrorException: Unknown error 1146和SQLSyntaxErrorException: Unknown error 1146
查看>>
Mysql_Postgresql中_geometry数据操作_st_astext_GeomFromEWKT函数_在java中转换geometry的16进制数据---PostgreSQL工作笔记007
查看>>
mysql_real_connect 参数注意
查看>>
mysql_secure_installation初始化数据库报Access denied
查看>>
MySQL_西安11月销售昨日未上架的产品_20161212
查看>>