OOD之思路乱发散

每一个object都含有state和method,因此定义一个object的时候一定要说明他的含有什么data member,还有什么method。

多用design pattern,少用inheritance;using interfaces




1. 设计一个文本编辑器

a)composite pattern来分别管理只读文本和可读写文本
b)singleton 一个文本管理manage
c) observer 可以告诉其他文本一些global状态的改变,比如format


2. Zoo

An interface for Animal,It has several abstract methods: sell,buy,sleep,eat
use composite pattern to implement differentspecies animal, like bird,cat. These species are also abstract class. And the implementation of specific animal,like cat,tiger,puma.

each abstract class has a state: habit,which shows the common habit of a particular species. two methods: sethabit / gethabit. eachconcrete class has more information: living area and so on. override method: sell buy,eat.

相关文章

什么是设计模式一套被反复使用、多数人知晓的、经过分类编目...
单一职责原则定义(Single Responsibility Principle,SRP)...
动态代理和CGLib代理分不清吗,看看这篇文章,写的非常好,强...
适配器模式将一个类的接口转换成客户期望的另一个接口,使得...
策略模式定义了一系列算法族,并封装在类中,它们之间可以互...
设计模式讲的是如何编写可扩展、可维护、可读的高质量代码,...