讀《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.

浙公網安備 33010602011771號