SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler

Download Report

Transcript SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler

SOEN 343
Software Design
Section H Fall 2006
Dr Greg Butler
http://www.cs.concordia.ca/~gregb/home/soen343h-f06.html
Outline
• GoF Design Patterns
– Adapter, Façade, Strategy, Observer
• Design Review
GoF Pattern
Summary
&
Relationships
GoF Pattern Classification
• Behavioral Patterns
• Creational Patterns
• Structural Patterns
GoF Behavioral Patterns
• Chain of
Responsibility
• Command
• Interpreter
• Iterator
• Mediator
• Memento
•
•
•
•
•
Observer
State
Strategy
Template Method
Visitor
GoF Creational Patterns
•
•
•
•
•
Abstract Factory
Builder
Factory Method
Prototype
Singleton
GoF Structural Patterns
•
•
•
•
•
•
•
Adapter
Bridge
Composite
Decorator
Facade
Flyweight
Proxy
Adapter
• Context / problem
How to resolve incompatible interfaces, or
provide a stable interface to similar
components with different interfaces?
• Solution:
Convert the original interface of a
component into another interface, through
an intermediate adapter object.
Adapter
• Suppose we have a tax calculation class
(or external library) but the interface is not
well suited for our application.
GoodAsGoldTaxPro
+computeTax() : double
Adapter
• Adapter provides
an interface
suited
to the application
GoodAsGoldTaxProAdapter
getTaxes( Sale ) : List of TaxLineItems
GoodAsGoldTaxPro
computeTax(…):double
Adapter
(For More than One Class)
• What if more than one class (library)
needs to be adapted?
«interface»
ITaxCalculatorAdapter
Adapter
getTaxes( Sale ) : List of TaxLineItems
TaxMasterAdapter
GoodAsGoldTaxPro
Adapter
getTaxes( Sale ) : List of TaxLineItems
getTaxes( Sale ) : List of TaxLineItems
*
*
GoodAsGoldTaxPro
+computeTax() : double
Facade
Facade
Strategy
• Context / problem:
How to design for varying, but related,
algorithms or policies? How to design for
the ability to change (even dynamically)
these algorithms or policies?
• Solution:
Define each algorithm/policy/strategy in a
separate class with a common interface
Strategy
PercentDiscount
PricingStrategy
«interface»
ISalePricingStrategy
getTotal( Sale ) : Money
AbsoluteDiscount
OverThreshold
PricingStrategy
percentage : float
getTotal( s:Sale ) : Money
discount : Money
threshold : Money
getTotal( s:Sale ) : Money
{
return s.getPreDiscountTotal() * percentage
}
{
pdt := s.getPreDiscountTotal()
if ( pdt < threshold )
return pdt
else
return pdt - discount
}
How Do We Create a Strategy?
1
PricingStrategyFactory
instance : PricingStrategyFactory
getInstance() : PricingStrategyFactory
getSalePricingStrategy() : ISalePricingStrategy
getSeniorPricingStrategy() : ISalePricingStrategy
...
{
String className = System.getProperty( "salepricingstrategy.class.name" );
strategy = (ISalePricingStrategy) Class.forName( className ).newInstance();
return strategy;
}
Observer Pattern
Goal: When the total of the sale
changes, refresh the display with
the new value
Sale
total
...
setTotal( newTotal )
...
Observer
• How shall we have the display be
updated?
• Why not …
have the Sale inform the display when it
changes value.
What is Wrong
With This?
update( )
Sale
total
...
setTotal( newTotal )
...
Observer Pattern
• Context / Problem:
Different kinds of subscriber objects are
interested in the state changes or events
of a publisher object, and want to react in
their own way when the publisher
generates the event. …
Observer Pattern
• Solution:
Define a “subscriber” or “listener”
interface. Subscribers implement this
interface. The publisher can dynamically
register subscribers who are interested in
an event, and notify them when an event
occurs.
• Clarification: Publisher can dynamically
process registration requests from
subscribers.
Observer Pattern (GoF book)
Observers: Illustration
Sale
Sale Example
addPropertyListener( PropertyListener lis )
publishPropertyEvent( name, value )
setTotal( Money newTotal )
...
propertyListeners
javax.swing.JFrame
*
...
setTitle()
setVisible()
...
«interface»
PropertyListener
onPropertyEvent( source, name, value )
SaleFrame1
onPropertyEvent( source, name, value )
initialize( Sale sale )
...
Recall …
• Way back in week 5 we asked the
question …
How do I come up with a
design?
How Do I Come Up With a
Design?
• What are the inputs to the activities of
design?
• What are the outputs?
Design Activities: Inputs
& Outputs
• Product requirements
• Architectural factors …
• ArchitectureDocument
• Project plan
– Architectural Style(s)
• …
Design Input: SRS
• What is contained in the SRS?
(name some of its parts)
Design: From SRS to ...
• Domain Model
• Design Model
• Use Case Model
• Control Style / Pattern
• User-interface design
…
Design Model Classes
• Domain Model often used as first
approximation to Design Model.
• Domain Model contains
– Conceptual classes
• Conceptual classes used as basis for
similarly named design classes.
• (E.g. Larman, Chapter 16, p. 222.)
Data Source Domain Presentation
EA Patterns
Page Controller
Template View
Front Controller
Transform View
Domain Model
Transaction Script
Data Mapper
Active Record
Table Module
Row Data Gateway
Table Data Gateway
Enterprise Applications:Domain
Model
• Requirements
Domain Model
• Design Model:
classes Domain
Model pattern
From Requirements to Design
Requirements
artifacts: e.g.
Design artifacts: e.g.
• Software Architecture
Document
• Domain Model
• Design Model
• Use Case Model
• User-interface design …
Conceptual Classes to Design Classes
Payment
amount
Sale
1
Pays-for
1
date
time
• Domain
Model
pattern
Sale
Payment
amount: Money
getBalance(): Money
date: Date
startTime: Time
getTotal(): Money
...
Representational Gap
• Larman, Section 17.2:
The object developer has taken inspiration
from the real-world domain in creating
software classes. Therefore, the
representational gap between how
stakeholders conceive the domain, and its
representation in software, has been
lowered.
From Rich Conceptual Domain Model
to Rich set of Domain Model classes
Records-sale-of
Described-by
Store
1
address : Address
1
Product
name : Text
Catalog
Contains
description ProductCatalog
1
addSale(...)
1..*
price
...
1
Looks-in itemID
1
1
Used-by
getSpecification(...)
Describes
1
0..1
*
*
Sales
LineItem
1
1..*
1
*
Sale
Captured-on
time
Payment
amount
1
1
1
Initiated-by
1
Customer
1..*
1
...
Describes
Sale
1..*
1
1
becomeComplete()
makeLineItem(...)
Manager
makePayment(...)
getTotal()
*
 Records-sales-on
*
date : Date
isComplete : Boolean
time : Time
Captures
SalesLineItem
Contains
1..*
1
Cashier
quantity : Integer
getSubtotal()
1
Payment
Paid-by
1
1
description : Text
price : Money
itemID : ItemID
1
*
Logs-completed
1
Contains
Item
Stocks
enterItem(...)
Register
makeNewSale()
makePayment(...) Started-by
1
1
date
1
1
1
Logs1
Houses
completed
 endSale() 1..*
Contained-in
1
1
address
name
Register
ProductSpecification
*
Houses
Store
quantity
Paid-by
1
Uses
Product
Specification
amount : Money
...