设计模式-装饰模式
来源:互联网 发布:中国制裁朝鲜 知乎 编辑:程序博客网 时间:2024/05/15 15:33
个人理解: 装饰模式,是对原有功能上的一种增强。
以下情况使用Decorator模式
1. 需要扩展一个类的功能,或给一个类添加附加职责。
2. 需要动态的给一个对象添加功能,这些功能可以再动态的撤销。
3. 需要增加由一些基本功能的排列组合而产生的非常大量的功能,从而使继承关系变的不现实。
4. 当不能采用生成子类的方法进行扩充时。一种情况是,可能有大量独立的扩展,为支持每一种组合将产生大量的子类,使得子类数目呈爆炸性增长。另一种情况可能是因为类 定义被隐藏,或类定义不能用于生成子类。
UML图:代码案例:
一个商场收费案例。
IShopCharge: 商场收费计费接口 +charge()
ShopChargeGeneral: 无任何装饰增强,常规计费(该多少就多少)
AShopChargePrivilege: 商场收费,优惠计费抽象类 实现 IShopCharge
// 因为是在原有的计费基础上的装饰增前,所以需要,依赖注入原始计费实现。
-IShopCharge shopCharge;
+setShopCharge(IShopCharge shopCharge)
//重写原有计费接口实现
+charge();
ShopCharegeReturn: 商场收费,优惠返现计费实现类 继承优惠计费抽象类
-double bCost; //返现基准
-double rCost //返现额
+charge() //重写计费实现.
ShopChargeDiscount 商场收费,优惠打折计费实现类 继承优惠计费抽象类
-double discountNum //折扣率
+charge() 重写计费实现
代码:
IShopCharge 接口
<pre name="code" class="html">package com.bo.decorator;public interface IShopCharge {public double charge();}
ShopChargeGeneral 普通计费实现类代码: package com.bo.decorator;/** * 商场正常收费 * @author 波 * */public class ShopChargeGeneral implements IShopCharge {privatedouble money ;private ShopChargeGeneral(){}public ShopChargeGeneral(double money){this.money = money;}@Overridepublic double charge() {// TODO Auto-generated method stubreturn money;}}
AShopChargePrivilege: 优惠计费抽象类package com.bo.decorator;public abstract class AShopChargePrivilege implements IShopCharge {private IShopCharge shopCharge; public final void setShopCharge(IShopCharge shopCharge) {this.shopCharge = shopCharge;}@Overridepublic double charge(){return this.shopCharge.charge();}}
ShopCharegeReturn :返现优惠实现类:package com.bo.decorator;public class ShopCharegeReturn extends AShopChargePrivilege {private double bCost;private double rCost;private ShopCharegeReturn(){}public ShopCharegeReturn(double bCost,double rCost){ this.bCost = bCost; this.rCost = rCost;}@Overridepublic double charge() {// TODO Auto-generated method stubdouble _money = super.charge();return _money-((int)_money/bCost)*rCost;}}
ShopChargeDiscount :打折优惠实现类:package com.bo.decorator;/** * 商场打折消费 * @author 波 * */public class ShopChargeDiscount extends AShopChargePrivilege {private double discountNum ;public ShopChargeDiscount(double discountNum){this.discountNum = discountNum;}private ShopChargeDiscount(){}@Overridepublic double charge() {// TODO Auto-generated method stubdouble _money = super.charge();return _money*this.discountNum;}}
TestMain :测试类package com.bo.decorator;public class TestMain {public static void main(String[] args) {IShopCharge shopCharge = new ShopChargeGeneral(300);double money = shopCharge.charge();System.out.println("常规收费: "+money+"$");//满一百返十块AShopChargePrivilege shopChargePrivilege = new ShopCharegeReturn(100,10);shopChargePrivilege.setShopCharge(shopCharge);money = shopChargePrivilege.charge();System.out.println("返现优惠后,收费: "+money+"$");//打折AShopChargePrivilege shopChargeDiscount = new ShopChargeDiscount(0.8);shopChargeDiscount.setShopCharge(shopCharge);money = shopChargeDiscount.charge();System.out.println("打折优惠后,收费: "+money+"$");}}
输出:常规收费: 300.0$返现优惠后,收费: 270.0$打折优惠后,收费: 240.0$
0 0
- 设计模式---装饰模式
- 设计模式:装饰模式
- 设计模式------装饰模式
- 设计模式--装饰模式
- 设计模式--装饰模式
- 设计模式--装饰模式
- 设计模式 装饰模式
- 设计模式----装饰模式
- 设计模式-【装饰模式】
- 设计模式--装饰模式
- 设计模式装饰模式
- 设计模式 - 装饰模式
- 设计模式-----装饰模式.
- 设计模式-----装饰模式.
- [ 设计模式 ] 装饰模式!
- 设计模式:装饰模式
- 设计模式 - 装饰模式
- 设计模式 - 装饰模式
- 一元多项式化简
- Python安装Pip出现的问题
- OC中声明Block的几种方式
- 64汇编写程序
- 数据结构试验—链表实现数据信息录入
- 设计模式-装饰模式
- swift教程【入门】 使用swift REPL编写第一个swift程序
- SWIFT实现图文混排
- ListView 点击事件
- 多线程-死锁Test
- 改善SQL语句
- java 冒泡排序算法
- setValue和setObject的区别
- 详解c++中类的六个默认的成员函数