【设计模式】—— 装饰模式Decorator

简介:

  模式意图

  在不改变原来类的情况下,进行扩展。

  动态的给对象增加一个业务功能,就功能来说,比生成子类更方便。

  应用场景

  1 在不生成子类的情况下,为对象动态的添加某些操作。

  2 处理一些可以撤销的职责。

  3 当不能使用生成子类来扩充时。

  模式结构

  Component 外部接口,用于定义外部调用的形式。提供默认的处理方法。

interface Component{
     public void operation();
 }

 

  ConcreteComponent  具体的处理类,用于实现operation方法。

复制代码
class ConcreteComponent implements Component{

    @Override
    public void operation() {
        // TODO Auto-generated method stub
        System.out.println("ConcreteComponent operation()");
    }
    
}
复制代码

  Decorator 装饰类,内部关联一个component对象,调用其operation方法,并添加自己的业务操作。

复制代码
class Decorator implements Component{
    private Component component;
    @Override
    public void operation() {
        // TODO Auto-generated method stub
        System.out.println("before decorator!");
        component.operation();
        System.out.println("after decorator!");
    }
    public Decorator() {
        // TODO Auto-generated constructor stub
    }
    public Decorator(Component component){
        this.component = component;
    }
    
}
复制代码

  全部代码

复制代码
 1 package com.xingoo.decorator;
 2 interface Component{
 3     public void operation();
 4 }
 5 class ConcreteComponent implements Component{
 6 
 7     @Override
 8     public void operation() {
 9         // TODO Auto-generated method stub
10         System.out.println("ConcreteComponent operation()");
11     }
12     
13 }
14 class Decorator implements Component{
15     private Component component;
16     @Override
17     public void operation() {
18         // TODO Auto-generated method stub
19         System.out.println("before decorator!");
20         component.operation();
21         System.out.println("after decorator!");
22     }
23     public Decorator() {
24         // TODO Auto-generated constructor stub
25     }
26     public Decorator(Component component){
27         this.component = component;
28     }
29     
30 }
31 
32 
33 public class test {
34     public static void main(String[] args) {
35         Component component = new Decorator(new ConcreteComponent());
36         component.operation();
37     }
38 }
复制代码

  运行结果

before decorator!
ConcreteComponent operation()
after decorator!

 

本文转自博客园xingoo的博客,原文链接:【设计模式】—— 装饰模式Decorator,如需转载请自行联系原博主。
相关文章
|
16天前
|
设计模式 PHP
php设计模式--装饰模式(七)装饰模式完成文章编辑
php设计模式--装饰模式(七)装饰模式完成文章编辑
10 0
|
3月前
|
设计模式 中间件 PHP
设计模式 | 装饰模式
设计模式 | 装饰模式
15 0
|
5月前
|
设计模式
设计模式系列教程(12) - 装饰模式
设计模式系列教程(12) - 装饰模式
16 0
|
6月前
|
设计模式 算法 uml
结构型设计模式01-装饰模式
结构型设计模式01-装饰模式
15 0
|
6月前
|
设计模式
设计模式13 - 装饰模式【Decorator Pattern】
设计模式13 - 装饰模式【Decorator Pattern】
15 0
|
1天前
|
设计模式 Go
[设计模式 Go实现] 结构型~装饰模式
[设计模式 Go实现] 结构型~装饰模式
|
4月前
|
设计模式 人工智能 移动开发
认真学习设计模式之装饰者模式(Decorator Pattern)
写在前言 利用继承设计子类的行为,是在编译时静态决定的,而且所有的子类都会继承到相同的行为。然而如果能够利用组合的做法扩展对象的行为,就可以在运行时动态地进行扩展。通过动态地组合对象,可以写新的代码添加新功能,而无须修改现有代码。既然没有改变现有代码,那么引进bug或产生意外副作用的机会将大幅度减少。
16 0
|
4月前
|
设计模式 容器
设计模式之装饰模式(2)--有意思的想法
设计模式之装饰模式(2)--有意思的想法
|
4月前
|
设计模式
设计模式之装饰模式--优雅的增强
设计模式之装饰模式--优雅的增强
设计模式之装饰模式--优雅的增强
|
6月前
|
设计模式 Java 数据库连接
JAVA设计模式8:装饰模式,动态地将责任附加到对象上,扩展对象的功能
JAVA设计模式8:装饰模式,动态地将责任附加到对象上,扩展对象的功能