Chapter 08
Chapter 08
Bernd Bruegge & Allen H. Dutoit ! ! Object-Oriented Software Engineering: Using UML, Patterns, and Java 2!
When do we identify objects?
Bernd Bruegge & Allen H. Dutoit ! ! Object-Oriented Software Engineering: Using UML, Patterns, and Java 3!
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
Off-the-shelf components
Bernd Bruegge & Allen H. Dutoit ! ! Object-Oriented Software Engineering: Using UML, Patterns, and Java 5!
Object Design Activities
Bernd Bruegge & Allen H. Dutoit ! ! Object-Oriented Software Engineering: Using UML, Patterns, and Java 7!
Discovering Inheritance
Bernd Bruegge & Allen H. Dutoit ! ! Object-Oriented Software Engineering: Using UML, Patterns, and Java 8!
Generalization Example: Modeling a
Coffee Machine
VendingMachine
Generalization:
The class CoffeeMachine is
discovered first, then the class
SodaMachine, then the
superclass
VendingMachine
CoffeeMachine
totalReceipts
numberOfCups
coffeeMix
collectMoney()
makeChange()
heatWater()
dispenseBeverage()
addSugar()
addCreamer()
Example of a Specialization
CandyMachine is a new
VendingMaschine
product and designed as a sub
class of the superclass
totalReceipts
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()
Bernd Bruegge & Allen H. Dutoit ! ! Object-Oriented Software Engineering: Using UML, Patterns, and Java 10!
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
operations specified, but not yet implemented.
Bernd Bruegge & Allen H. Dutoit ! ! Object-Oriented Software Engineering: Using UML, Patterns, and Java 11!
Example for Implementation Inheritance
Bernd Bruegge & Allen H. Dutoit ! ! Object-Oriented Software Engineering: Using UML, Patterns, and Java 13!
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()
Bernd Bruegge & Allen H. Dutoit ! ! Object-Oriented Software Engineering: Using UML, Patterns, and Java 14!
Comparison: Delegation v. Inheritance
Bernd Bruegge & Allen H. Dutoit ! ! Object-Oriented Software Engineering: Using UML, Patterns, and Java 15!
Frameworks
Bernd Bruegge & Allen H. Dutoit ! ! Object-Oriented Software Engineering: Using UML, Patterns, and Java 16!
Frameworks in the Development Process
Bernd Bruegge & Allen H. Dutoit ! ! Object-Oriented Software Engineering: Using UML, Patterns, and Java 17!
White-box and Black-box Frameworks
• White-box frameworks:
• Extensibility achieved through inheritance and dynamic
binding.
• Existing functionality is extended by subclassing
framework base classes and overriding specific
methods (so-called hook methods)
• Black-box frameworks:
• Extensibility 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.
Bernd Bruegge & Allen H. Dutoit ! ! Object-Oriented Software Engineering: Using UML, Patterns, and Java 18!
Another Source for Finding Objects : Design
Patterns
• What are Design Patterns?
• A design pattern describes a problem which occurs
over and over again in our environment
• Then it describes the core of the solution to that
problem, in such a way that you can use the this
solution a million times over, without ever doing it the
same twice
Bernd Bruegge & Allen H. Dutoit ! ! Object-Oriented Software Engineering: Using UML, Patterns, and Java 19!
More on Design Patterns
• Design patterns are partial solutions to common
problems such as
• such as separating an interface from a number of
alternate implementations
• wrapping around a set of legacy classes
• protecting a caller from changes associated with specific
platforms
• A design pattern consists of a small number of
classes
• uses delegation and inheritance
• these classes can be adapted and refined for the specific
system under construction
Bernd Bruegge & Allen H. Dutoit ! ! Object-Oriented Software Engineering: Using UML, Patterns, and Java 20!
Adapter Pattern
Bernd Bruegge & Allen H. Dutoit ! ! Object-Oriented Software Engineering: Using UML, Patterns, and Java 21!
Adapter Pattern
Client
ClientInterface LegacyClass
Request() ExistingRequest()
adaptee
Inheritance
Adapter
Delegation
Request()
Bernd Bruegge & Allen H. Dutoit ! ! Object-Oriented Software Engineering: Using UML, Patterns, and Java 22!
Bridge Pattern
Bernd Bruegge & Allen H. Dutoit ! ! Object-Oriented Software Engineering: Using UML, Patterns, and Java 23!
Bridge Pattern
Taxonomy in
Taxonomy in
Application Domain
Solution Domain
Bernd Bruegge & Allen H. Dutoit ! ! Object-Oriented Software Engineering: Using UML, Patterns, and Java 24!
Motivation for the Bridge Pattern
Bernd Bruegge & Allen H. Dutoit ! ! Object-Oriented Software Engineering: Using UML, Patterns, and Java 25!
Use of the Bridge Pattern:
Support multiple Database Vendors
Arena
imp
LeagueStore LeagueStoreImplementor
Bernd Bruegge & Allen H. Dutoit ! ! Object-Oriented Software Engineering: Using UML, Patterns, and Java 26!
Adapter vs Bridge
• Similarities:
• Both are used to hide the details of the underlying
implementation.
• Difference:
• The adapter pattern is geared towards making
unrelated components work together
• Applied to systems after they’re designed
(reengineering, interface engineering).
• “Inheritance followed by delegation”
• A bridge, on the other hand, is used up-front in a
design to let abstractions and implementations vary
independently.
• Green field engineering of an “extensible system”
• “Delegation followed by inheritance”
Bernd Bruegge & Allen H. Dutoit ! ! Object-Oriented Software Engineering: Using UML, Patterns, and Java 27!
Facade Pattern
• Provides a unified interface to a set of objects in a
subsystem.
• A facade defines a higher-level interface that
makes the subsystem easier to use (i.e. it
abstracts out the gory details)
• Facades allow us to provide a closed
Bernd Bruegge & Allen H. Dutoit ! ! Object-Oriented Software Engineering: Using UML, Patterns, and Java 28!
Design Example
• Subsystem 1 can look into
Subsystem 1
the Subsystem 2 (vehicle
subsystem) and call on any
component or class
operation at will.
• This is “Ravioli Design”
• Why is this good? Subsystem 2
• Efficiency
Seat
• Why is this bad?
• Can’t expect the caller to Card
understand how the
subsystem works or the AIM
complex relationships SA/RT
within the subsystem.
• We can be assured that the
subsystem will be misused,
leading to non-portable
code
Bernd Bruegge & Allen H. Dutoit ! ! Object-Oriented Software Engineering: Using UML, Patterns, and Java 29!
Subsystem Design with Façade, Adapter,
Bridge
• The ideal structure of a subsystem consists of
• an interface object
• a set of application domain objects (entity objects)
modeling real entities or existing systems
• Some of the application domain objects are
interfaces to existing systems
• one or more control objects
• We can use design patterns to realize this
subsystem structure
• Realization of the Interface Object: Facade
• Provides the interface to the subsystem
• Interface to existing systems: Adapter or Bridge
• Provides the interface to existing system (legacy
system)
• The existing system is not necessarily object-oriented!
Bernd Bruegge & Allen H. Dutoit ! ! Object-Oriented Software Engineering: Using UML, Patterns, and Java 30!
When should you use these Design
Patterns?
• A façade should be offered by all subsystems in a
software system who a services
• The adapter design pattern should be used to
interface to existing components
• The bridge design pattern should be used to
interface to a set of objects
• where the full set of objects is not completely known at
analysis or design time.
• when a subsystem or component must be replaced later
after the system has been deployed and client programs
use it in the field.
Bernd Bruegge & Allen H. Dutoit ! ! Object-Oriented Software Engineering: Using UML, Patterns, and Java 31!
Observer Pattern Motivation Portfolio
*
Stock
• Problem:
• We have an object that changes its state quite
often
• Example: A Portfolio of stocks
• We want to provide multiple views of the current
state of the portfolio
• Example:Histogram view, pie chart view, time
line view, alarm
• Requirements:
• The system should maintain consistency across
the (redundant) views, whenever the state of the
observed object changes
• It should be possible to add new views without
having to recompile the observed object or the
existing views.
Bernd Bruegge & Allen H. Dutoit ! ! Object-Oriented Software Engineering: Using UML, Patterns, and Java 32!
Observer Pattern: Decouples an Abstraction from its
Views
Subject! Observer!
observers! *!
subscribe(subscriber)
unsubscribe(subscriber) update()
notify()
ConcreteSubject! ConcreteObserver!
state observeState
getState()
update()
setState()
Bernd Bruegge & Allen H. Dutoit ! ! Object-Oriented Software Engineering: Using UML, Patterns, and Java 35!
Strategy Pattern
Policy
Context
*
Strategy
ContextInterface()
AlgorithmInterface
ConcreteStrategyA
ConcreteStrategyB
ConcreteStrategyC
AlgorithmInterface()
AlgorithmInterface()
AlgorithmInterface()
Database
*
SortInterface
SelectSortAlgorithm()
Sort()
Sort()
BubbleSort
QuickSort
MergeSort
Sort()
Sort()
Sort()
Bernd Bruegge & Allen H. Dutoit ! ! Object-Oriented Software Engineering: Using UML, Patterns, and Java 37!