读《Head First Design Patterns》(第1章)

从JOE设计模拟鸭子游戏中我体会到了继承和接口另一区别:接口可以让个别的类拥有某一功能,而继承则必须使整个类家族拥有该功能特征,不需要的子孙就覆盖掉。以前我最大的感受是接口可以实现多重继承,而继承不能(针对单根继承的语言)
Design principle:
take the parts that vary and encapsulate them,so that later you can alter or extend the parts that vary without affecting those that don&apost
这一原则几乎是每一种模式的基础
Design principle:
program to an interface, not to an implementation.assign the concrete implementation to object at runtime.
programming to implementation:
Dog d = new Dog();
d.bark();
programming to interface/superType:
Animal animal = new Animal();
animal.MakeSound();
even better :
animal = GetAnimal();
animal.MakeSound();
我的理解,这一原则就是把一个类中频繁变动或可能变动的部分抽象出来,形成一个独立的实现某一功能(behavior)的类的集合,一个面对某一接口(抽象类)的类集合。这样把变动部分分离出来,而不是藕合在具体的实现细节中,避免了由于变动部分的不断变化而导致整个类的代码跟着变化。这样新增变化只要在变化部分新增一种具体实现就可以了。
Design principle:
Favor Composition over inheritance
The strategy pattern
defines a familly of algorithms ,encapsulates each one, and makes them interchangeable.
Strategy lets the algorithm vary independently from clients that ues it. 

 

posted @ 2006-01-21 23:30  红心李  阅读(112)  评论(0编辑  收藏  举报