7.GoF Design Patterns
7.GoF Design Patterns
TaxMasterAdapter GoodAsGoldTaxPro
Adapter
«interface» «interface»
IAccountingAdapter ICreditAuthorizationService
Adapter
postReceivable( CreditPayment )
postSale( Sale ) requestApproval(CreditPayment,TerminalID, MerchantID)
... ...
«interface»
IInventoryAdapter
SAPAccountingAdapter GreatNorthernAccountingAdapter
...
makePayment
...
SOAP over
HTTP
postSale( sale )
xxx «actor»
: SAPSystem
GoF Design
Adapter Patterns
Factory Pattern
Problem: Who should be responsible for
creating objects when there are special
considerations such as complex creation
logic, a desire to separate creation
responsibilities for better cohesion, etc.?
getAccountingAdapter() : IAccountingAdapter
getInventoryAdapter() : IInventoryAdapter
getTaxCalculatorAdapter() : ITaxCalculatorAdapter
...
if ( taxCalculatorAdapter == null )
{
// a reflective or data-driven approach to finding the right class: read it from an
// external property
}
return taxCalculatorAdapter;
Factory Pattern (26.4)
Note: In Fig. 26.5 the implementation of
ServicesFactory illustrates data-driven
design – a form of Protected Variation
Factory Pattern (26.4)
Idea: Define an object whose purpose is to
create objects
Benefits:
– Separate the responsibility of complex
creation into cohesive helper objects
– Can provide object caching (e.g. having only
one random number generator)
Singleton Pattern (26.5)
Problem: Exactly one instance of a class is
allowed. Objects need a global and single
point of access.
1
ServicesFactory
// static method
public static synchronized ServicesFactory getInstance()
{
if ( instance == null )
instance = new ServicesFactory()
return instance
}
Singleton Pattern (26.5)
Note: concurrency control in
ServicesFactory – making getInstance()
synchronized
create
create :Register
accountingAdapter =
getAccountingAdapter
create : SAPAccounting
Adapter
accountingAdapter:
:Register
SAPAccountingAdapter
makePayment
create(cashTendered) : Payment SOAP over
HTTP
postSale( sale )
xxx «actor»
: SAPSystem
Observer Pattern (26.10)
Also known as Publish-Subscribe Pattern
Sale
total
...
setTotal( newTotal )
...
Observer Pattern (26.10)
Example (cont):
Simple solution – when Sale changes its
total the object sends a message to the
window telling it to refresh its display
Sale
javax.swing.JFrame propertyListeners
*
... «interface»
setTitle() PropertyListener
setVisible()
... onPropertyEvent( source, name, value )
{
if ( name.equals("sale.total") )
saleTextField.setText( value.toString() );
}
SaleFrame1
sf : SaleFrame1 propertyListeners :
s : Sale
List<PropertyListener>
initialize( s : Sale )
addPropertyListener( sf )
add( sf )
Observer Pattern (26.10)
Notes:
– When the Sale total changes it iterates over
all registered subscribers and sends the
onPropertyEvent message to each – Figs.
26.24, 26.25
Fig. 26.24
s :Sale propertylisteners[ i ] :
PropertyListener
setTotal( total )
publishPropertyEvent
( "sale.total", total )
setText( value.toString() )
javax.swing.JFrame propertyListeners
*
... «interface»
setTitle() PropertyListener
setVisible()
... onPropertyEvent( source, name, value )
setTime( newTime ) {
... time = newTime;
if ( time == alarmTime )
publishAlarmEvent( time );
}
javax.swing.JFrame alarmListeners
*
... «interface»
setTitle() AlarmListener
setVisible()
... onAlarmEvent( source, time )
{ {
{
display notification dialog check that all required processes
beep
box are executing normally
}
} }
Summary
• Singleton:
– Factory for a singular (sole) instance
– Ensures that only one object of a particular class is
ever created.
• Adapter:
– Translator adapts a server interface for a client
– Used to provide link between two or incompatible
types by wrapping with a class that supports the
interface required by the client.
Observer Pattern:
– Used to allow an object to publish changes to its
state.
– Other objects subscribe to be immediately notified
of any changes.
– Dependents update automatically when subject
changes
• Factory: