Gang of Four
Gang of Four
Wednesday, 31 October 12
Wednesday, 31 October 12
1. Adapter
2. Factory Method (& Abstract Factory)
3. Singleton
4. Observer
5. Command (& Command-Holder)
1. Adapter
Wednesday, 31 October 12
problem:
solution:
Wednesday, 31 October 12
problem:
solution:
from http://sourcemaking.com/design_patterns/adapter
Wednesday, 31 October 12
the wrapper takes the clients request and repackages it for LegacyRectangle
How to Apply
Wednesday, 31 October 12
the client can now use the new interface provided by the
wrapper class to access the functionality of the adaptee class.
Comment
Wednesday, 31 October 12
2. Factory Method
Wednesday, 31 October 12
problem:
solution:
Implementation
the client needs a new Product, so it asks the factory object for a new Product, providing
information about the type of Product it needs
the factory instantiates a new Concrete Product and returns it to the client (casted to abstract
Product class)
the client uses the Products it is given as abstract Products without being aware of their concrete
implementation
we can add more products (subtypes) without altering the clients code
from http://www.oodesign.com/factory-pattern.html
Wednesday, 31 October 12
Example: Shapes
DrawingFramewk
Shape
a graphical application:
abstract Shape class defines the draw and move operations which must be implemented by the
concrete shapes
for example, the user selects to create a circle and specifies its radius
the factory creates a new Circle and returns it to the client cast as an abstract Shape
Wednesday, 31 October 12
Circle
createShape( String
type, double []
parameters)
the framework receives the parameters circle and 2.5 and asks the factory to create a shape
based upon them
adapted from http://www.oodesign.com/factory-pattern.html
implements operations to
make concrete Products
adapted from http://www.oodesign.com/factory-pattern.html
Wednesday, 31 October 12
Implementation
Wednesday, 31 October 12
AbstractFactory:
determines the actual types of each concrete object and creates them
returns abstract pointers to the objects created
Client:
those objects will have differing formats depending upon which country the
customer lives in
so we can create factories for each country (or format), and point the Order class
to the correct one based upon the customers place of residence
Issues
Wednesday, 31 October 12
Pros:
Cons:
3. Singleton
problem:
Wednesday, 31 October 12
a print handler
a factory which issues unique ids to the objects which it creates
solution:
if lots of clients need visibility to a single instance of a class, then we could just
achieve that by:
its far easier to instead provide a globally visible access point to the single instance
if we only need one instance of a class in our design, used by many objects, then
who should create it?
Wednesday, 31 October 12
Example
the underlining tells us that the attribute
or method is static
MoleculeFactory
instance : MoleculeFactory
- MoleculeFactory()
+ getInstance() : MoleculeFactory
Wednesday, 31 October 12
Lazy or Eager?
Lazy Initialisation:
public static MoleculeFactory getInstance()
{
if ( instance == null )
{
instance = new MoleculeFactory();
}
return instance;
}
Wednesday, 31 October 12
Eager Initialisation
instance = new MoleculeFactory();
...
public static MoleculeFactory getInstance()
{
return instance;
}
notice that the single instance is not initialised until the getInstance method is called
creation work is avoided if the instance is never actually accessed during runtime
initialisation could require complex or conditional creation logic
Wednesday, 31 October 12
it permits subclassing
Issues
Wednesday, 31 October 12
Issues
Wednesday, 31 October 12
4. Observer
Wednesday, 31 October 12
problem:
solution:
Wednesday, 31 October 12
if more classes need to change then we have to add more code to our
program to follow any changes to the subject
Observer Pattern
Observer
<<interface>>
Subject
<<abstract>>
observers : ArrayList<Observer>
register( Observer) : void
unregister( Observer) : void
notifyObservers( Object) : void
Wednesday, 31 October 12
Wednesday, 31 October 12
by email
through their account summary
the seller, current highest bidder and previous highest bidder need to be told too
the nature and frequency of these updates will depend upon each customers
contact preferences
Item
itemId : ID
description: String
currentHighestBid: Money
observers : ArrayList<Observer>
register( Observer) : void
unregister( Observer) : void
notifyObservers( Object) : void
...
observe
DisplayListingUI
Observer
<<interface>>
notify( Subject, Object) : void
ItemsDB
Customer
Seller
...
...
...
...
...
...
...
...
Benefits
Wednesday, 31 October 12
coupling is minimised:
Benefits
Wednesday, 31 October 12
Problems
Wednesday, 31 October 12
timing issues:
Problems
Wednesday, 31 October 12
performance issues:
Problems
Wednesday, 31 October 12
5. Command
Wednesday, 31 October 12
problem:
it often also becomes the repository for all the logic which
responds to user actions
solution:
Command Interface
public interface Command
{
public void execute();
}
Wednesday, 31 October 12
pause
resume
using Command:
Wednesday, 31 October 12
Wednesday, 31 October 12
CommandHolder Interface
public interface CommandHolder
{
public void setCommand( Command command);
public Command getCommand();
}
all the working code now resides within separate Command classes
Wednesday, 31 October 12
pause
resume
using CommandHolder:
inside the main UI class:
Benefits
Wednesday, 31 October 12
To conclude
the five (and a bit) patterns investigated here build upon the core
GRASP principles
taken together they can lead to clean, cohesive, lightly coupled code
Wednesday, 31 October 12
easy to read
easy to re-use
easy to change
you can create your own java libraries for some of the interfaces
as always, use appropriately
Wednesday, 31 October 12
Adapter
http://sourcemaking.com/design_patterns/adapter
Factory Method
http://www.oodesign.com/factory-pattern.html
http://www.oodesign.com/abstract-factory-pattern.html
Singleton
http://www.ibm.com/developerworks/java/library/j-dcl/index.html
Observer Pattern
http://www.research.ibm.com/designpatterns/example.htm
Command Holder