Java 装饰模式 (Decorator)
装饰模式
动态的将责任附加到对象上。若要扩展功能,装饰者提供了比继承更有弹性的取代方案
代码
package gx.component;
/**
 * 组件:装饰类和被装饰类 都要继承:为了类型保持一致
 * @author always
 *
 */
public abstract class Component {
    public abstract void description();
    public abstract int cost();
}
package gx.component.impl;
import gx.component.Component;
/**
 * 装饰类的抽象类
 * @author always
 *
 */
public abstract class  Decorator extends Component{
    protected Component component;
}
package gx.component.impl;
import gx.component.Component;
/**
 *
 * 被包装的类
 * @author always
 *
 */
public class Phone extends Component{
    public void description() {
        System.out.println("裸机");
    }
    public int cost() {
        return 1900;
    }
}
package gx.decorator.impl;
import gx.component.Component;
import gx.component.impl.Decorator;
/**
 *
 * 装饰1:给手机买个壳
 * @author always
 *
 */
public class DaiKe extends Decorator{
    public DaiKe(){
    }
    public DaiKe(Component component){
        this.component=component;
    }
    public void description() {
        this.component.description();
        System.out.println("带了手机壳");
    }
    public int cost() {
        return 50+this.component.cost();
    }
}
package gx.decorator.impl;
import gx.component.Component;
import gx.component.impl.Decorator;
/**
 *
 * 装饰2:给手机贴个膜
 * @author always
 *
 */
public class TieMo extends Decorator{
    public TieMo(){}
    public TieMo(Component component){
        this.component=component;
    }
    public void description() {
        this.component.description();
        System.out.println("贴了膜");
    }
    public int cost() {
        return 20+this.component.cost();
    }
}
測试类:
package gx;
import gx.component.Component;
import gx.component.impl.Phone;
import gx.decorator.impl.DaiKe;
import gx.decorator.impl.TieMo;
import junit.framework.TestCase;
public class TestDecorator extends TestCase {
    public void testDecorator() {
        Component component = new TieMo(new DaiKe(new Phone()));
        component.description();
        System.out.println("价钱:" + component.cost());
        /*
         * 结果:
         *  裸机
         *  带了手机壳
         *  贴了膜
         *  价钱:1970
         */
    }
}
Java 装饰模式 (Decorator)的更多相关文章
- 装饰模式/decorator模式/结构型模式
		
装饰模式Decorator 定义 为对象动态的增加新的功能,实现要求装饰对象和被装饰对象实现同一接口或抽象类,装饰对象持有被装饰对象的实例. java实现要点 定义一个接口或抽象类,作为被装饰者的抽象 ...
 - Netty学习-IO体系架构系统回顾 & 装饰模式Decorator的具体使用
		
Netty学习-IO体系架构系统回顾 IO和NIO的学习 NIO - 1.4 开始出的 在网络应用框架中,NIO得到了大量的使用,特别是netty里面 前提:对IO及其了解 对IO的总结和回顾 理解J ...
 - 设计模式系列之装饰模式(Decorator Pattern)——扩展系统功能
		
说明:设计模式系列文章是读刘伟所著<设计模式的艺术之道(软件开发人员内功修炼之道)>一书的阅读笔记.个人感觉这本书讲的不错,有兴趣推荐读一读.详细内容也可以看看此书作者的博客https:/ ...
 - 二十四种设计模式:装饰模式(Decorator Pattern)
		
装饰模式(Decorator Pattern) 介绍动态地给一个对象添加一些额外的职责.就扩展功能而言,它比生成子类方式更为灵活.示例有一个Message实体类,某个对象对它的操作有Insert()和 ...
 - 乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)
		
原文:乐在其中设计模式(C#) - 装饰模式(Decorator Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 装饰模式(Decorator Pattern) 作者:weba ...
 - 设计模式 装饰模式(Decorator)
		
设计模式 装饰模式(Decorator) @author ixenos 装饰模式是什么 1.装饰模式以对客户端透明的方式对象的功能,是继承关系的一个替代方案,但装饰模式可以在不创造更多子类的情况下,对 ...
 - 设计模式-装饰模式(Decorator Pattern)
		
装饰模式(Decorator Pattern):动态地给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更为灵活
 - Java 装饰模式(4.4)
		
装饰模式(decorator pattern). 依照Num模型.讨论职业/IProfession类层次. IProfession定义了方法say(String),事实上现类教师/ Teacher.医 ...
 - 设计模式-09装饰模式(Decorator Pattern)
		
1.模式动机 一般有两种方式可以实现给一个类或对象增加行为: 继承机制:使用继承机制是给现有类添加功能的一种有效途径,通过继承一个现有类可以使得子类在拥有自身方法的同时还拥有父类的方法.但是这种方法是 ...
 - 《JAVA设计模式》之装饰模式(Decorator)
		
在阎宏博士的<JAVA与模式>一书中开头是这样描述装饰(Decorator)模式的: 装饰模式又名包装(Wrapper)模式.装饰模式以对客户端透明的方式扩展对象的功能,是继承关系的一个替 ...
 
随机推荐
- 在英文 sql2005中 比较nvarchar 与 varchar的速度
			
declare @str1 varchar(max); declare @count int; ; print 'begin' begin set @str1 = @str1 + '*'; ; end ...
 - e2e 自动化集成测试 架构  实例 WebStorm Node.js Mocha WebDriverIO Selenium Step by step (六) 自动化测试结构小节
			
上一篇‘e2e 自动化集成测试 架构 京东 商品搜索 实例 WebStorm Node.js Mocha WebDriverIO Selenium Step by step (五) 如何让窗体记录登录 ...
 - 【跟我一起学Python吧】python with statement 进阶理解
			
由于之前有一个项目老是要打开文件,然后用pickle.load(file),再处理...最后要关闭文件,所以觉得有点繁琐,代码也不简洁.所以向python with statement寻求解决方法.以 ...
 - 安装CPqD/ofdissector遭遇的错误
			
为了安装支持openflow1.3的wireshark插件,在下载了ofdissector.git,并进入了其src目录后,执行scons install,出现如下错误: util/FieldMana ...
 - HDU-4726 Kia's Calculation 贪心
			
题目链接:http://acm.hdu.edu.cn/userstatus.php?user=zhsl 题意:给两个大数,他们之间的加法法则每位相加不进位.现在可以对两个大数的每位重新排序,但是首位不 ...
 - google_protobuf数据类型
			
要通信,必须有协议,否则双方无法理解对方的码流.在protobuf中,协议是由一系列的消息组成的.因此最重要的就是定义通信时使用到的消息格式. Protobuf消息定义 消息由至少一个字段组合而成,类 ...
 - 为Delphi程序增加UAC功能(管理员身份运行exe)
			
相关资料:http://bbs.csdn.net/topics/320071356# 操作方法: 在Source\VCL目录下应该有这样两个文件sample.manifest和WindowsXP.rc ...
 - HDU 2425 DNA repair (AC自动机+DP)
			
DNA repair Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
 - [iOS微博项目 - 1.5] - NavigationBar标题按钮
			
A.NavigationBar标题按钮 1.需求 在“首页”的导航栏中部设置一个“首页”文字+箭头按钮 统一设置样式 根据实际文本长度调整宽度 消除系统自带的点击高亮效果 点击按钮,箭头上下颠倒 gi ...
 - LCA算法
			
LCA算法: LCA(Least Common Ancestor),顾名思义,是指在一棵树中,距离两个点最近的两者的公共节点.也就是说,在两个点通往根的道路上,肯定会有公共的节点,我们就是要求找到公共 ...