0% found this document useful (0 votes)
3 views50 pages

Lecture - 8

The lecture discusses object design with a focus on reuse, emphasizing the importance of transforming system models and optimizing them for implementation. It outlines four key activities in object design: reuse, interface specification, restructuring, and optimization, while also introducing concepts like the Adapter Pattern and different types of reuse (white-box and black-box). Additionally, it covers inheritance, delegation, and the use of abstract classes and methods to enhance flexibility and maintainability in software design.

Uploaded by

pand4inca
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views50 pages

Lecture - 8

The lecture discusses object design with a focus on reuse, emphasizing the importance of transforming system models and optimizing them for implementation. It outlines four key activities in object design: reuse, interface specification, restructuring, and optimization, while also introducing concepts like the Adapter Pattern and different types of reuse (white-box and black-box). Additionally, it covers inheritance, delegation, and the use of abstract classes and methods to enhance flexibility and maintainability in software design.

Uploaded by

pand4inca
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 50

Lecture - 8

Object Design I: Reuse


Object Design

• Purpose of object design:


• Prepare for the implementation of the system model
based on design decisions
• Transform the system model (optimize it)
• Investigate alternative ways to implement the
system model
• Use design goals: minimize execution time, memory
and other measures of cost.
• Object design serves as the basis of
implementation.
System Development as a Set of
Activities
System Model Problem

Application objects Analysis

Solution objects Design

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

Specifying types &


signatures

Identifying patterns
Specifying constraints

Specifying exceptions Adjusting patterns


Detailed View of Object Design
Activities (ctd)
Check Use Cases

Restructuring Optimization

Revisiting Optimizing access


inheritance paths

Collapsing classes Caching complex


computations

Delaying complex
Realizing associations computations
One Way to do Object Design

1. Identify the missing components in the design gap


2. Make a build or buy decision to obtain the missing
component
=> Component-Based Software Engineering:
The design gap is filled with available
components (“0 % coding”)

• Special Case: COTS-Development


• COTS: Commercial-off-the-Shelf
• The design gap is filled with commercial-off-the-shelf-
components.
=> Design with standard components.
Adapter Pattern .

• Adapter Pattern: Connects incompatible


components
• It converts the interface of one component into
another interface expected by the other (calling)
component
• Used to provide a new interface to existing legacy
components (Interface engineering, reengineering)
• Also known as a wrapper.
Adapter Pattern
New System Old System
(“Legacy System”)

Client

ClientInterface LegacyClass

Request() ExistingRequest()

adaptee
Adapter

Request().
Modeling of the Real World

• Design knowledge such as the adapter pattern


complements application domain knowledge and
solution domain knowledge
• Modeling of the real world leads to a system that
reflects today’s realities but not necessarily
tomorrow’s
• There is a need for reusable and extendable
(“flexible”) designs.
Reuse
• Main goal:
• Reuse knowledge from previous experience to current
problem
• Reuse functionality already available
• Composition (also called Black Box Reuse)
• New functionality is obtained by aggregation
• The new object with more functionality is an
aggregation of existing components
• Inheritance (also called White-box Reuse)
• New functionality is obtained by inheritance.
• Three ways to get new functionality:
• Implementation inheritance
• Interface inheritance
• Delegation
Reuse of Code

• I have a list, but the customer wants to have a stack


• The list offers the operations Insert(), Find(), Delete()
• The stack needs the operations Push(), Pop() and Top()
• Can I reuse the existing list?
• I am an employee in a company that builds cars with
expensive car stereo systems
• Can I reuse the existing car software in a home stereo
system?
Reuse of interfaces

• I am an off-shore programmer in Hawaii. I have a


contract to implement an electronic parts catalog
for Daimler
• How can my contractor make sure that I implement it
correctly?
• I would like to develop a window system for Linux
that behaves the same way as in Vista
• How can I make sure that I follow the conventions for
Vista and not those of MacOS X?
• I have to develop a new service for cars, that
automatically call a help center when the car is
used the wrong way
• Can I reuse the help desk software that I developed for a
company in the telecommunication industry?
Reuse of existing classes

• I have an implementation for a list of elements


of type int
• Can I reuse this list to build
• a list of customers
• a spare parts catalog
• a flight reservation schedule?
• I have developed a class “Addressbook” in a
previous project
• Can I add it as a subsystem to my e-mail program
which I purchased from a vendor (replacing the vendor-
supplied address book)?
• Can I reuse this class in the billing software of my
dealer management system?
Customization: Build Custom Objects

• Problem: Closing the object design gap


• Develop new functionality
• Main goals:
• Reuse functionality already available
• Use design knowledge (from previous experience)
• Composition (also called Black Box Reuse)
• The new functionality is obtained by aggregation
• The new object with more functionality is an
aggregation of existing objects
• Inheritance (also called White-box Reuse)
• The new functionality is obtained by inheritance.
Example of Composition

Requirements Analysis Incident


(Language of Application
Domain)
Report

Text box Menu Scrollbar

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

• White box reuse


• Access to the development artifacts (analysis model,
system design, object design, source code) must be
available

• Black box reuse


• Access to models and designs is not available, or
models do not even exist
• Worst case: Only executables (binary code) are
available
• Better case: A specification of the system interface
is available.
Types of Whitebox Reuse

