当前位置: 代码迷 >> 设计模式 >> HeadFirst 设计方式 读书笔记
  详细解决方案

HeadFirst 设计方式 读书笔记

热度:8884   发布时间:2013-02-26 00:00:00.0
HeadFirst 设计模式 读书笔记
要依赖抽象,不要依赖具体类
1. 抽象工厂模式 ??


2. 单例模式 ? ?为什么全局变量不能保证只有一个实例?
?
?
3. HeadFirst ?设计模式 ? ?命令模式 ?P205
?
public class GarageDoorOpenCommand implements Command {
? ? ?
? ? ?GarageDoor mGarageDoor;
?
? ? ?public GarageDoorOpenCommand(GarageDoor aGarageDoor) {
? ? ? ? ? mGarageDoor = aGarageDoor;r
? ? }
?
? ? ?public void excute() {
? ? ? ? ? mGarageDoor.up();
?????}??????????
?
}
?
命令模式将“请求”封装成对象,以便使用不同的请求、队列或者日志来参数化其他对象。命令
模式也支持可撤销的操作。
?
P218 onButtonWasPushed方法的undoCommand = onCommands[slot];错了?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?应该是 undoCommand = offCommands[slot]; ?
?
P223 ? [slot 0]应该是headfirst.command.undo.CeilingFanMediumCommand
?
命令模式可以让你使用不同的请求、队列,或者日志请求来参数化其他对象,
命令模式也可以支持撤销操作。
?
?
4. ? 适配器模式
? ? ?
? ? ?public class DuckAdapter implements Turkey {
? ? ? ? ? Duck mDuck;
? ? ? ? ? Random mRand;
? ? ??????
? ? ? ? ? public DuckAdapter(Duck aDuck) {
? ? ? ? ? ? ? ?mDuck = aDuck;
??????????}
? ? ??????
? ? ? ? ? public void gooble() {
? ? ? ? ? ? ? ?mDuck.quack();
??????????}
?
? ? ? ? ? public void fly() {
? ? ? ? ? ? ? ?if (rand.nextInt(5) == 0) {
? ? ? ? ? ? ? ? ? ? duck.fly();
???????????????}
??????????}
?????}
?
? ? ?适配器模式将一个类的接口,转换成客户期望的另一个接口。
?
?
? ? ?Client: want a Duck
? ? ? Target: Duck
? ? ? Adpter:TurkeyAdpter
? ? ?Adaptee: Turkey
?
? ? ?P251 ? ?将迭代器转成枚举
? ? ? ? ? ? ? ? ? ? ?
?????public class IteratorEnumeration implements Enumeration {
? ? ? ? ? ?Iterator iterator;
?
? ? ? ? ? public IteratorEnumeration(Iterator iterator) {
? ? ? ? ? ? ? ?this.iterator = ?iterator;
??????????} ? ?
?
? ? ? ? ? public boolean hasMoreElements() {
? ? ? ? ? ? ? ?return iterator.hasNext();
??????????}
?
? ? ? ? ? public Object nextElement() {
? ? ? ? ? ? ? ?return iterator.next();
??????????}
? ? ? }
?5. 外观模式
? ? ? 外观不只是简化了接口,也将客户从组件的子系统中解耦。
? ? ? 外观和适配器可以包装许多类,但是外观的意图是简化接口,而适配器的意图是将接口转化成不同接口。