有各种各样的鸭子(酱鸭、板鸭、黑头鸭、野鸭、盐水鸭...),特征是Quack(嘎嘎叫),Swim(游水)。
于是定义的一个SuperDuck(父类),里面有方法Quack、Swim、Display显示!
子类比如黑头鸭、RedHeadDuck,重写Display方法!
 
But Now We need the ducks to Fly!
How to do?
(鸭子要可以飞,怎么办?)
 
 
I Just need to add a fly() method in the SuperDuck(父类),then all the ducks will inherit it.
Now's  my time to really show my true OO genius!(我真是个OO的天才啊,哈哈!)
 
They just give  a demo  and there were rubber duckies flying around the screen!
Was this idea of a joke?
(他们只不过做了一个让好多橡皮的鸭子满屏幕飞的演示!
这是在开玩笑吗?)
 
不是所有的鸭子都能飞的呀!!!
 
Thinks about  inheritance(考虑一下继承)
我可以在RubberDuck(橡皮鸭)中重写Fly的方法。
 
But then what happens when wu add wooden decoy ducks(木头鸭),它既不能Quack叫,也不能Fly。
在木头鸭重写Quack()、Fly()????
 
How about a interface?
I could take the fly() out of the SuperDuck(父类),and make a flyable() interface with a fly() method. That way,only the ducks that are supposed to fly will implement that interface and have a fly() method....and I might as well make a Quackable(嘎嘎),too,since not all the ducks can quack!
(接口怎么样?
我可以把Fly从父类中剥离出来,变成一个接口,如果有鸭子可以飞,那么这只鸭子就实现这个接口。 Quack(嘎嘎叫)也是类似的.)
 
That is , like , the dumbest idea you've come up with.Can you say "duplicate code"?If you thought having to override a few methods was bad,How are you gonna feel when you need to make a little change to the flying behavior... in all 48 of the flying Duck subclass?!
(现在你可以说copy代码。那么我们如果对flying 有一个小小的改变怎么样?所有48个用到这个接口的子类都要改变一下,Copy48份?????)
 
 
How we can do next???
到底该怎么办?
Please be with <<Head First Design pattern>>!
 
 
posted on 2006-01-13 17:25 MasterCai 阅读(1270) 评论(4)  编辑 收藏 网摘 所属分类: 软件工程设计模式

  回复  引用  查看    
2006-01-15 11:48 | 死刑犯      
关注
  回复  引用    
2006-01-17 09:23 | 非鱼___ [未注册用户]
在SupperDuck 中加一个虚方法,不能飞的子类不一定要实现它.
如果只有一种鸭子能飞,就只要添加该鸭子的fly方法.
  回复  引用  查看    
2006-01-18 23:08 | Goodspeed      
这本书什么时候能出中文版呀
  回复  引用  查看    
2006-06-28 11:00 | MasterCai      
中文版估计快了吧,由UML China把台湾的繁体版翻译成简体版。期待啊,必买!

标题  
姓名  
主页
Email (博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
"五向定位"职业成长路线公开课(上海、南京、大连)
Google站内搜索


相关链接: