用机器码编程,用汇编编程,用C语言编程,用C++编程,用Java编程...都离不开代码重用。代码重用这一概念更是在面向对象编程中发挥到极致----衍化出许多种代码重用的技术,有实现继承--直接使用父类的实现;有接口继承--实现父类的接口;还有近些年异常流行的范型技术。 首先区别实现继承和接口继承。先看一段实现继承的代码: class animal { public: int weight(){return m_weight;} private: int m_weight; //each animal has its weight }; class pig:public animal { public: int pork_weight(){return m_pork_weight;} //it's useful to know the pork weight private: int m_pork_weight; }; 这样pig就不用一行代码就可以获得属性total weight. 再看看接口继承: class IPlayFootball { public: virtual int pass() = 0; virtual int shot() = 0; virtual int free_kick() = 0; virtual int defence() = 0; }; class footballer: public IPlayFootball,public I... { public: virtual int pass(){/*algorithm to get the pass ratio*/} virtual int shot(){/*algorithm to get the shot ratio*/} virtual int free_kick() {/*algorithm to get the free_kick ratio*/} virtual int defence(){/*algorithm to get the defence ratio*/} private: int m_strength; int m_weight; int m_abs_speed; int m_ex_speed; int m_height; ... }; 它们的使用可以示例为: pig apig; cout<<"apig's total weight is "<<apig.weight()<<",and its pork weight is "<<apig.pork_weight()<<".\n"; footballer Raul(...); //initialize one player with his parameters footballer Nesta(...); //as above ... footballer* pcurr_controller = &Raul; footballer* pcurr_defender = &Nesta; //below occur a pass event in a football game if(pcurr_controller.pass() >= pcurr_defender.defence()) //Raul.pass() is not so good, hehe ... //do something else { ... //do something pcurr_controller = pcurr_defender; } 从上面的代码可以看到实现继承依赖于父类的实现;而接口继承所有的代码都在子类。 二者都有各自的好处:实现继承能直接简化代码,不用维护父类已经维护了的代码;而接口继承只是实现了父类的方法,并不依赖于任何父类的实现。从设计的角度考虑,实现继承实现了一组物件的内部关系;而接口继承定义了一组事物它们外部的联系。 实现继承可以让代码得到更大的重用,而它的劣势也很明显,就是过于依赖父类的实现,因此对父类的组织结构和扩展性要求非常高;接口继承并不定义对象间内部关系,因此耦合度更低,扩展性更好。 设计的时候怎样协调二者的关系呢?这就要设计者对对象的特点的把握,如果有的属性可能会发生变化或者子类可能有不同的实现要求,就不应该放到父类的域里面,提供一个接口可能更好;而如果对象有的属性是固定的,那可以考虑放到父类里面。 大家都提倡“面向接口编程”,我认为如果怀疑自己设计水平的话,这倒是最好的方法。 参考书籍: GOF <<Design Patterns>> Bjarne Stroustrup <<The Design&Evolution of C++>> Bjarne Stroustrup <<The C++ Programming Language>> 
|