Basic OO
design patterns
This page is published under the terms of the licence summarized in the footnote.
This paper continues our discussion of design patterns with a focus on OO design patterns commonly used in web applications.
Contents
Proxy
(aka Surrogate or Ambassador)
Singleton
– a single-object
class
Other design patterns in the
GoF’s famous book
An
anti-OO pattern: point-to-point coupling
Software architecture is about specifying data processing needs, and describing how components will be organised to cooperate in the required processes.
To design a large and complex system – one that is successful and easy to manage - takes more than description.
The architect has to make a series of modular design decisions.
· What is the right size and scope of a component?
· How to avoid or minimise duplication between components?
· How to separate or distribute components?
· How integrate components?
The architect’s tool kit includes pre-defined OO design patterns..
Common
OO design patterns, used by architects of web-oriented SOA implementations are:
There
are overlaps between some patterns
But what follows may help you understand software architecture thinking.
A general idea underpins many OO design patterns.
Problem: A client wants something a server will not do directly, or wants to be hidden from server details
Solution: Introduce a broker or intermediary component between client and server "adding a level of indirection".
Façade is a frontage to a building; an interface that sits in front of a system.
Essentials
Problem: How can a client avoid being too tightly coupled with a server subsystem?
How to aggregate services into a coarser-grained component?
Solution: introduce a façade that provides a unified interface for different clients, and makes the subsystem easier to use
So clients know what the subsystem does for them, but nothing else
Trade offs
About a façade component
The façade pattern takes the interface to a substantial system of components and puts the interface inside a component that does nothing else but present the interface.
Essentials
Problem: How can a client avoid knowing server details, be shielded from server changes, call a server that does not have exactly the interface required?
Solution: Introduce an adapter to convert the server’s offered interface into the one that is required.
Essentials
Problem: How can a client avoid dependence on a specific object representation or implementation?
Invoke a server in a different name space, or a server to which access must be restricted in some way?
Solution: Introduce a proxy (on the client side) to forward the request and control access.
A proxy can hide and handle the distribution of code to different name spaces; this principle underlies CORBA and DCOM.
In Distributed Objects, Object Request Broker establishes the proxies which manage the remote procedure call.
We only
need one copy of:
So, a
single-object class can hold the data or do the job.
How to recognise it?
The
observer pattern comprises two kinds of component.
A subject:
notifies observers of changes to its state
Observers:
registers with the subject to be notified of changes. Unregisters when it lose
interest.
This table lists about half the patterns (including
the ones mentioned above).
Name |
Description or purpose |
Singleton |
A component (or class) with only one instance (or object). |
Façade |
Consistent interface coarse-grained services that encapsulate a suibsystem |
Proxy |
A surrogate for a distributed component. Used in distribution of code between different name spaces. |
Adapter |
A wrapper that converts a provided service into a required service. Facilitates the reuse of existing technologies. |
Observer |
A component that monitors the state of another component. Curiously, the subject of observation may be an object. |
Composite |
Enables a class to process operations on every level of a hierarchical structure, including composite and leaf nodes |
Template method |
Offers several variations of an algorithm. |
Strategy |
Adds a façade to a template method. |
Manager |
Often used to manage a set of objects. |
Factory method |
Create the right server object for the client - hiding how the server object is initialized. |
Abstract object factory |
Create the right factory object for the client - hiding which factory class is used. |
Adding a level of indirection underpins many OO design patterns
But sometimes, the anti-pattern is better.
Problem: A broker cannot entirely shield a client from server changes. A broker requires some redundant coding and incur some communication costs.
Solution: There are times when it is simpler and cheaper for the client to call the server directly, accept the coupling this involves.
(Sometimes house seller and buyer are better off talking to each other without an estate agent getting in the way.)
Footnote: Creative Commons Attribution-No Derivative Works Licence 2.0
Attribution: You may copy, distribute and display this copyrighted work
only if you clearly credit “Avancier Limited: http://avancier.co.uk” before the start and
include this footnote at the end.
No Derivative Works: You may copy, distribute, display only complete and verbatim
copies of this page, not derivative works based upon it.
For more information about the
licence, see http://creativecommons.org