Lecture - 8
Lecture - 8
Custom objects
Object Design
Off-the-Shelf Components
System Design
Existing Machine
Object Design Activities consists of
4 Activities
1. Reuse: Identification of existing solutions
• Use of inheritance
• Off-the-shelf components and Focus on
additional solution objects Reuse
• Use of Design patterns and
Specification
2. Interface specification
• Describes precisely each class interface
3. Object model restructuring
• Transforms the object design model to
improve its understandability and extensibility Towards
Mapping
4. Object model optimization Models to
• Transforms the object design model to address Code
performance criteria such as response
time or memory utilization.
Object Design
Activities
Select Subsystem We start here
Specification Reuse
Identifying missing
Identifying components
attributes & operations
Specifying visibility
Adjusting components
Identifying patterns
Specifying constraints
Restructuring Optimization
Delaying complex
Realizing associations computations
One Way to do Object Design
Client
ClientInterface LegacyClass
Request() ExistingRequest()
adaptee
Adapter
Request().
Modeling of the Real World
Object Design
(Language of Solution
Domain)
Other Reasons for additional Objects
in Object Design
• The implementation of algorithms may
necessitate objects to hold values
• New low-level operations may be needed during
the decomposition of high-level operations
• Example: EraseArea() in a drawing program
• Conceptually very simple
• Implementation is complicated:
• Area represented by pixels
• We need a Repair() operation to clean up objects
partially covered by the erased area
• We need a Redraw() operation to draw objects
uncovered by the erasure
• We need a Draw() operation to erase pixels in
background color not covered by other objects.
White Box and Black Box Reuse
1. Implementation inheritance
• Reuse of Implementations
2. Specification Inheritance
• Reuse of Interfaces
Subclass:
LuxuryCar public class LuxuryCar extends Car
{
public void playMusic() {…}
public void ejectCD() {…}
playMusic() public void resumeMusic() {…}
ejectCD()
public void pauseMusic() {…}
resumeMusic()
pauseMusic() }
Discovering Inheritance Associations
CoffeeMachine SodaMachine
totalReceipts totalReceipts
numberOfCups cansOfBeer
coffeeMix cansOfCola
collectMoney() collectMoney() CoffeeMachine
makeChange() makeChange() SodaMachine
heatWater() chill() numberOfCups
coffeeMix cansOfBeer
dispenseBeverage() dispenseBeverage()
cansOfCola
addSugar() heatWater()
addCreamer() addSugar() chill()
addCreamer()
Example of a Specialization
VendingMachine CandyMachine is a new
product. We design it as a sub
totalReceipts class of the superclass
VendingMachine
collectMoney()
makeChange()
dispenseBeverage() A change of names might now
be useful: dispenseItem()
instead of
dispenseBeverage()
and
dispenseSnack()
CoffeeMachine
SodaMachine CandyMachine
numberOfCups
coffeeMix cansOfBeer bagsofChips
cansOfCola numberOfCandyBars
heatWater()
addSugar() chill() dispenseSnack()
addCreamer()
Example of a Specialization (2)
VendingMachine
totalReceipts
collectMoney()
makeChange()
dispenseItem()
CoffeeMachine
SodaMachine
numberOfCups CandyMachine
coffeeMix cansOfBeer
cansOfCola bagsofChips
heatWater() numberOfCandyBars
addSugar() chill()
dispenseItem() dispenseItem()
addCreamer()
dispenseItem()
Implementation Inheritance and
Specification Inheritance
• Implementation inheritance
• Also called class inheritance
• Goal:
• Extend an applications’ functionality by reusing
functionality from the super class
• Inherit from an existing class with some or all
operations already implemented
• Specification Inheritance
• Also called subtyping
• Goal:
• Inherit from a specification
• The specification is an abstract class with all the
operations specified but not yet implemented.
Implementation Inheritance vs.
Specification Inheritance
• Implementation inheritance: The combination of
inheritance and implementation
• The interface of the superclass is completely inherited
• Implementations of methods in the superclass
("Reference implementations") are inherited by any
subclass
• Specification inheritance: The combination of
inheritance and specification
• The interface of the superclass is completely inherited
• Implementations of the superclass (if there are any) are
not inherited.
Example for Implementation Inheritance
A class is already implemented that does almost
the same as the desired class
Example: List
• I have a List, I need a Stack Add()
• How about subclassing the Remove()
“Already
Stack class from the List
implemented”
class and implementing
Push(), Pop(), Top() with Stack
Add() and Remove()?
Push()
Pop()
Top()
+Push()
+Pop()
+Top()
Delegation
CoffeeMachine
SodaMachine
numberOfCups CandyMachine
coffeeMix cansOfBeer
cansOfCola bagsofChips
heatWater() numberOfCandyBars
addSugar() chill()
dispenseItem() dispenseItem()
addCreamer()
dispenseItem()
Rewriteable Methods and Strict
Inheritance
• Rewriteable Method: A method which allows a
reimplementation
• In Java methods are rewriteable by default, i.e. there is
no special keyword
• Strict inheritance
• The subclass can only add new methods to the
superclass, it cannot over write them
• If a method cannot be overwritten in a Java program, it
must be prefixed with the keyword final.
Strict Inheritance
Car Superclass:
public class Car {
drive() public final void drive() {…}
brake() public final void brake() {…}
accelerate() public final void accelerate()
{…}
}
Subclass:
LuxuryCar public class LuxuryCar extends Car
{
public void playMusic() {…}
public void ejectCD() {…}
playMusic() public void resumeMusic() {…}
ejectCD()
public void pauseMusic() {…}
resumeMusic()
pauseMusic() }
Example: Strict Inheritance and
Rewriteable Methods
Original Java-Code: help() not
overwritable
class Device {
int serialnr;
public final void help() {….}
public void setSerialNr(int n) {
serialnr = n;
}
setSerialNr()
}
overwritable
class Valve extends Device {
Position s;
public void on() {
….
}
}
Example: Overwriting a Method
Original Java-Code: New Java-Code :
class Device { class Device {
int serialnr; int serialnr;
public final void help() {….} public final void help() {….}
public void setSerialNr(int n) { public void setSerialNr(int n) {
serialnr = n; serialnr = n;
} }
} }
class Valve extends Device { class Valve extends Device {
Position s; Position s;
public void on() { public void on() {
…. …
} }
} // class Valve public void setSerialNr(int n) {
serialnr = n + s.serialnr;
}
} // class Valve
UML Class Diagram
Device Device
- int serialnr
- int serialnr
+ setSerialNr(int n)
+setSerialNr(int n)
Valve
Valve
-Position s
- Position s
+ on()
+on()
+ setSerialNr()
Overwriteable Methods:
Usually implemented with Empty
Body
class Device {
int serialnr;
public void setSerialNr(int n) {}
}
class Valve extends Device { I expect, that the method
Position s; setSerialNr()will be
public void on() { overwritten. I only write an
….. empty body
}
public void setSerialNr(int n) {
seriennr = n + s.serialnr;
}
Overwriting of the method
} // class Valve setSerialNr() of Class
Device
Bad Use of OverwritingMethods
One can overwrite the operations of a superclass with
completely new meanings
Example:
Public class SuperClass {
public int add (int a, int b) { return a+b; }
public int subtract (int a, int b) { return a-b; }
}
Public class SubClass extends SuperClass {
public int add (int a, int b) { return a-b; }
public int subtract (int a, int b) { return a+b; }
}
• We have redefined addition as subtraction and subtraction
as addition!!
Bad Use of Implementation
Inheritance
• We have delivered a car with software that allows to
operate an on-board stereo system
• A customer wants to have software for a cheap stereo
system to be sold by a discount store chain
• Dialog between project manager and developer:
• Project Manager:
• „Reuse the existing car software. Don‘t change this
software, make sure there are no hidden surprises.
There is no additional budget, deliver tomorrow!“
• Developer:
• „OK, we can easily create a subclass BoomBox inheriting
the operations from the existing Car software“
• „And we overwrite all method implementations from Car
that have nothing to do with playing music with empty
bodies!“
Contraction
• Class Library:
• Provide a smaller scope of reuse
• Is less domain specific
• Class libraries are passive; there is no constraint on the
flow of control
• Framework:
• Classes cooperate for a family of related applications.
• Frameworks are active; they affect the flow of control.
Components vs. Frameworks
• Components:
• Self-contained instances of classes
• Plugged together to form complete applications
• Can even be reused on the binary code level
• The advantage is that applications do not have to be
recompiled when components change
• Framework:
• Often used to develop components
• Components are often plugged into blackbox
frameworks.