1 / 9

OO Design Principle 3

OO Design Principle 3. Class AddSet. Class AddSet has the following public methods Constructor: AddSet() initializes to null public ArrayList get_Elements() returns all elements in the AddSet public Boolean add(Object ele) add ele to the AddSet and return true if successful

lajos
Download Presentation

OO Design Principle 3

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. OO Design Principle 3 OO Design Principle 3

  2. Class AddSet • Class AddSet has the following public methods • Constructor: AddSet() initializes to null • public ArrayList get_Elements() returns all elements in the AddSet • public Boolean add(Object ele) add ele to the AddSet and return true if successful • public Boolean addAll(ArrayList v) add an array list referenced by v to the AddSet and return true if successful. • Write a program1 to add {“a”,”b”,”c”}. • Write a program2 to not only add an element/elemtns but also count how many have been added OO Design Principle 3

  3. Principle 3 • Prefer Composition Over Inheritance OO Design Principle 3

  4. Inheritance • Method of reuse in which new functionality is obtained byextending the implementation of an existing object • The generalization class (the superclass) explicitly captures thecommon attributes and methods • The specialization class (the subclass) extends the implementationwith additional attributes and methods OO Design Principle 3

  5. Advantage/Disadvantage of Inheritance • Advantages: • New implementation is easy, since most of it is inherited • Easy to modify or extend the implementation being reused • Disadvantages: • Breaks encapsulation, since it exposes a subclass to implementation detailsof its superclass • "White-box" reuse, since internal details of superclasses are often visible tosubclasses • Subclasses may have to be changed if the implementation of the superclasschanges • Implementations inherited from superclasses can not be changed at runtime OO Design Principle 3

  6. Composition • Method of reuse in which new functionality is obtained bycreating an object composed of other objects • The new functionality is obtained by delegating functionality toone of the objects being composed • Sometimes called aggregation or containment, although someauthors give special meanings to these terms • For example: • Aggregation - when one object owns or is responsible for another objectand both objects have identical lifetimes (GoF) • Aggregation - when one object has a collection of objects that can exist ontheir own (UML) • Containment - a special kind of composition in which the contained objectis hidden from other objects and access to the contained object is only viathe container object OO Design Principle 3

  7. Composition • Composition can be: • By reference • By value • C++ allows composition by value or by reference • In Java/C# all we have are object references! OO Design Principle 3

  8. Advantages/Disadvantages of Composition • Advantages: • Contained objects are accessed by the containing class solely through theirinterfaces • "Black-box" reuse, since internal details of contained objects are not visible • Good encapsulation • Fewer implementation dependencies • Each class is focused on just one task • The composition can be defined dynamically at run-time through objectsacquiring references to other objects of the same type OO Design Principle 3

  9. Advantages/Disadvantages of Composition • Disadvantages: • Resulting systems tend to have more objects • Interfaces must be carefully defined in order to use many different objects as composition blocks OO Design Principle 3

More Related