160 likes | 299 Views
Mechanisms for Reuse. CMPS 2143. OOP billed as technology that permits software to be constructed from general-purpose reusable components Two main mechanisms that permit this are Inheritance composition. Illustration: Construct a Set from a List. class List { public: List();
E N D
Mechanisms for Reuse CMPS 2143
OOP billed as technology that permits software to be constructed from general-purpose reusable components • Two main mechanisms that permit this are • Inheritance • composition
Illustration: Construct a Set from a List class List { public: List(); void add (int); int firstElement (); int size (); int includes (int); void remove (int); : };
is-a and has-a revisited • is-a corresponds to inheritance • has-a corresponds to composition • if class A has-a class B, then a data field of class B should be part of an instance of class A • like Customer was a part of Room
Using composition • Investigate how Set ADT can be formed with composition • A portion of the state of the new data structure is simply an instance of the existing structure • So here, ADT Set contains an instance field name theData(type List)
Since List is stored as part of the data area for our set, it must be initialized in the constructor Set::Set() : theData() { } • Operations that manipulate the new Set structure use existing operations provided by the List data type int Set::includes(int newValue) { return theData.includes(newValue); }
Only one operation is slightly complex – must do a check before include add new value, since sets do not contain duplicates int Set::add( int newValue) { if (!includes (newValue)) theData.add(newValue); } • Composition provides a way to use existing software components in the creation of new software • Composition makes no claims about substitutability!!!
Using inheritance • The new class Set will be declared as subclass of class List • The class Set • will inherit member data and methods • can define new data values or new methods • can also override methods
#include <List.h> class Set : public List { public: Set(); void add (int); int size (); }; • Notice new class does not define any new data fields
data fields defined in the List class will be used to maintain the set elements, but they must still be initialized. Set::Set() : List() { } • Operations that manipulate the new Set structure inherit existing operations provided by the List data type int Set::includes(int newValue) { return theData.includes(newValue); }
Only one operation is slightly complex – must do a check before include add new value, since sets do not contain duplicates int Set::add( int newValue) { if (!includes (newValue)) List::add(newValue); //List:: why? } • Composition provides a way to use existing software components in the creation of new software • Composition makes no claims about substitutability!!!
Composition and Inheritance Contrasted • Composition is simpler • more clearly indicates what operations can be performed on a particular data structure • In Set ADT, can ONLY do add, includes, and size • In Inheritance, operations of new ADT are a superset of operations of the original data structure • must examine operations of original ADT to see entire set of legal operations • yo-yo problem – flip back and forth between classes
Composition and Inheritance Contrasted • Inheritance requires writing less code • Inheritances doesn’t prevent users from manipulating new ADT using methods from parent class, even if not appropriate • can retrieve firstElement from aSet • In Composition, can change the storage mechanism from one ADT to another • Set could use a HashTable instead, no external difference to users of Set
Composition and Inheritance Contrasted • Inheritance allows us to use the new ADT polymorphically • since the new ADT exhibits property of substitutability • Inheritance • brevity of code, but not protocol • Composition • more code, but less protocol • ADTs implemented through inheritance avoid that extra additional call
Composition and Inheritance Contrasted • So which is better? • Depends! • on whether you need substitution • Do we need a set to act like a list sometimes? • probably not.
Study questions • Pg. 286: 6-8