上游充通大兽 (3)Delegation as alternative to SHANGHAI JIAO TONG UNIVERSITY Implementation Inheritance In Delegation two objects are involved in handling a request A receiving object delegates operations to its delegate. The developer can make sure that the receiving object does not allow the client to misuse the delegate object calls Client Receiver Delegates to Delegate Software Engineering
Software Engineering Client calls Receiver Delegates to Delegate (3) Delegation as alternative to Implementation Inheritance In Delegation two objects are involved in handling a request • A receiving object delegates operations to its delegate. • The developer can make sure that the receiving object does not allow the client to misuse the delegate object
上游充通大兽 Delegation instead of Implementation SHANGHAI JIAO TONG UNIVERSITY Inheritance Inheritance:Extending a Base class by a new operation or overwriting an operation. Delegation:Catching an operation and sending it to another object. Which of the following models is better for implementing a stack? List Stack +Add() List +Remove( Remove() +Push() Add() Stack +Pop() +Top() +Push() +Pop() +Top() Software Engineering
Software Engineering Delegation instead of Implementation Inheritance Inheritance: Extending a Base class by a new operation or overwriting an operation. Delegation: Catching an operation and sending it to another object. Which of the following models is better for implementing a stack? +Add() +Remove() List Stack +Push() +Pop() +Top() +Push() +Pop() +Top() Stack Add() Remove() List
上游充通大学 SHANGHAI JIAO TONG UNIVERSITY Object design model before transformation Object design model after transformation Hashtable Hashtable put(key,element) put(key,element) get(key):Object get(key):Object containsKey(key):boolean containsKey(key):boolean containsValue(element):boolean containsValue(element):boolean table 1 1 MySet MySet put(element) put(element) containsValue(element):boolean containsValue(element):boolean Software Engineering
Software Engineering Hashtable MySet put(element) containsValue(element):boolean put(key,element) get(key):Object containsKey(key):boolean containsValue(element):boolean Object design model before transformation Object design model after transformation Hashtable MySet put(element) containsValue(element):boolean put(key,element) get(key):Object containsKey(key):boolean containsValue(element):boolean table 1 1
上游充通大兽 SHANGHAI JIAO TONG UNIVERSITY /Implementation of MySet using /Implementation of MySet using inheritance delegation * class MySet extends Hashtable class MySet /Constructor omitted private Hashtable table; MySet() MySet(){ ) table Hashtable(); 3 void put(Object element){ void put(object element){ if (containsKey(element)){ if (containsValue(element)){ put(element,this); table.put(element,this); } ) boolean containsValue(Object boolean containsValue(Object element){ element) return containsKey(element); return (table.containsKey(element)); /Other methods omitted * 2 3 /Other methods omitted * 3 Software Engineering
Software Engineering /* Implementation of MySet using inheritance */ class MySet extends Hashtable { /* Constructor omitted */ MySet() { } void put(Object element) { if (!containsKey(element)){ put(element, this); } } boolean containsValue(Object element){ return containsKey(element); } /* Other methods omitted */ } /* Implementation of MySet using delegation */ class MySet { private Hashtable table; MySet() { table = Hashtable(); } void put(Object element) { if (!containsValue(element)){ table.put(element,this); } } boolean containsValue(Object element) { return (table.containsKey(element)); } /* Other methods omitted */ }
上泽通大学 Comparison:Delegation vs Implementation SHANGHAI JIAO TONG UNIVERSITY Inheritance Delegation 。Pro: Flexibility:Any object can be replaced at run time by another one (as long as it has the same type) .Con: why? Inefficiency:Objects are encapsulated. Inheritance 。Pro: Straightforward to use Supported by many programming languages Easy to implement new functionality Con: Inheritance exposes a subclass to the details of its parent class Any change in the parent class implementation forces the subclass to change(which requires recompilation of both) Software Engineering
Software Engineering Comparison: Delegation vs Implementation Inheritance Delegation • Pro: • Flexibility: Any object can be replaced at run time by another one (as long as it has the same type) • Con: • Inefficiency: Objects are encapsulated. Inheritance • Pro: • Straightforward to use • Supported by many programming languages • Easy to implement new functionality • Con: • Inheritance exposes a subclass to the details of its parent class • Any change in the parent class implementation forces the subclass to change (which requires recompilation of both) why?