【设计模式】—— 装饰模式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++设计模式——装饰模式 前言 在实际开发时,你有没有碰到过这种问题:开发一个类,封装了一个对象的核心操作,而这些操作就是客户使用该类时都会去调用的操作:而有一些非核心的操作,可能会使用,也可能不会 ...
随机推荐
- 原生js switch语句
一.我们在流判断的时候,我们大多数的情况我使用if else 语句.但是对于一些大量的逻辑的判断的时候,我们不建议使用if elseif语句 这种语句的效率执行不高,因为他每个expression ...
- HTML5 <iframe> 标签
iframe 元素会创建包含另外一个文档的内联框架(即行内框架). 即页面中嵌入另外一个独立的页面使用iframe,熟悉src是嵌套的页面的路径地址,scrolling属性可以设置iframe的滚动条 ...
- gitlab webhook php exec 调用 shell 脚本。shell 脚本中调用 git pull 命令无法执行。
情况如下: 我在ubuntu server 14.04 上面安装了gitlab,来托管项目代码.然后想通过gitlab的web hook 功能来做测试服务器代码自动化更新代码功能.现在遇到一个问题:就 ...
- HUE配置HBase
HBase的配置 修改配置hue.ini的配置文件 [hbase] hbase_clusters=(Cluster|node1:) hbase_conf_dir=/usr/hbase-0.98.12. ...
- OO——电梯作业总结
目录 电梯作业总结 程序结构与复杂度的分析 第一次作业 第二次作业 第三次作业 程序BUG的分析 互测 自动评测 有效性 总结 电梯作业总结 程序结构与复杂度的分析 第一次作业 1.设计思路 第一次作 ...
- python之Django实现商城从0到1
dailyfresh-B2Cdailyfresh mall based on B2C model 基于B2C的天天生鲜商城 项目托管地址:https://github.com/Ylisen/daily ...
- C# Test Encryption and Decryption
public MainWindow() { InitializeComponent(); Title = getUUID(); string s= httpGet("http://220.1 ...
- vb用createprocess启动其他应用程序
Option Explicit Private Type PROCESS_INFORMATION hProcess As Long hThread As Long dwProcessId As Lon ...
- 20155204《网络对抗》Exp9 Web安全基础实践
20155204<网络对抗>Exp9 Web安全基础实践 一.基础问题回答 SQL注入攻击原理,如何防御? 原理: SQL注入即是指web应用程序对用户输入数据的合法性没有判断,攻击者可以 ...
- 20155227《网络对抗》Exp8 Web基础
20155227<网络对抗>Exp8 Web基础 实验内容 (1)Web前端HTML (2)Web前端javascipt (3)Web后端:MySQL基础:正常安装.启动MySQL,建库. ...