Design pattern:Adapter 设计模式:适配器

Categories: Java; Tagged with: ; @ July 19th, 2012 11:04

Adapter pattern(Wrapper pattern), is a design pattern that allows classes to work together, those classes can not work normally because they have incompatible interface.
For example, you travel from US to China, you need to use an adapter so that you charger can work, because they have different standard.

There are two types, one is Class level, and the other is Object(Instance) level.
Class Adapter pattern:

“The adapter is created by implementing or inheriting both the interface that is expected and the interface that is pre-existing. It is typical for the expected interface to be created as a pure interface class, especially in languages such as Java that do not support multiple inheritance

(http://en.wikipedia.org/wiki/Adapter_pattern)

Object Adapter pattern

Adapter contains an instance of the class it wraps, so that the adapter can delegate the request to warped instance.

 

设计模式之策略模式: Strategy Design Pattern

Categories: Flex; Tagged with: ; @ July 17th, 2011 17:59

适用情景:   假如有一组相关的算法, 客户需要从中动态选择一个来使用. 譬如: 学校中的新生分班, 有按照成绩分班, 按照性别分班, 按照住宿类型分班等等.  (当然实际情况中的分班考虑因素会更复杂一些)

最简单的方法是一个Method, 通过判断类型进行相应操作. 但这样做的是这个Method会巨大无比, 不易维护. 因此我们可使用Strategy模式, 会多一些Interface或Class, 但会降低耦合, 拥有更佳的可扩展性.

_SHUPZTTF4MMN_D7DT7TBX0
(ActionScript)

负责进行分班的主体类包含IClassEnrollStrategy的一个Instance, 对于这个Instance可以使用以下方式注入:

1. 使用Constructor注入到ClassEnroll中, 可在ClassEnroll中增加一个Constructor:
ClassEnroll(strategy:IClassEnrollStrategy)

2. 使用Setter注入. ClassEnroll中对strategy的Setter方法:
setStrategy(strategy:IClassEnrollStrateStrategy)

3. 在ClassEnroll使用工厂模式创建IClassEnrollStrategy实例.



// Proudly powered by Apache, PHP, MySQL, WordPress, Bootstrap, etc,.