Title: Design Issues and Patterns
1Design Issues and Patterns
2The Joy of Design
When sending astronauts into outer space NASA
discovered a problem. Ballpoint pens would not
write in zero gravity. NASA undertook a 12
billion program to develop a pen that writes in
zero gravity, upside down, underwater, on any
surface including glass, and in temperatures from
32 to 300 F.
THIS IS ACTUALLY AN URBAN LEGEND, BUT IT STILL
ILLUSTRATES THE IDEA SO WELL I LEFT IT IN.
Faced with the same problem, the Russians used a
pencil.
3Design Expertise
- Architectural Styles
- Frameworks
- Design Patterns
- Language Idioms
4Common Architectural Styles
- Model-View-Controller, Document-View
- Pipe and Filter
- Layered
- Implicit Invocation (Event-Driven)
- Blackboard
- Shared Memory
- Client-Server (Fat)
- Client-Server (Thin)
- N-tier
5Common Frameworks
- MFC (C) Windows Development
- JFC (Java) Java GUI Development
- Fox, Wx, Qt Cross-Platform GUI Development
- CORBA, ATL Distributed Development
- OpenDoc
- ACE Communications Development
6What are design patterns?
- Proven solutions to common problems
- Capture design expertise
- Aid in meeting quality factors
7Information Expert
- Assign a responsibility to the class that has
the information necessary to fulfill the
responsibility.
Start assigning responsibilities by clearly
stating the responsibility.
Who should be responsible for knowing the grand
total of a sale?
Who should be responsible for saving the Sale in
the database?
8Singleton
- Problem I want to limit the application to only
one instance of a particular class, but need
global access to that class. - Normally used to control access to key resources.
- Solution?
Make constructor protected, make static accessor
method.
9Singleton
public class Singleton private static
Singleton instance new Singleton() public
static Singleton getInstance() return
instance private Singleton()
- instanceSingleton
getInstance Singleton - Singleton
10Singleton Problems
- Garbage Collection
- Use Object Preserver class
- Anyone can get reference to class
- Subclassing
- Watch out for default public constructors
11Factory
- Define an interface for creating an object, but
let subclasses decide which class to instantiate.
Allows a class to defer instantiation to
subclasses. - Think of a multi-document application framework.
An application object may know when an object
needs to be created, but not which object. How
do we create the correct object when needed? - Can also be used when a complex initialization of
objects is necessary, for instance when
aggregation is heavily used. - Can also be used to take advantage of
memory-optimization like object pools, cached
objects, etc.
12Factory
Socket
EncryptedSocket
Encryption
instanceIEncryptFactorycipher Encryption
Encrypts/Decrypts with
encryptOut encryptIn
EncryptionFactory
DESEncryption
RSAEncryption
Requests Creation
CreateEncryption(Key) Encryption
Creates
13Factory
public interface IEncryptionFactory public
Encryption createEncryption(Key key) throws
NoSuchAlgorithm public class
EncryptionFactory implements IEncryptionFactory
public Encryption createEncryption(Key key)
throws NoSuchAlgorithm String
algorithm key.getAlgorithm() if
(DES.equals(algorithm)) return new
DESEncryption(key) if
(RSA.equals(algorithm)) return new
RSAEncryption(key) throw new
NoSuchAlgorithmException(algorithm)
14Command
- Encapsulate commands in objects, so we can queue
them, undo them or make macros.
managerCmdMgr
doIt()bool undoIt()bool
data
doIt()bool undoIt()bool
doIt()bool undoIt()bool
15Command
public abstract class AbstractCommand
public final static CommandManager manager new
CommandManager() public abstract boolean
doIt() public abstract boolean
undoIt() public class InsertCommand extends
AbstractCommand protected Document
document protected int pos protected
String str InsertCommand(Document d, int p,
String s) documentd public boolean
doIt() document.insert(pos,str)
return true //undoable public boolean
undoIt() document.delete(pos,str.length()
) return true
16Command
//this would possibly be a good candidate for
singleton class also Class CommandManager
private int maxHistory 100 private List
history new LinkedList() private List
redoList new LinkedList() public void
invokeCommand(AbstractCommand command)
if (command instanceOf Undo) undo() return
if (command instanceOf Redo) redo()
return if (command.doIt())
addToHistory(command) else
history.clear()
17Observer Pattern
- Allows objects to dynamically register their
interest in being notified of any changes that
occur in the state of the observed object.
18Observer
ltltinterfacegtgt Observable
registers with
addObserver(oObserver) removeObserver(oObserver)
Sender
public class Monitor implements Observer
public void notify(Observable o) Data d
o.getState()
public class Sender implements Observable
public Sender() addObservable(new
Monitor())
19Null Object
- An alternative to using null to indicate the
absence of an object to delegate an operation to.
Can eliminate a test for null by using an object
that doesnt do anything. - This allows objects to always behave the same
way, i.e. use an object without any additional
tests required.
20Null Object
0..
Processes
doIt
doIt
doIt
doIt
public class NullEvent public boolean doIt()
return true
21Delegation
- A way to extend and use the functionality of a
class by writing an additional class with added
functionality that uses instances of the original
class to provide original functionality. - An alternative to inheritance. Used to handle
multiple roles. - In Java event sources delegate responsibility
for processing an event to an event listener."
22Delegation
0..1
0..1
0..1
Uses
Uses
Uses
Delegation avoids the subclass explosion problem.
23Moral
- Design patterns, styles and frameworks are
great. - But dont design a 12 billion pen when a pencil
will work just fine. - For Final Project, if you implement a non-trivial
design pattern, it is worth extra credit, points
depend on implementation complexity.