Adapter:Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.
参考文献: 1:阎宏,《Java与模式》,电子工业出版社 2:Eric Freeman & Elisabeth Freeman,《Head First Design Pattern》,O'REILLY
一:定义:
Decorator:Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.
二:引入
假设现在有一家咖啡店,经营咖啡,茶等饮料,我们来为它设计一个系统。
问题:
饮料加糖或牛奶等调料时是要另收费的(每包1元),顾客可以要也可以不要,所以这个固定价格的cost方法不符合要求. 不符合OCP(open for extension,close for modification)
you want to use an existing class, and its interface does not match the one you need.
you want to create a reusable class that cooperates with unrelated or unforeseen classes, that is, classes that don't necessarily have compatible interfaces.
(object adapter only) you need to use several existing subclasses, but it's impractical to adapt their interface by subclassing every one. An object adapter can adapt the interface of its parent class.
参考文献: 1:阎宏,《Java与模式》,电子工业出版社 2:Eric Freeman & Elisabeth Freeman,《Head First Design Pattern》,O'REILLY