装饰模式-设计模式(三)
2015-05-07 00:00
946 查看
摘要: GoF《设计模式》中定义装饰模式:动态的给一个对象添加一些额外的职责。就增加功能而言,Decorator模式比生成子类更为灵活。
* 装饰模式 结构型设计模式
*
* Decorator装饰模式是一种结构型模式,它主要是解决:“过度地使用了继承来扩展对象的功能”,由于继承为类型引入的静态特质,使得这种扩展方式缺乏灵活性;
* 并且随着子类的增多(扩展功能的增多),各种子类的组合(扩展功能的组合)会导致更多子类的膨胀(多继承)。
* 继承为类型引入的[静态特质]的意思是说以继承的方式使某一类型要获得功能是在编译时。所谓静态,是指在编译时;动态,是指在运行时。
*
* GoF《设计模式》中说道:动态的给一个对象添加一些额外的职责。就增加功能而言,Decorator模式比生成子类更为灵活。
*
* Spring中常用Wrapper关键字作为类的后缀{@link org.springframework.beans.BeanWrapperImpl}
* Spring中常用Holder关键字作为类名的后缀{@link org.springframework.jdbc.datasource.ConnectionHolder}
*
* 以BeanWrapper对构造完成的对象实例进行包裹,返回相应的BeanWrapper实例。
* BeanWrapper接口通常在Spring框架内部使用,它有一个实现类org.springframework.beans.BeanWapperImpl
* 其作用是对某个bean进行包裹,然后对这个包裹的bean进行操作,比如设置或者获取bean的相应属性值。
* 使用BeanWapper对bean实例进行操作很方便,可以免去直接使用Java反射API操作对象实例的烦琐。
*
*
* WikiPedia:
* In object-oriented programming, the decorator pattern (also known as Wrapper,
* an alternative naming shared with the Adapter pattern) is a design pattern
* that allows behavior to be added to an individual object, either statically or dynamically,
* without affecting the behavior of other objects from the same class.
* The decorator pattern is often useful for adhering to the Single Responsibility Principle,
* as it allows functionality to be divided between classes with unique areas of concern
*
* @see javax.servlet.http.HttpServletRequestWrapper
* @see javax.servlet.http.HttpServletResponseWrapper
* @see java.util.Collections#checkedMap() checkedXXX()
* @see java.util.Collections#synchronizedSet() synchronizedXXX()
* @see java.util.Collections#unmodifiableList() unmodifiableXXX()
*
* @{link java.io.BufferedInputStream} can decorate other streams such as @{link java.io.FilterInputStream}
*
*
* All subclasses InputStream, OutputStream, Reader and Writer have a constructor taking an instance of same type.
* @see java.io.InputStream
* @see java.io.OutputStream
* @see java.io.Reader
* @see java.io.FilterOutputStream
* @author no1
我们以小米手机和ViVo手机来写一个列子,希望能帮助大家理解:
1000.0
打电话的手机
1400.0
打电话的手机 , 智能音乐手机
1600.0
打电话的手机 , 智能音乐手机
3ff0
, 会发烧的手机
* 装饰模式 结构型设计模式
*
* Decorator装饰模式是一种结构型模式,它主要是解决:“过度地使用了继承来扩展对象的功能”,由于继承为类型引入的静态特质,使得这种扩展方式缺乏灵活性;
* 并且随着子类的增多(扩展功能的增多),各种子类的组合(扩展功能的组合)会导致更多子类的膨胀(多继承)。
* 继承为类型引入的[静态特质]的意思是说以继承的方式使某一类型要获得功能是在编译时。所谓静态,是指在编译时;动态,是指在运行时。
*
* GoF《设计模式》中说道:动态的给一个对象添加一些额外的职责。就增加功能而言,Decorator模式比生成子类更为灵活。
*
* Spring中常用Wrapper关键字作为类的后缀{@link org.springframework.beans.BeanWrapperImpl}
* Spring中常用Holder关键字作为类名的后缀{@link org.springframework.jdbc.datasource.ConnectionHolder}
*
* 以BeanWrapper对构造完成的对象实例进行包裹,返回相应的BeanWrapper实例。
* BeanWrapper接口通常在Spring框架内部使用,它有一个实现类org.springframework.beans.BeanWapperImpl
* 其作用是对某个bean进行包裹,然后对这个包裹的bean进行操作,比如设置或者获取bean的相应属性值。
* 使用BeanWapper对bean实例进行操作很方便,可以免去直接使用Java反射API操作对象实例的烦琐。
*
*
* WikiPedia:
* In object-oriented programming, the decorator pattern (also known as Wrapper,
* an alternative naming shared with the Adapter pattern) is a design pattern
* that allows behavior to be added to an individual object, either statically or dynamically,
* without affecting the behavior of other objects from the same class.
* The decorator pattern is often useful for adhering to the Single Responsibility Principle,
* as it allows functionality to be divided between classes with unique areas of concern
*
* @see javax.servlet.http.HttpServletRequestWrapper
* @see javax.servlet.http.HttpServletResponseWrapper
* @see java.util.Collections#checkedMap() checkedXXX()
* @see java.util.Collections#synchronizedSet() synchronizedXXX()
* @see java.util.Collections#unmodifiableList() unmodifiableXXX()
*
* @{link java.io.BufferedInputStream} can decorate other streams such as @{link java.io.FilterInputStream}
*
*
* All subclasses InputStream, OutputStream, Reader and Writer have a constructor taking an instance of same type.
* @see java.io.InputStream
* @see java.io.OutputStream
* @see java.io.Reader
* @see java.io.FilterOutputStream
* @author no1
我们以小米手机和ViVo手机来写一个列子,希望能帮助大家理解:
1000.0
打电话的手机
1400.0
打电话的手机 , 智能音乐手机
1600.0
打电话的手机 , 智能音乐手机
3ff0
, 会发烧的手机
interface Phone{ double getPrice(); String function(); } class BasicPhone implements Phone{ @Override public String function() { return " 打电话的手机 "; } @Override public double getPrice() { return 1000.00; } } class PhoneDecorator extends BasicPhone{ private Phone decoratedPhone; public PhoneDecorator(Phone decoratedPhone) { this.decoratedPhone = decoratedPhone; } @Override public String function() { return decoratedPhone.function(); } @Override public double getPrice() { return decoratedPhone.getPrice(); } } class XiaoMi extends PhoneDecorator{ public XiaoMi(Phone decoratedPhone) { super(decoratedPhone); } @Override public String function() { return super.function()+" , 会发烧的手机 "; } @Override public double getPrice() { return super.getPrice() + 200; } } class Vivo extends PhoneDecorator{ public Vivo(Phone decoratedPhone) { super(decoratedPhone); } @Override public String function() { return super.function()+" , 智能音乐手机 "; } @Override public double getPrice() { return super.getPrice() + 400; } } public class DecoratorPattern { public static void main(String[] args) { Phone phone = new PhoneDecorator(new BasicPhone()); System.out.println(phone.getPrice()); System.out.println(phone.function()); System.out.println(); phone = new PhoneDecorator(new Vivo(new BasicPhone())); System.out.println(phone.getPrice()); System.out.println(phone.function()); System.out.println(); phone = new PhoneDecorator(new XiaoMi(new Vivo(new BasicPhone()))); System.out.println(phone.getPrice()); System.out.println(phone.function()); } }
相关文章推荐
- 设计模式---装饰
- 设计模式之装饰着模式
- C++设计模式之十四--Decorator装饰模式
- 设计模式-装饰模式
- 设计模式之:装饰模式
- 黑马程序员:装饰设计模式介绍及装饰模式与继承关系的区别
- 设计模式--装饰器模式Decorator(结构型)
- 24种设计模式--装饰模式【Decorator Pattern】
- php设计模式20-装饰着模式
- 设计模式笔记(10)---装饰模式(结构型)
- 设计模式之装饰模式(Decorate Pattern)
- 黑马程序员-JAVA基础-IO流中的装饰设计模式
- 设计模式之装饰模式Decorator
- Day22 设计模式之装饰模式
- IO流,缓冲区,装饰设计模式
- 设计模式之装饰模式
- java设计模式--装饰模式
- 设计模式-装饰模式
- 大话设计模式之装饰设计模式
- 编程经常使用设计模式具体解释--(中篇)(适配器、装饰、代理、外观、桥接、组合、享元)