1 / 16

Mechanisms for Reuse

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();

paytah
Download Presentation

Mechanisms for Reuse

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Mechanisms for Reuse CMPS 2143

  2. OOP billed as technology that permits software to be constructed from general-purpose reusable components • Two main mechanisms that permit this are • Inheritance • composition

  3. Illustration: Construct a Set from a List class List { public: List(); void add (int); int firstElement (); int size (); int includes (int); void remove (int); : };

  4. 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

  5. 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)

  6. 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); }

  7. 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!!!

  8. 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

  9. #include <List.h> class Set : public List { public: Set(); void add (int); int size (); }; • Notice new class does not define any new data fields

  10. 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); }

  11. 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!!!

  12. 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

  13. 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

  14. 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

  15. 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.

  16. Study questions • Pg. 286: 6-8

More Related