【设计模式】—— 装饰模式Decorator
前言:【模式总览】——————————by xingoo
模式意图
在不改变原来类的情况下,进行扩展。
动态的给对象增加一个业务功能,就功能来说,比生成子类更方便。
应用场景
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;
}
}
全部代码
package com.xingoo.decorator;
interface Component{
public void operation();
}
class ConcreteComponent implements Component{ @Override
public void operation() {
// TODO Auto-generated method stub
System.out.println("ConcreteComponent 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;
} } public class test {
public static void main(String[] args) {
Component component = new Decorator(new ConcreteComponent());
component.operation();
}
}
运行结果
before decorator!
ConcreteComponent operation()
after decorator!
【设计模式】—— 装饰模式Decorator的更多相关文章
- 设计模式 装饰模式(Decorator)
设计模式 装饰模式(Decorator) @author ixenos 装饰模式是什么 1.装饰模式以对客户端透明的方式对象的功能,是继承关系的一个替代方案,但装饰模式可以在不创造更多子类的情况下,对 ...
- 设计模式-装饰模式(Decorator Pattern)
装饰模式(Decorator Pattern):动态地给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更为灵活
- [工作中的设计模式]装饰模式decorator
一.模式解析 装饰模式又名包装(Wrapper)模式.装饰模式以对客户端透明的方式扩展对象的功能,是继承关系的一个替代方案. 装饰模式的要点主要是: 1.需要对已有对象扩展新的功能,又不希望改变原有对 ...
- 设计模式——装饰模式(Decorator Pattern)
装饰模式:动态的给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更为灵活. UML图: 模型类: Component类: package com.cnblog.clarck; /** ...
- 乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)
原文:乐在其中设计模式(C#) - 装饰模式(Decorator Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 装饰模式(Decorator Pattern) 作者:weba ...
- 设计模式系列之装饰模式(Decorator Pattern)——扩展系统功能
说明:设计模式系列文章是读刘伟所著<设计模式的艺术之道(软件开发人员内功修炼之道)>一书的阅读笔记.个人感觉这本书讲的不错,有兴趣推荐读一读.详细内容也可以看看此书作者的博客https:/ ...
- 深入浅出设计模式——装饰模式(Decorator Pattern)
模式动机 一般有两种方式可以实现给一个类或对象增加行为: 继承机制,使用继承机制是给现有类添加功能的一种有效途径,通过继承一个现有类可以使得子类在拥有自身方法的同时还拥有父类的方法.但是这种方法是静 ...
- 二十四种设计模式:装饰模式(Decorator Pattern)
装饰模式(Decorator Pattern) 介绍动态地给一个对象添加一些额外的职责.就扩展功能而言,它比生成子类方式更为灵活.示例有一个Message实体类,某个对象对它的操作有Insert()和 ...
- 设计模式-09装饰模式(Decorator Pattern)
1.模式动机 一般有两种方式可以实现给一个类或对象增加行为: 继承机制:使用继承机制是给现有类添加功能的一种有效途径,通过继承一个现有类可以使得子类在拥有自身方法的同时还拥有父类的方法.但是这种方法是 ...
- 装饰模式decorator
C++设计模式——装饰模式 前言 在实际开发时,你有没有碰到过这种问题:开发一个类,封装了一个对象的核心操作,而这些操作就是客户使用该类时都会去调用的操作:而有一些非核心的操作,可能会使用,也可能不会 ...
随机推荐
- Kafka设计解析(十九)Kafka consumer group位移重设
转载自 huxihx,原文链接 Kafka consumer group位移重设 本文阐述如何使用Kafka自带的kafka-consumer-groups.sh脚本随意设置消费者组(consumer ...
- Android 对BaseAdapter做优化处理
对于BaseAdapter相信大家都不陌生,都知道该怎样用.怎样显示数据.怎样尽可能的把每个item做的令自己满意.但问题来了:有些朋友会说我界面做的非常的漂亮,数据也显示的非常完美,但是问什么我的L ...
- Android 给CheckBox设置背景
一般来说我们给控件(Button,LinearLayout,ImageView,TextView等)设这背景的时候只需要设置这些控件的android:background即可, 但是在给CheckBo ...
- HDU 1827 Summer Holiday(tarjan求强连通分量+缩点构成新图+统计入度+一点贪心思)经典缩点入门题
Summer Holiday Time Limit: 10000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- storm报错:Exception in thread "main" java.lang.RuntimeException: InvalidTopologyException(msg:Component: [mybolt] subscribes from non-existent stream: [default] of component [kafka_spout])
问题描述: storm版本:1.2.2,kafka版本:2.11. 在使用storm去消费kafka中的数据时,发生了如下错误. [root@node01 jars]# /opt/storm-1. ...
- Exp3
利用不同免杀方式生成文件 1.msfvenom 使用msfvenom命令查看功能介绍 其中有: -p 选择一个载荷(或者叫模块) -l 载荷列表 -f 生成的文件格式 -e 编码方式 -l 编码次数 ...
- MFC 用ShellExecute打开外部文件
知识点: 获取CListCtrl选中文本 用ShellExecute打开外部文件 一.CListCtrl::GetFirstSelectedItemPosition CListCtrl::GetFir ...
- python 井字棋(Tic Tac Toe)
说明 用python实现了井字棋,整个框架是本人自己构思的,自认为比较满意.另外,90%+的代码也是本人逐字逐句敲的. minimax算法还没完全理解,所以参考了这里的代码,并作了修改. 特点 可以选 ...
- FormData 数据转化为 json 数据
两种方法 <!-- 实例:将 FormData 转化为 json --> <meta charset="utf-8"/> <form enctype= ...
- 【HNOI2017】礼物
题面 题解 显然两个手环只需要一个的亮度增加\(c \in [-m, m]\)和原题是等价的. 于是可以写成这样一个公式: \[ \sum_{i = 1} ^ n(x_i - y_{i+k} + c) ...