2010年11月13日星期六

一个关于Inheritance和Delegate的故事

发现一段更有意思的。很怀念的东东啊~Knight Rider and Robo Cop~(怨念:Knight Rider 2010版 居然只播了一季就因为收视率不佳而停播了~怨念啊怨念~~~~~)

Once upon a time (before Baywatch), there was a man with no name. Knight Industries decided that if this man were given guns and wheels and booster rockets, he would be the perfect crime-fighting tool. First, they thought, "Let's subclass him and override everything we need to add the guns and wheels and booster rockets." The problem was that to subclass Michael Knight, you would need to know an awful lot about his guts so that you could wire them to guns and booster rockets. So instead, they created a helper object, the Knight Industries 2000, or "KITT the super car."

Note how this is different from the RoboCop approach. RoboCop was a man subclassed and extended. The whole RoboCop project involved dozens of surgeons who extended the man's brain into a fighting machine. This is the approach taken with many object-oriented frameworks.

While approaching the perimeter of an arms dealer's compound, Michael Knight would speak to KITT over his watch-radio. "KITT," he would say, "I need to get to the other side of that wall." KITT would then blast a big hole in the wall with a small rocket. After destroying the wall, Kitt would return control to Michael, who would stroll through the rubble.

Many objects in the Cocoa framework are extended in much the same way. That is, there is an existing object that needs to be extended for your purpose. Instead of subclassing the table view, you simply supply it with a helper object. For example, when a table view is about to display itself, it will turn to the helper object to ask "How many rows of data am I displaying?" "What should be displayed in the first column, second row?"

Thus, to extend an existing Cocoa class, you will frequently write a helper object. This chapter focuses on creating helper objects and connecting them to the standard Cocoa objects.

所以从技术上来说~ KITT (印象里应该是是Knight Industry Two Thousand的缩写~)其实是用了比 Robo Cop 更少的投入,做到了几乎相似,甚至要更优越的功能~毕竟在Knight Rider里,还有一个Karl的存在,而在Robo Cop里只有它一个(那个大个子似乎是没有大脑的版本,是个纯AI制品~)~~~

PS:每次想起Knight Rider 我就会想起小时候的一个故事~~~
我到现在还记得小时候看的Knight Rider里那个让我困惑了无数年的Phrase:计算机超负荷~记得剧情里是某女爆喝一声“计算机超负荷”,然后对键盘敲几下,然后就让KITT行为发生改变,不在接受Michael的指挥~整个场景和小强同学的“天马流星拳”一样让人印象深刻~而我百思不得其解的问题是,为什么超负荷能让代码的行为发生改变~还有如何才能写几行代码就能让计算机超负荷~难道是写了一个死循环(那个时候我好像刚刚开始玩GWBasic,正在研究 For 循环,GWBasic的For循环很难产生死循环,所以对“死循环”这个术语有着不同寻常的兴趣)~
直到N年以后,我读大学了~代码也写了N多了~某天回想起这个“计算机超负荷”的时候~我才意识到,这是那个翻译完全不同计算机术语瞎翻译导致的结果~
“计算机超负荷”的原文应该是"Computer Overload"~Overload 从构词来说的确挺像是超出(over)-负荷(load)这两个词复合而成~所以他那样翻译了~
而这个词,一个准确的属于应该是"重载"~“计算机重载”要比“计算机超负荷”这个说法靠谱多了~虽然我到现在也不知道如何去重载一个计算机~呵呵~不过 Science Fiction 嘛~意境到了就 ok 了~“计算机重载”这种说法可要比“计算机超负荷”要有意境多了~

TimNew
------------
Release your passion
To Realize your potential

I am a pessimist, I feel I'm living in a world without light.
But I am also a prayer, I believe I’m going towards a world full of sunshine!


2010/11/13 米良
Cocoa Programming for Mac OS X 里看到这么一段话~挺有意思的~~~

Once upon a time, there was a company called Taligent, which was created by IBM and Apple to develop a set of tools and libraries like Cocoa. About the time Taligent reached the peak of its mindshare, I met one of its engineers at a trade show. I asked him to create a simple application for me: A window would appear with a button, and when the button was clicked, the words "Hello, World!" would appear in a text field. The engineer created a project and started subclassing madly: subclassing the window and the button and the event handler. Then he started generating code: dozens of lines to get the button and the text field onto the window. After 45 minutes, I had to leave. The app still did not work. That day, I knew that the company was doomed. A couple of years later, Taligent quietly closed its doors forever.

可以想象,这个故事一定发生在OO刚刚起步的年代~那个年代几乎把 inheritance 当作所有问题的 Killer Pill~随着技术的演化,在无数的 Developer 对着一堆堆让人眼花缭乱的继承树(特别是像C++这样具有多继承能力的预言的继承树)浪费的无数的 Working Hour,杀死了无数自己的脑细胞后,人们终于开始意识到或许应该有一种比 Inheritance 更好的方式去处理某些问题~ 于是有了 Composition 有了 Delegate~~~甚至把继承深度作为了代码可维护性的一个重要指标!

看新的预言中~C# 和 Java 拿掉了多继承~ 在产生变种对象的时候,Java 是采用 Handler 的新类,其实就是 Delegate 的方式~ C# 对于比较重量级的对象(比如 Form,Window,Page)依然采用 Inheritance ,但是对于轻量级的对象(Button,TextBox,MIenutem)时则采用更为灵活的方法级别的Delegate~
C#的方式和 Cocoa 比较类似。
Cocoa 中,由于界面采用溜达MVC模式~需要大量定制的对象,比如Window,View,会采用继承重载的方式。需要改变行为时一般会 Controller,而要改变Visual时,通常会重载Visual Element对象本身~ 而轻量级的定制,或者仅仅是响应某些交互的话,则会采用Delegate的方式~比如Application的Delegate~~

Vision只有一个,但是Implementation会有很多~而对比不同语言对同一个问题的处理方式是一件非常有意思的事情~在这种对比中~才能发现不同的设计模式和实现模式的优劣~~~和在现实中的实用效果~~~~

TimNew
------------
Release your passion
To Realize your potential

I am a pessimist, I feel I'm living in a world without light.
But I am also a prayer, I believe I’m going towards a world full of sunshine!



Posted via email from 米良的草窝

没有评论:

发表评论