90 likes | 197 Views
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
E N D
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 • 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
Principle 3 • Prefer Composition Over Inheritance OO Design Principle 3
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
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
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
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
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
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