1. Implementation inheritance
• Reuse of Implementations
2. Specification Inheritance
• Reuse of Interfaces

• Programming concepts to achieve reuse


 Inheritance
• Delegation
• Abstract classes and Method Overriding
• Interfaces
Why Inheritance?

1. Organization (during analysis):


• Inheritance helps us with the construction of
taxonomies to deal with the application domain
• when talking the customer and application domain
experts we usually find already existing
taxonomies
2. Reuse (during object design):
• Inheritance helps us to reuse models and code to deal
with the solution domain
• when talking to developers
Example of using Inheritance
Car Superclass:
public class Car {
drive() public void drive() {…}
brake() public void brake() {…}
accelerate() public 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() }
Discovering Inheritance Associations

• To “discover“ inheritance associations, we can


proceed in two ways, which we call
specialization and generalization

• Generalization: the discovery of an inheritance


relationship between two classes, where the sub
class is discovered first.
• Specialization: the discovery of an inheritance
relationship between two classes, where the
super class is discovered first.
Generalizing often leads to
Restructuring
VendingMachine
VendingMachine Called Remodeling if done on
the model level;
called Refactoring if done on totalReceipts
the source code level.
collectMoney()
makeChange()
dispenseBeverage()

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

 Problem with implementation inheritance:


• The inherited operations might exhibit unwanted behavior
• Example: What happens if the Stack user calls Remove()
instead of Pop()?
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?
List

+Add() Stack List


+Remove()
+Push() +Add()
+Pop() +Remove()
Stack +Top()

+Push()
+Pop()
+Top()
Delegation

• Delegation is a way of making composition as


powerful for reuse as inheritance
• In delegation two objects are involved in
handling a request from a Client

• The Receiver object delegates operations to the


Delegate object
• The Receiver object makes sure, that the Client
does not misuse the Delegate object.

Client Receiver delegates to Delegate


calls
Comparison: Delegation vs
Implementation Inheritance
• Delegation
☺ Flexibility: Any object can be replaced at run time by
another one (as long as it has the same type)
☹ Inefficiency: Objects are encapsulated
• Inheritance
☺ Straightforward to use
☺ Supported by many programming languages
☺ Easy to implement new functionality in the subclass
☹ 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).
Recall: Implementation Inheritance v.
Specification-Inheritance
• Implementation Inheritance: The combination of
inheritance and implementation
• The interface of the super class is completely inherited
• Implementations of methods in the super class
("Reference implementations") are inherited by any
subclass
• Specification Inheritance: The combination of
inheritance and specification
• The interface of the super class is completely inherited
• Implementations of the super class are not inherited
• The super class is an abstract class.
Abstract Operations and Abstract
Classes
• Abstract method:
• A method with a signature but without an
implementation. Also called abstract operation
• Abstract class:
• A class which contains at least one abstract method is
called abstract class
• UML Interface: An abstract class which has only
abstract operations
• An interface is primarily used for the specification
of a system or subsystem. The implementation is
provided by a subclass or by other mechanisms.
Example of an Abstract Operation
VendingMachine
dispenseItem() must be
totalReceipts
implemented in each subclass.
collectMoney() We do this by specifying the
makeChange()
dispenseItem()
operation as abstract. Abstract
operations are written in UML
in italics.

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

• Contraction: Implementations of methods in


the super class are overwritten with empty
bodies in the subclass to make the super class
operations “invisible“
• Contraction is a special type of inheritance
• It should be avoided at all costs but is used
often.
Contraction should be avoided

A contracted subclass delivers the desired


functionality expected by the client, but:
• The interface contains operations that make no sense
for this class
• What is the meaning of the operation brake() for a
BoomBox?
The subclass does not fit into the taxonomy
A BoomBox ist not a special form of Auto
Frameworks

• A framework is a partial application that can be


specialized to produce custom applications
• The key benefits of frameworks are reusability
and extensibility:
• Reusability leverages of the application domain
knowledge and prior effort of experienced developers
• Extensibility is provided by methods which can be
overwritten by the application to extend the
framework.
Classification of Frameworks

• Frameworks can be classified by their position in


the software development process:
• Infrastructure frameworks
• Middleware frameworks

• Frameworks can also be classified by the


techniques used to extend them:
• Whitebox frameworks
• Blackbox frameworks
Frameworks in the Development
Process
• Infrastructure frameworks aim to simplify the
software development process
• Used internally, usually not delivered to a client
• Middleware frameworks are used to integrate
existing distributed applications
• Examples: Java RMI, CORBA, WebObjects, WebSphere.
• Enterprise application frameworks are
application specific and focus on domains
• Example of application domains: telecommunications,
avionics, environmental modeling, manufacturing,
financial engineering, enterprise business activities.
White-box and Black-box
Frameworks
• White-box frameworks:
• Extensibility is achieved through inheritance and
dynamic binding
• Existing functionality is extended by subclassing
framework base classes and overwriting specific
methods designed to be overwritten(so-called hook
methods)
• Black-box frameworks:
• Extensibility is achieved by defining interfaces for
components that can be plugged into the framework.
• Existing functionality is reused by defining components
that conform to a particular interface
• These components are integrated with the framework
via delegation.
Class libraries vs. Frameworks

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy