Design Issues and Patterns - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

Design Issues and Patterns

Description:

... to using null to indicate the absence of an object to delegate an operation to. ... Delegation ... In Java 'event sources' delegate responsibility for processing an event to an ' ... – PowerPoint PPT presentation

Number of Views:23
Avg rating:3.0/5.0
Slides: 22
Provided by: Robert9
Category:

less

Transcript and Presenter's Notes

Title: Design Issues and Patterns


1
Design Issues and Patterns
  • CS2335
  • Fall 2003

2
The 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.
Faced with the same problem, the Russians used a
pencil.
3
Design Expertise
  • Architectural Styles
  • Frameworks
  • Design Patterns
  • Language Idioms

4
Common 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

5
Common 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

6
What are design patterns?
  • Proven solutions to common problems
  • Capture design expertise
  • Aid in meeting quality factors

7
Information 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?
8
Singleton
  • 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.
9
Singleton
public class Singleton private static
Singleton instance new Singleton() public
static Singleton getInstance() return
instance private Singleton()
- instanceSingleton
getInstance Singleton - Singleton
10
Singleton Problems
  • Garbage Collection
  • Use Object Preserver class
  • Anyone can get reference to class
  • Subclassing
  • Watch out for default public constructors

11
Factory
  • 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.

12
Factory
Socket
EncryptedSocket
Encryption
instanceEncryptFactorycipher Encryption
Encrypts/Decrypts with
encryptOut encryptIn
EncryptionFactory
DESEncryption
RSAEncryption
Requests Creation
CreateEncryption(Key) Encryption
Creates
13
Factory
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)
14
Command
  • 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
15
Command
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
16
Command
//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()
17
Null 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.

18
Null Object
0..
Processes
doIt
doIt
doIt
doIt
public class NullEvent public boolean doIt()
return true
19
Delegation
  • 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."

20
Delegation
0..1
0..1
0..1
Uses
Uses
Uses
Delegation avoids the subclass explosion problem.
21
Moral
  • Design patterns, styles and frameworks are
    great.
  • But dont design a 12 billion pen when a pencil
    will work just fine.
Write a Comment
User Comments (0)
About PowerShow.com