博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
设计模式10-装饰模式
阅读量:4596 次
发布时间:2019-06-09

本文共 6153 字,大约阅读时间需要 20 分钟。

1. 概念

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

2. 案例

/********************************************************************** * 
 * FILE : Demo01.java * CLASS : Demo01 * * AUTHOR : Liaokailin * * FUNCTION : TODO * * *====================================================================== * CHANGE HISTORY LOG *---------------------------------------------------------------------- * MOD. NO.|   DATE   |   NAME  | REASON  | CHANGE REQ. *---------------------------------------------------------------------- *             |2014-3-6|Liaokailin| Created | * DESCRIPTION: * 
***********************************************************************/package org.demo.decorate.demo01;/** * 动态地给一个对象添加一些额外的职责。就增加功能来说,Decorator模式相比生成子类更为灵活。 * 适用性 1.在不影响其他对象的情况下,以动态、透明的方式给单个对象添加职责。 2.处理那些可以撤消的职责。 3.当不能采用生成子类的方法进行扩充时。 参与者 1.Component 定义一个对象接口,可以给这些对象动态地添加职责。 2.ConcreteComponent 定义一个对象,可以给这个对象添加一些职责。 3.Decorator 维持一个指向Component对象的指针,并定义一个与Component接口一致的接口。 4.ConcreteDecorator 向组件添加职责。 *///componentinterface Person{ void eat() ;}//ConcreteComponent class Man implements Person{ @Override public void eat() { System.out.println("Man: eat"); }}//Decorate abstract class Decorator implements Person{ protected Person person ; public void setPerson(Person person){ this.person = person ; } public void eat(){ person.eat() ; }}//ConcreteDecorateclass ConcreteDecorate extends Decorator{ public void eat(){ System.out.println("wash before each meal"); super.eat() ; System.out.println("wipe mouth"); }}public class Demo01 { public static void main(String[] args) { Man m = new Man() ; ConcreteDecorate c = new ConcreteDecorate() ; c.setPerson(m) ; //装饰man c.eat() ;}}

 

    运行结果: 

wash before each mealMan: eatwipe mouth

3. 案例二

    

/********************************************************************** * 
 * FILE : Demo02.java * CLASS : Demo02 * * AUTHOR : Liaokailin * * FUNCTION : TODO * * *====================================================================== * CHANGE HISTORY LOG *---------------------------------------------------------------------- * MOD. NO.|   DATE   |   NAME  | REASON  | CHANGE REQ. *---------------------------------------------------------------------- *             |2014-3-6|Liaokailin| Created | * DESCRIPTION: * 
***********************************************************************/package org.demo.decorate.demo02;interface DrinkComponent{ float getTotalCost() ; String getDescription() ;}class Espresso implements DrinkComponent{ private String description = "Espresso" ; private float cost = 0.75f ; public float getTotalCost() { return cost; } public String getDescription() { return description; }}class EspressoConPanna implements DrinkComponent { private String description = "EspressoConPare"; private float cost = 1; public float getTotalCost() { return cost; } public String getDescription() { return description; } }abstract class Decorator implements DrinkComponent{ protected DrinkComponent component ; public Decorator(DrinkComponent component){ this.component = component ; } @Override public float getTotalCost(){ return component.getTotalCost() ; } public String getDescription() { return component.getDescription(); }}class ExtraEspresso extends Decorator{ private float cost = 1f ; public ExtraEspresso(DrinkComponent component){ super(component) ; } @Override public float getTotalCost() { return super.getTotalCost()+cost; } @Override public String getDescription() { return super.getDescription() +"extra espresso"; } }public class Demo02 { public static void main(String[] args) { Espresso espresso = new Espresso() ; //DrinkComponent ; ExtraEspresso decorate = new ExtraEspresso(espresso) ; System.out.println("coffer:"+decorate.getDescription()+";totalCost:"+decorate.getTotalCost() ); }}

 

执行结果: 

     

coffer:Espressoextra espresso;totalCost:1.75

 

 

4. 案例三

    

/********************************************************************** * 
 * FILE : Demo03.java * CLASS : Demo03 * * AUTHOR : Liaokailin * * FUNCTION : TODO * * *====================================================================== * CHANGE HISTORY LOG *---------------------------------------------------------------------- * MOD. NO.|   DATE   |   NAME  | REASON  | CHANGE REQ. *---------------------------------------------------------------------- *             |2014-3-6|Liaokailin| Created | * DESCRIPTION: * 
***********************************************************************/package org.demo.decorate.demo03;/** * 写一个简单的系统, * 用decorator模式模拟以下情况: * 有些鸟会飞而有一些不会, * 有一些鸟会游泳而有一些不会, * 还有一些既会飞又会游泳。 * * 分析 如果写一个bird类 然后继承生成各种bird 这显然不合理的 bird太多 导致子类过多 。 * 但是为了给bird增加一些功能 如: fly swim */ interface Bird{ void type() ; //类型 }class Bird01 implements Bird{ public void type() { System.out.print(" a bird :"); }} abstract class Decorator implements Bird{ protected Bird bird ; public Decorator(Bird bird){ this.bird = bird ; } public void type(){ bird.type() ; }}class Decorator01 extends Decorator{ public Decorator01(Bird bird) { super(bird); } @Override public void type() { super.type(); System.out.println(" only can fly "); }} class Decorator02 extends Decorator{ public Decorator02(Bird bird) { super(bird); } @Override public void type() { super.type(); System.out.println(" only can swim "); }} class Decorator03 extends Decorator{ public Decorator03(Bird bird) { super(bird); } @Override public void type() { super.type(); System.out.println(" not only fly,but also swim "); }} public class Demo03 { public static void main(String[] args) { Bird b = new Bird01() ; Decorator d1 = new Decorator03(b) ; d1.type(); }}

 

执行结果:

 

a bird : not only fly,but also swim

 

 

    

 

 

 

 

 

 

 

 

 

   

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/liaokailin/p/3799940.html

你可能感兴趣的文章
mysql命令gruop by报错this is incompatible with sql_mode=only_full_group_by
查看>>
LeetCode55 Jump Game
查看>>
poj 3764 The xor-longest Path (01 Trie)
查看>>
预备作业01
查看>>
【Spark】Spark-Redis连接池
查看>>
【云计算】使用supervisor管理Docker多进程-ntpd+uwsgi+nginx示例最佳实践
查看>>
Ubuntu16.04下配置ssh免密登录
查看>>
实验二 2
查看>>
will-change属性
查看>>
android学习笔记54——ContentProvider
查看>>
Unity3d android开发之触摸操作识别-双击,滑动去噪处理
查看>>
Custom view * is not using the 2- or 3-argument View constructors; XML attributes will not work
查看>>
模型选择准则
查看>>
安卓动态增加按钮
查看>>
iOS7程序后台运行
查看>>
maven+testng+reportng的pom设置
查看>>
IT telephone interview
查看>>
gitlab安装配置
查看>>
ps载入画笔
查看>>
悲怆:IT人的一声叹息->一个程序员的自白[转帖]
查看>>