装饰模式

动态的将责任附加到对象上。若要扩展功能,装饰者提供了比继承更有弹性的取代方案

代码

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)的更多相关文章

  1. 装饰模式/decorator模式/结构型模式

    装饰模式Decorator 定义 为对象动态的增加新的功能,实现要求装饰对象和被装饰对象实现同一接口或抽象类,装饰对象持有被装饰对象的实例. java实现要点 定义一个接口或抽象类,作为被装饰者的抽象 ...

  2. Netty学习-IO体系架构系统回顾 & 装饰模式Decorator的具体使用

    Netty学习-IO体系架构系统回顾 IO和NIO的学习 NIO - 1.4 开始出的 在网络应用框架中,NIO得到了大量的使用,特别是netty里面 前提:对IO及其了解 对IO的总结和回顾 理解J ...

  3. 设计模式系列之装饰模式(Decorator Pattern)——扩展系统功能

    说明:设计模式系列文章是读刘伟所著<设计模式的艺术之道(软件开发人员内功修炼之道)>一书的阅读笔记.个人感觉这本书讲的不错,有兴趣推荐读一读.详细内容也可以看看此书作者的博客https:/ ...

  4. 二十四种设计模式:装饰模式(Decorator Pattern)

    装饰模式(Decorator Pattern) 介绍动态地给一个对象添加一些额外的职责.就扩展功能而言,它比生成子类方式更为灵活.示例有一个Message实体类,某个对象对它的操作有Insert()和 ...

  5. 乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)

    原文:乐在其中设计模式(C#) - 装饰模式(Decorator Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 装饰模式(Decorator Pattern) 作者:weba ...

  6. 设计模式 装饰模式(Decorator)

    设计模式 装饰模式(Decorator) @author ixenos 装饰模式是什么 1.装饰模式以对客户端透明的方式对象的功能,是继承关系的一个替代方案,但装饰模式可以在不创造更多子类的情况下,对 ...

  7. 设计模式-装饰模式(Decorator Pattern)

    装饰模式(Decorator Pattern):动态地给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更为灵活

  8. Java 装饰模式(4.4)

    装饰模式(decorator pattern). 依照Num模型.讨论职业/IProfession类层次. IProfession定义了方法say(String),事实上现类教师/ Teacher.医 ...

  9. 设计模式-09装饰模式(Decorator Pattern)

    1.模式动机 一般有两种方式可以实现给一个类或对象增加行为: 继承机制:使用继承机制是给现有类添加功能的一种有效途径,通过继承一个现有类可以使得子类在拥有自身方法的同时还拥有父类的方法.但是这种方法是 ...

  10. 《JAVA设计模式》之装饰模式(Decorator)

    在阎宏博士的<JAVA与模式>一书中开头是这样描述装饰(Decorator)模式的: 装饰模式又名包装(Wrapper)模式.装饰模式以对客户端透明的方式扩展对象的功能,是继承关系的一个替 ...

随机推荐

  1. iOS 获取已连接的wifi信息

    转:http://blog.csdn.net/marujunyy/article/details/16843173 首先需要   #import <SystemConfiguration/Cap ...

  2. 基于Dubbo框架构建分布式服务

    Dubbo是Alibaba开源的分布式服务框架,我们可以非常容易地通过Dubbo来构建分布式服务,并根据自己实际业务应用场景来选择合适的集群容错模式,这个对于很多应用都是迫切希望的,只需要通过简单的配 ...

  3. HDU 5278 PowMod 数论公式推导

    题意:中文题自己看吧 分析:这题分两步 第一步:利用已知公式求出k: 第二步:求出k然后使用欧拉降幂公式即可,欧拉降幂公式不需要互质(第二步就是BZOJ3884原题了) 求k的话就需要构造了(引入官方 ...

  4. ADO.NET 中的数据并发

    当多个用户试图同时修改数据时,需要建立控制机制来防止一个用户的修改对同时操作的其他用户所作的修改产生不利的影响.处理这种情况的系统叫做“并发控制”.并发控制的类型通常,管理数据库中的并发有三种常见的方 ...

  5. leetcode—3sum

    1.题目描述 Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find ...

  6. 回车跳到下一个EDIT

    1.按下方法procedure TForm2.Edit1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);begin if Key ...

  7. Red5下的room

    http://blog.csdn.net/whycold/article/details/6142475 package com.test; import java.util.ArrayList;im ...

  8. Red5源代码分析 - 关键类及其初始化过程

    原文地址:http://semi-sleep.javaeye.com/blog/348768 Red5如何响应rmpt的请求,中间涉及哪些关键类? 响应请求的流程如下: 1.Red5在启动时会调用RT ...

  9. UVALive 7457 Discrete Logarithm Problem (暴力枚举)

    Discrete Logarithm Problem 题目链接: http://acm.hust.edu.cn/vjudge/contest/127401#problem/D Description ...

  10. HDU 5778 abs (枚举)

    abs 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5778 Description Given a number x, ask positive ...