Title: CSSE374 Applying GoF Design Patterns
1Below The Gang of Four at OOPSLA 1994, shortly
after publication of their Patterns book. Left
to right are Ralph Johnson, Erich Gamma, Richard
Helm, and John Vlissides. From
http//c2.com/ppr/wiki/ComponentDesignPatterns/Joh
nVlissides.html.
CSSE374 Applying GoF Design Patterns
Steve Chenoweth Day 22, Jan 20, 2009
2Today this week
- How was the inauguration?
- If I didnt see your tests run yesterday, lets
see! - GoF patterns, Ch 26 ?
- Thursday Friday Other design tools Activity
diagrams State machines - Friday -- P5. Strategic goal of this assignment
Brings back memories of past ones
Req done by clients
Save some impl for P6!
Des done thru P5
Impl done thru P5
3What GoF patterns are in Ch 26?
- Adapter
- Factory (a start on one GoF pattern)
- Singleton
- Strategy
- Composite
- Façade
- Observer/Publish-Subscribe/Delegation Event
Model - Book examples are from NextGen POS case study.
- I added a few here from the GoF book. They use a
document editor example.
4Adapter
- Problem How to resolve incompatible interfaces,
or provide a stable interface to similar
components with different interfaces? - GoFs Motivation Sometimes a toolkit class
that's designed for reuse isn't reusable only
because its interface doesn't match the
domain-specific interface an application
requires. - Solution (advice) Convert the original
interface of a component into another interface,
through an intermediate adapter object.
5Adapter
Fig 26.1
6Adapter
- Discussion Adapter is a wrapper.
- It adapts a needed interface to one of several
possible service providers. - Looks a lot like other patterns weve seen. ?
- Looks a lot like façade coming up.
7Adapter
- Heres the GoFs document editor example of
adapter, and accompanying explanation
8Factory
- Problem Who should be responsible for creating
objects when there are special considerations,
such as complex creation logic, a desire to
separate the creation responsibilities for better
cohesion, and so forth? - Solution (advice) Create a Pure Fabrication
object called a Factory that handles the creation.
9Factory
Fig 26.5
Cool code
10Factory
- Advantages
- Separate the responsibility of complex creation
into cohesive helper objects. - Hide potentially complex creation logic.
- Allow introduction of performance-enhancing
memory management strategies, such as object
caching or recycling.
11Singleton
- Problem Exactly one instance of a class is
allowed it is a singleton. Objects need a
global and single point of access. - GoFs Motivation How do we ensure that a
class has only one instance and that the instance
is easily accessible? A global variable makes an
object accessible, but it doesn't keep you from
instantiating multiple objects. - Solution (advice) Define a static method of the
class that returns the singleton.
Note We introduced Singletons back in Ch 15 (p.
227).
12Singleton
Fig 26.6
13Singleton
- Advantage The obvious one is that one class
controls a resource. Say, a printer, or access
to the DB. - Disadvantage The obvious one is that singletons
produce bottlenecks in what are otherwise
multi-tasking systems. - Adapter, factory and singleton patterns are used
together to manage interfaces to systems.
Question How many Singletons are in the NBA?
14Strategy
- Problem How to design for varying, but related,
algorithms or policies? How to design for the
ability to change these algorithms or policies? - GoFs Motivation (in their Document Editor
example) Many algorithms exist for breaking a
stream of text into lines. Hard-wiring all such
algorithms into the classes that require them
isn't desirable - Solution (advice) Define each algorithm /
policy / strategy in a separate class, with a
common interface.
15Strategy
Fig 26.9
16Strategy
- Heres the GoFs document editor example of
strategy, and accompanying explanation
17Composite
- Problem How to treat a group or composition
structure of objects the same way
(polymorphically) as a non-composite (atomic)
object? - GoFs Motivation Having to distinguish these
objects makes the application more complex. - Solution (advice) Define classes for composite
and atomic objects so that they implement the
same interface.
18Composite
Fig 26.14
19Composite
- Heres the GoFs document editor example of
composite, and accompanying explanation
20Façade
- Problem A common, unified interface to a
disparate set of implementations or
interfacessuch as within a subsystemis
required. There may be undesirable coupling to
many things in the subsystem, or the
implementation of the subsystem may change. What
to do? - GoFs Motivation One way to simplify
applications is to introduce a façade object that
provides a single, simplified interface to the
more general facilities of a subsystem it uses. - Solution (advice) Define a single point of
contact to the subsystema façade object that
wraps the subsystem. This façade object presents
a single unified interface and is responsible for
collaborating with the subsystem components.
21Façade
Fig 26.20
22Observer/Publish-Subscribe/Delegation Event Model
- 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 unique way when the publisher generates an
event. Moreover, the publisher wants to maintain
low coupling to the subscribers. What to do? - GoFs Motivation A common side-effect of
partitioning a system into a collection of
cooperating classes is the need to maintain
consistency between related objects. You don't
want to achieve consistency by making the classes
tightly coupled, because that reduces their
reusability. - Solution (advice) 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.
23Observer/Publish-Subscribe/Delegation Event Model
Fig 26.22
24Observer
- Heres the GoFs example of observer, and
accompanying explanation
25Publish / Subscribe
- Many communications systems work like this.
- E.g., you subscribe for an RSS feed, expressing
an interest in some particular data. - The subscribers are observers.
- Such a theme also can be at the heart of a
system, like CORBA. - Providers also sign-up, saying what services they
can do for subscribers. - The broker goes in the middle. ?
26Publish / Subscribe Example
- Provider-Subscriber systems used for event
management, and more
Managed System
S1
P1
DB
Connection to Broker
Connection to Broker
Services Provided
Subscriptions
Web Client
Subscription Broker Architecture
S2
Subscription Distribution
Connection to Broker
Subscriptions
Managed System
Another System
P2
S3
Connection to Broker
Connection to Broker
Services Provided
Subscriptions
Ss subscribers to information. Ps
providers of information. Think RSS Feed, or
Object Request Brokers like CORBA.