大话设计模式读后感之装饰模式
来源:互联网 发布:杨氏模量测量实验数据 编辑:程序博客网 时间:2024/05/20 13:20
一、装饰模式(Decorator)
动态地给一个对象添加一个额外的职责,就增加功能来说,装饰模式比子类更为灵活。
其实我们对装饰模式并不陌生,在JDK中使用装饰模式设计的类的有以下。
•java.io.BufferedInputStream(InputStream)
•java.io.DataInputStream(InputStream)
•java.io.BufferedOutputStream(OutputStream)
•java.util.zip.ZipOutputStream(OutputStream)
•java.util.Collections#checked[List|Map|Set|SortedSet|SortedMap]()
简单装饰模式
Component类
/** * Created with Intellij IDEA. * User : ybh * Created on 2017/8/4. * Description : * Component 是定义一个对象的接口,可以给这些对象动态添加职责 */public abstract class Component { public abstract void Operation();}
ConcreteComponent 类
/** * Created with Intellij IDEA. * User : Yebinghuan * Created on 2017/8/4. * Description : * ConcreteComponent 定义了一个具体对象,也可以给这个对象添加一些职责 */public class ConcreteComponent extends Component { @Override public void Operation() { System.err.println("具体对象操作"); }}
Decorator 类
/** * Created with Intellij IDEA. * User : Yebinghuan * Created on 2017/8/4. * Description : * Decorator 装饰抽象类 * 继承了Component,作为子类扩展Component类功能, * Component并不需要知道Decorator的存在 */public abstract class Decorator extends Component { protected Component component; /** * 设置Component * @param component */ public void setComponent(Component component) { this.component = component; } /** * 重写Operation 实际执行的是compoent */ @Override public void Operation() { if (component!=null){ component.Operation(); } }}
/** * Created with Intellij IDEA. * User : Yebinghuan * Created on 2017/8/4. * Description : */public class ConcreteDecoratorA extends Decorator{ //独有属性区别于装饰B private String addedState; @Override public void Operation() { //运行Component的方法 super.Operation(); addedState="New State"; System.err.println("具体装饰对象A的操作"); }}
装饰类B
/** * Created with Intellij IDEA. * User : Yebinghuan * Created on 2017/8/4. * Description : */public class ConcreteDecoratorB extends Decorator { /* 独有方法 */ public void addedBehavior(){ } @Override public void Operation() { //运行Component的方法 super.Operation(); addedBehavior(); System.err.println("具体装饰对象B的操作"); }}Main
/** * Created with Intellij IDEA. * User : Yebinghuan * Created on 2017/8/4. * Description : */public class TestMain { public static void main(String[] args) { //具体对象 ConcreteComponent c=new ConcreteComponent(); ConcreteDecoratorA cA=new ConcreteDecoratorA(); ConcreteDecoratorB cB=new ConcreteDecoratorB(); //对象包装 cA.setComponent(c); cB.setComponent(cA); cB.Operation(); /** * 装饰模式利用setComponent进行对象包装,每个装饰对象的实现和使用分离, * 每个装饰对象只要关系自己的功能,不需要关心如何被使用 */ }}
例子
ConcreteComponent类
/** * Created with Intellij IDEA. * User : Yebinghuan * Created on 2017/8/4. * Description : * 该类为我们需要装饰的具体类 */public class Person { //人名 private String name; public Person(){ } public Person(String name){ this.name=name; } public void show(){ System.err.println(name+"时装有"); }}
/** * Created with Intellij IDEA. * User : Yebinghuan * Created on 2017/8/4. * Description : * 装饰类 */public class Finery extends Person { private Person person; /* 装饰对象 */ public void Decorate(Person person){ this.person=person; } @Override public void show() { if (person!=null){ person.show(); } }}
/** * Created with Intellij IDEA. * User : Yebinghuan * Created on 2017/8/4. * Description : * 装饰方法之一 */public class TShirts extends Finery { @Override public void show() { super.show(); System.err.println("T恤"); }}
装饰B
/** * Created with Intellij IDEA. * User : Yebinghuan * Created on 2017/8/4. * Description : * 装饰方法之一 */public class Pants extends Finery { @Override public void show() { super.show(); System.err.println("短裤"); }}
Main类
/** * Created with Intellij IDEA. * User : Yebinghuan * Created on 2017/8/4. * Description : */public class Main { public static void main(String[] args) { Person Person=new Person("七月"); Finery finery=new Finery(); TShirts tShirts=new TShirts(); Pants t=new Pants(); finery.Decorate(Person); tShirts.Decorate(finery); t.Decorate(tShirts); t.show(); }}
设计模式好有意思
阅读全文
0 0
- 大话设计模式读后感之装饰模式
- 大话设计模式读后感之工厂模式
- 大话设计模式读后感之策略模式
- 大话设计模式读后感之原型模式
- 大话设计模式读后感之外观模式
- 大话设计之装饰模式
- 大话设计模式之装饰设计模式
- 《大话设计模式》读后感
- 《大话设计模式》读后感
- 大话设计模式读后感
- 读前辈的大话设计模式(四)之装饰模式,抒自己的读后感
- 大话设计模式之-----装饰模式
- 大话设计模式之装饰模式
- 大话设计模式之装饰模式
- 大话设计模式之装饰模式
- 大话设计模式之装饰模式
- 大话设计模式之装饰者模式
- 大话设计模式之装饰器模式
- ## 本地文件上传github (坚持看到尾部,提供各种命令行error,解决办法)##
- Android Studio 安装应用的时候提示Installation failed with message INSTALL_FAILED_USER_RESTRICTED
- 1120. Friend Numbers (20) <set>
- IdentityActivator()
- 利息、汇率与外汇
- 大话设计模式读后感之装饰模式
- 高可用、开源的Redis缓存集群方案
- c++ unique_lock与lock_guard的区别
- [USACO3.2]纺车的轮子 Spinning Wheels
- System.IO.Directory.GetCurrentDirectory与System.Windows.Forms.Application.StartupPath的用法
- VC++(unicode)实现URLDecode函数,对URl编码的字符串进行解码
- Cache 的作用
- 7.IDA-创建结构体
- 基于AWS搭载laravel