Professional Documents
Culture Documents
Object Design:
Reusing Pattern
Solutions
Using UML, Patterns, and Java
Object Design
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
Page 1
Object Design: Closing the Gap
System Problem
Application objects
Solution objects
Custom objects
Off-the-shelf components
Machine
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
Page 2
A More Detailed View of Select Subsystem
Specifying visibility
Adjusting components
Specifying types &
signatures
Identifying patterns
Specifying constraints
Restructuring Optimization
Delaying complex
Realizing associations computations
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
Page 3
Finding Objects
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
♦ Requirements Analysis
w Start with Use Cases. Identify participating objects
w Textual analysis of flow of events (find nouns, verbs, ...)
w Extract application domain objects by interviewing client
(application domain knowledge)
w Find objects by using general knowledge
♦ System Design
w Subsystem decomposition
w Try to identify layers and partitions
♦ Object Design
w Find additional objects by applying implementation domain
knowledge
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
Page 4
Application domain vs solution domain objects
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
Page 5
Another Source for Finding Objects : Design Patterns
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
* *
Doors Wheels Battery Engine
*
*
Block
Compound Simple
Statement Statement
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
Page 6
Review: Modeling Typical Aggregations
Fixed Structure: Car
* *
Doors Wheels Battery Engine
Composite
Program
Pattern
Dynamic tree (recursive aggregate):
*
*
Block
Compound Simple
Statement Statement
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
Composite Pattern
♦ Composes objects into tree structures to represent part-whole
hierarchies with arbitrary depth and width.
♦ The Composite Pattern lets client treat individual objects and
compositions of these objects uniformly
*
Client Component
Composite
Leaf
Children
Operation()
Operation() AddComponent
RemoveComponent()
GetChild()
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
Page 7
Graphic Applications use Composite Patterns
• The Graphic Class represents both primitives (Line,
Circle) and their containers (Picture)
*
Client Graphic
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
♦ Software Lifecycle:
w Definition: The software lifecycle consists of a set of development
activities which are either other activities or collection of tasks
w Composite: Activity (The software lifecycle consists of activities
which consist of activities, which consist of activities, which....)
w Leaf node: Task
♦ Software System:
w Definition: A software system consists of subsystems which are
either other subsystems or collection of classes
w Composite: Subsystem (A software system consists of subsystems
which consists of subsystems , which consists of subsystems,
which...)
w Leaf node: Class
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
Page 8
Modeling the Software Lifecycle with a Composite
Pattern
Software *
Manager
Lifecycle
Task Activity
Children
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
Software *
User
System
Class Subsystem
Children
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
Page 9
Ideal Structure of a Subsystem: Façade, Adapter,
Bridge
♦ A subsystem consists of
w an interface object
w a set of application domain objects (entity objects)
modeling real entities or existing systems
t Some of the application domain objects are interfaces to
existing systems
w one or more control objects
♦ Realization of Interface Object: Facade
w Provides the interface to the subsystem
♦ Interface to existing systems: Adapter or Bridge
w Provides the interface to existing system (legacy system)
w The existing system is not necessarily object-oriented!
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
Facade Pattern
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
Page 10
Open vs Closed Architecture
♦ Open architecture:
VIP Subsystem
w Any client can see into the
vehicle subsystem and call on
any component or class
operation at will.
♦ Why is this good?
w Efficiency Vehicle Subsystem
♦ Why is this bad?
w Can’t expect the caller to Seat
understand how the subsystem
works or the complex Card
relationships within the
subsystem. AIM SA/RT
w We can be assured that the
subsystem will be misused,
leading to non-portable code
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
AIM SA/RT
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
Page 11
Realizing a Compiler with a Facade pattern
Compiler
Compiler
compile(s)
CodeGenerator Lexer
create() getToken()
Optimizer Parser
create() generateParseTree()
ParseNode
create()
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
Compiler Compiler
Compiler
compile(s)
CodeGenerator Lexer
create() getToken()
Optimizer Parser
create() generateParseTree()
ParseNode
create()
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
Page 12
Some Additional Definitions
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
Page 13
Metamodel for Inheritance
Inheritance Object
Design
Analysis
activity
Taxonomy Inheritance
for Reuse
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
Taxonomy Example
Mammal
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
Page 14
Reuse
♦ Main goal:
w Reuse knowledge from previous experience to current problem
w Reuse functionality already available
♦ Composition (also called Black Box Reuse)
w New functionality is obtained by aggregation
w The new object with more functionality is an
aggregation of existing components
♦ Inheritance (also called White-box Reuse)
w New functionality is obtained by inheritance.
♦ Three ways to get new functionality:
t Implementation inheritance
t Interface inheritance
t Delegation
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
♦ Implementation inheritance
w Also called class inheritance
w Goal: Extend an applications’ functionality by reusing
functionality in parent class
w Inherit from an existing class with some or all operations
already implemented
♦ Interface inheritance
w Also called subtyping
w Inherit from an abstract class with all operations
specified, but not yet implemented
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
Page 15
Implementation Inheritance
♦ A very similar class is already implemented that does almost
the same as the desired class implementation.
Delegation
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
Page 16
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?
List
+Add() Stack List
+Remove()
Remove()
+Push() Add()
Stack +Pop()
+Top()
+Push()
+Pop()
+Top()
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
♦ Delegation
w Pro:
t Flexibility: Any object can be replaced at run time by another one (as
long as it has the same type)
w Con:
t Inefficiency: Objects are encapsulated.
♦ Inheritance
w Pro:
t Straightforward to use
t Supported by many programming languages
t Easy to implement new functionality
w Con:
t Inheritance exposes a subclass to the details of its parent class
t Any change in the parent class implementation forces the subclass to
change (which requires recompilation of both)
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
Page 17
Design Heuristics
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
Page 18
Reuse...
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
Page 19
Component Selection
♦ Select existing
w off-the-shelf class libraries
w frameworks or
w components
♦ Adjust the class libraries, framework or components
w Change the API if you have the source code.
w Use the adapter or bridge pattern if you don’t have access
♦ Architecture Driven Design
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
Adapter Pattern
Page 20
Adapter pattern
Target Adaptee
Client
Request() ExistingRequest()
adaptee
Adapter
Request()
♦ Delegation is used to
bind an Adapter and an Adaptee
♦ Interface inheritance is used to specify the interface of the
Adapter class.
♦ Target and Adaptee (usually called legacy system) pre-exist
the Adapter.
♦ Target may be realized as an interface in Java.
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
adaptee
ServicesEnumeration
hasMoreElements()
public class ServicesEnumeration nextElement()
implements Enumeration {
public boolean hasMoreElements() {
return this.currentServiceIdx <= adaptee.numServices();
}
public Object nextElement() {
if (!this.hasMoreElements()) {
throw new NoSuchElementException();
}
return adaptee.getService(this.currentSerrviceIdx++);
}
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
Page 21
Bridge Pattern
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
Using a Bridge
♦ The bridge pattern is used to provide multiple implementations
under the same interface.
♦ Examples: Interface to a component that is incomplete, not yet
known or unavailable during testing
♦ JAMES Project: if seat data is required to be read, but the seat
is not yet implemented, not yet known or only available by a
simulation, provide a bridge:
Seat imp
(in Vehicle Subsystem)
VIP SeatImplementation
GetPosition()
SetPosition()
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
Page 22
Seat Implementation
public interface SeatImplementation {
public int GetPosition();
public void SetPosition(int newPosition);
}
public class Stubcode implements SeatImplementation {
public int GetPosition() {
// stub code for GetPosition
}
...
}
public class AimSeat implements SeatImplementation {
public int GetPosition() {
// actual call to the AIM simulation system
}
….
}
public class SARTSeat implements SeatImplementation {
public int GetPosition() {
// actual call to the SART seat simulator
}
...
}
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
Adapter vs Bridge
♦ Similarities:
w Both used to hide the details of the underlying
implementation.
♦ Difference:
w The adapter pattern is geared towards making unrelated
components work together
t Applied to systems after they’re designed (reengineering,
interface engineering).
w A bridge, on the other hand, is used up-front in a design
to let abstractions and implementations vary
independently.
t Green field engineering of an “extensible system”
t New “beasts” can be added to the “object zoo”, even if these are
not known at analysis or system design time.
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
Page 23
Example for Combination of Adapters and Bridges in
JAMES
Seat Seat Preferences
SetSeatPos() GetSeatPos()
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
Creational
Structural Pattern
Pattern
Behavioral
Pattern
Abstract Builder
Factory Pattern
Command Observer Strategy
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
Page 24
Proxy Pattern: Motivation
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
Proxy Pattern
♦ What is expensive?
w Object Creation
w Object Initialization
♦ Defer object creation and object initialization to the time you
need the object
♦ Proxy pattern:
w Reduces the cost of accessing objects
w Uses another object (“the proxy”) that acts as a stand-in for the real
object
w The proxy creates the real object only if the user asks for it
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
Page 25
Proxy pattern Subject
Request()
Proxy RealSubject
realSubject
Request() Request()
Proxy Applicability
♦ Remote Proxy
w Local representative for an object in a different address space
w Caching of information: Good if information does not change too
often
♦ Virtual Proxy
w Object is too expensive to create or too expensive to download
w Proxy is a standin
♦ Protection Proxy
w Proxy provides access control to the real object
w Useful when different objects should have different access and
viewing rights for the same document.
w Example: Grade information for a student shared by
administrators, teachers and students.
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
Page 26
Virtual Proxy example Image
boundingBox()
draw()
ProxyImage RealImage
boundingBox()
realImage
boundingBox()
draw() draw()
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
Before
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
Page 27
Controlling Access
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
After
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
Page 28
Summary
♦ Composite Pattern:
w Models trees with dynamic width and dynamic depth
♦ Adapters, Bridges, Facades, and Proxies (structural Patterns) are variations
on a single theme:
w They reduce the coupling between two or more classes
w They introduce an abstract class to enable future extensions
w They encapsulate complex structures
♦ Facade Pattern:
w Interface to a Subsystem, Closed vs Open Architecture
♦ Adapter Pattern:
w Interface to Reality
♦ Bridge Pattern:
w Interface Reality and Future
♦ Proxy Patterns
w Defer object creation and initialization to the time you need the object
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
A Pattern Taxonomy
Pattern
Creational
Structural Pattern
Pattern
Behavioral
Pattern
Abstract Builder
Factory Pattern
Command Observer Strategy
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
Page 29
Command Pattern: Motivation
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
Command pattern
Command
Invoker
execute()
Client
binds
Receiver
ConcreteCommand
action() execute()
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
Page 30
Menu Example
Invoker:
Asks the Command object
To carry out the request
Menu *
Menu Command
Item
execute()
Application
*
binds
Document
Client Copy Paste
action() execute() execute()
Concrete
Receiver Command
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
♦ Uses:
w Undo queues
w Database transaction buffering
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
Page 31
Command pattern: Editor with unlimited undos
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
Menu
MoveCommand
Triangle
Editor
Rectangle
Receiver Circle
(Entity objects)
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
Page 32
Command pattern: typical sequence
aUser aRectangle:
anEditor moveCommand undoQueue
Rectangle
Drags mouse
newCommand(info)
“MoveCommand1”
store(MoveCommand1)
execute()
move(x, y)
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
♦ Uses:
w Maintaining consistency across redundant state
w Optimizing batch changes to maintain consistency
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
Page 33
Observer pattern (continued)
Observers Subject
9DesignPatterns2.ppt
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
subject
ConcreteObserver
ConcreteSubject update()
getState()
setState(newState) observerState
subjectState
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
Page 34
Sequence diagram for scenario:
Change filename to “foo”
aFile anInfoView aListView
Attach()
Attach()
setState(“foo”)
Subject goes through all its
observers and calls update() on
notify() them, asking for the new
state is decoupled from
the notification
update()
getState()
“foo”
update()
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
Attach()
Attach()
setState(“foo”)
notify()
update()
update()
getState()
“foo”
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
Page 35
Observer pattern implementation in Java
// import java.util;
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
Strategy Pattern
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
Page 36
Strategy Pattern
Policy
Context *
Strategy
ContextInterface() AlgorithmInterface
Database
Strategy * Strategy
Search() Sort()
Sort()
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
Page 37
Applicability of Strategy Pattern
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
A Pattern Taxonomy
Pattern
Creational
Structural Pattern
Pattern
Behavioral
Pattern
Abstract Builder
Factory Pattern
Command Observer Strategy
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
Page 38
Abstract Factory Motivation
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
Abstract Factory
AbstractFactory AbstractProductA
Client
CreateProductA
CreateProductB
ProductA1 ProductA2
ConcreteFactory1 AbstractProductB
CreateProductA
CreateProductB
ProductB1 ProductB2
ConcreteFactory2
Page 39
Applicability for Abstract Factory Pattern
♦ Independence from Initialization or Representation:
w The system should be independent of how its products are created,
composed or represented
♦ Manufacturer Independence:
w A system should be configured with one of multiple family of products
w You want to provide a class library for a customer (“facility management
library”), but you don’t want to reveal what particular product you are
using.
♦ Constraints on related products
w A family of related products is designed to be used together and you need
to enforce this constraint
♦ Cope with upcoming change:
w You use one particular product family, but you expect that the underlying
technology is changing very soon, and new products will appear on the
market.
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
Blinds
SiemensFactory
InitLightSystem
InitBlindSystem
InitACSystem
InstabusBlind ZumtobelBlind
Controller Controller
ZumtobelFactory
InitLightSystem
InitBlindsystem
InitACSystem
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
Page 40
Builder Pattern Motivation
♦ Conversion of documents
♦ Software companies make their money by introducing new
formats, forcing users to upgrades
w But you don’t want to upgrade your software every time there is an
update of the format for Word documents
♦ Idea: A reader for RTF format
w Convert RTF to many text formats (EMACS, Framemaker 4.0,
Framemaker 5.0, Framemaker 5.5, HTML, SGML, WordPerfect
3.5, WordPerfect 7.0, ….)
t Problem: The number of conversions is open-ended.
♦ Solution
w Configure the RTF Reader with a “builder” object that specializes
in conversions to any known format and can easily be extended to
deal with any new format appearing on the market
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
Builder Pattern
Director Builder
Construct() BuildPart()
ConcreteBuilderB Represen-
BuildPart()
tation B
GetResult()
ConcreteBuilderA
BuildPart()
GetResult() Represen-
tation A
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
Page 41
Example
RTFReader
Parse() TextConverter
ConvertCharacter()
ConvertFontChange
ConvertParagraph()
While (t = GetNextToken()) {
Switch t.Type {
CHAR: builder->ConvertCharacter(t.Char)
FONT: bulder->ConvertFont(t.Font)
PARA: builder->ConvertParagraph
}
}
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
Page 42
Abstract Factory vs Builder
♦ Abstract Factory
w Focuses on product family
t The products can be simple (“light bulb”) or complex
w The abstract factory does not hide the creation process
t The product is immediately returned
♦ Builder
w The underlying product needs to be constructed as part of the
system but is very complex
w The construction of the complex product changes from time to time
w The builder patterns hides the complex creation process from the
user:
t The product is returned after creation as a final step
♦ Abstract Factory and Builder work well together for a family of
multiple complex products
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
Summary
♦ Structural Patterns
w Focus: How objects are composed to form larger structures
w Problems solved:
t Realize new functionality from old functionality,
t Provide flexibility and extensibility
♦ Behavioral Patterns
w Focus: Algorithms and the assignment of responsibilities to objects
w Problem solved:
t Too tight coupling to a particular algorithm
♦ Creational Patterns
w Focus: Creation of complex objects
w Problems solved:
t Hide how complex objects are created and put together
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
Page 43
Conclusion
♦ Design patterns
w Provide solutions to common problems.
w Lead to extensible models and code.
w Can be used as is or as examples of interface inheritance and
delegation.
w Apply the same principles to structure and to behavior.
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
Page 44