标签:des style class blog code java
1. 概念
动态地给一个对象添加一些额外的职责。就增加功能来说,Decorator模式相比生成子类更为灵活
2. 案例
/********************************************************************** * <pre> * 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: * </pre> ***********************************************************************/ package org.demo.decorate.demo01; /** * 动态地给一个对象添加一些额外的职责。就增加功能来说,Decorator模式相比生成子类更为灵活。 * 适用性 1.在不影响其他对象的情况下,以动态、透明的方式给单个对象添加职责。 2.处理那些可以撤消的职责。 3.当不能采用生成子类的方法进行扩充时。 参与者 1.Component 定义一个对象接口,可以给这些对象动态地添加职责。 2.ConcreteComponent 定义一个对象,可以给这个对象添加一些职责。 3.Decorator 维持一个指向Component对象的指针,并定义一个与Component接口一致的接口。 4.ConcreteDecorator 向组件添加职责。 */ //component interface 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() ; } } //ConcreteDecorate class 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 meal
Man: eat
wipe mouth
3. 案例二
/********************************************************************** * <pre> * 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: * </pre> ***********************************************************************/ 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. 案例三
/********************************************************************** * <pre> * 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: * </pre> ***********************************************************************/ 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
标签:des style class blog code java
原文地址:http://www.cnblogs.com/liaokailin/p/3799940.html