Top Banner
Design Patterns – Eran Toch Methodologies in Information System Development Design Patterns: Design Patterns: Talking in the Design Language Talking in the Design Language Eran Toch Methodologies in the Development of Information Systems
35
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Design patterns

Design Patterns – Eran TochMethodologies in Information System Development

Design Patterns:Design Patterns:

Talking in the Design LanguageTalking in the Design Language

Eran TochMethodologies in the Development

of Information Systems

Page 2: Design patterns

Design Patterns – Eran TochMethodologies in Information System Development

2November 2003

AgendaAgenda

• What is a Design Pattern?• Structural Patterns

– The Composite Pattern

• Behavioral Patterns– The Strategy Pattern

• Creational Patterns– The Singleton Pattern

• Human Interaction Patterns– Go Back to a Safe Place

• Summary

Page 3: Design patterns

Design Patterns – Eran TochMethodologies in Information System Development

3November 2003

What is a Design Pattern?What is a Design Pattern?

In Short, a solution for a typical problem

“a description of a recurrent problem and of the core of possible solutions.“

Page 4: Design patterns

Design Patterns – Eran TochMethodologies in Information System Development

4November 2003

Why do we need them?Why do we need them?

• Problems are not always unique. Reusing existing experience might be proved useful.

• We need a common language to describe problems and solutions.

• The Design of Object-Oriented systems limits the scope of problems and solutions we can talk about

Page 5: Design patterns

Design Patterns – Eran TochMethodologies in Information System Development

5November 2003

History of Design PatternsHistory of Design Patterns

Christopher AlexanderA Pattern Language: Towns, Buildings Construction 1970’

1995’

2000’

Architecture

Object OrientedSoftware Design

Other Areas:HCI, Organizational Behavior…

Gang of Four (GoF)Design Patterns: Elements of Reusable Object-Oriented Software

Many Authors

Page 6: Design patterns

Design Patterns – Eran TochMethodologies in Information System Development

6November 2003

Structure of a design patternStructure of a design pattern

• Pattern Name and Classification

• Intent– a Short statement about what the pattern does

• Motivation– A scenario that illustrates where the pattern would be

useful

• Applicability– Situations where the pattern can be used

Page 7: Design patterns

Design Patterns – Eran TochMethodologies in Information System Development

7November 2003

Structure of a design pattern – Structure of a design pattern – cont’dcont’d

• Structure– A graphical representation of the pattern

• Participants– The classes and objects participating in the pattern

• Collaborations– How to do the participants interact to carry out their

responsibilities?

• Consequences– What are the pros and cons of using the pattern?

• Implementation– Hints and techniques for implementing the pattern

Page 8: Design patterns

Design Patterns – Eran TochMethodologies in Information System Development

8November 2003

Classification of patternsClassification of patterns

• Structural– Composite*, Decorator, Façade, Adapter…

• Behavioral– Strategy*, Command, Observer, Chain of

Responsibilities…

• Creational– Singleton*, Abstract Factory, Factory Method…

• Other– User interface Patterns

• Go Back to a Safe Place

Page 9: Design patterns

Design Patterns – Eran TochMethodologies in Information System Development

9November 2003

AgendaAgenda

• What is a Design Pattern?

• Structural Patterns

• Behavioral Patterns

• Creational Patterns

• Human Interaction Patterns

• Summary

Page 10: Design patterns

Design Patterns – Eran TochMethodologies in Information System Development

10November 2003

The Composite PatternThe Composite Pattern

• Intent:– Compose objects into tree structures to represent

part-whole hierarchies.– Composite lets clients treat individual objects and

compositions of objects uniformly. This is called recursive composition.

Page 11: Design patterns

Design Patterns – Eran TochMethodologies in Information System Development

11November 2003

MotivationMotivation

When Processing the hierarchal collection, we need to query the type of the object (composite or primitive) and act differently

Diagram reference: Design Patterns [Gof]

Page 12: Design patterns

Design Patterns – Eran TochMethodologies in Information System Development

12November 2003

Composite: ApplicabilityComposite: Applicability

• Use the Composite pattern when:– You want to represent part-whole hierarchies of

objects.

– You want clients to be able to ignore the difference between compositions of objects and individual objects. Clients will treat all objects in the composite structure uniformly.

Page 13: Design patterns

Design Patterns – Eran TochMethodologies in Information System Development

13November 2003

Composite: StructureComposite: Structure

• Define an abstract base class that specifies the uniform behavior.

• Subclass the Primitive and Composite classes of the Component class.

Diagram reference: Design Patterns [Gof]

Page 14: Design patterns

Design Patterns – Eran TochMethodologies in Information System Development

14November 2003

Composite: ConsequencesComposite: Consequences

• Benefits– It makes it easy to add new kinds of components– It makes clients simpler, since they do not have to

know if they are dealing with a leaf or a composite component

• Liabilities– It makes it harder to restrict the type of components of

a composite

Page 15: Design patterns

Design Patterns – Eran TochMethodologies in Information System Development

15November 2003

Composite: Known UsesComposite: Known Uses

1. A GUI system has window objects which can contain various GUI components (widgets) such as, buttons and text areas. A window can also contain widget container objects which can hold other widgets.Concrete examples: Java Swing, MFC

2. Folder based file system3. Arithmetic expressions

Window

Tab Another Tab

Another Window

OKCancel

ButtonCancel

OK

op 1

op 2

Page 16: Design patterns

Design Patterns – Eran TochMethodologies in Information System Development

16November 2003

AgendaAgenda

• What is a Design Pattern?

• Structural Patterns

• Behavioral Patterns

• Creational Patterns

• Human Interaction Patterns

• Summary

Page 17: Design patterns

Design Patterns – Eran TochMethodologies in Information System Development

17November 2003

The Strategy PatternThe Strategy Pattern

• Intent:– Define a family of algorithms, encapsulate each one,

and make them interchangeable. – Strategy lets the algorithm vary independently from

clients that use it.

Page 18: Design patterns

Design Patterns – Eran TochMethodologies in Information System Development

18November 2003

Strategy: MotivationStrategy: Motivation

The client wants to use the algorithm without knowing which one to use in advance. We want to add new algorithms without changing the client class.

Client

check() : Grade

Questionuses

Check() : Grade

MultipleChoice

Check() : Grade

MatchingQuestion

switch (type) {case multiple: question = new MultipleChoice(); break;case matching: question = new MatchingQuestion(); break;}

Page 19: Design patterns

Design Patterns – Eran TochMethodologies in Information System Development

19November 2003

Strategy: StructureStrategy: Structure

Client

check() : Grade

Questionuses

Check() : Grade

MultipleChoice

Check() : Grade

MatchingQuestion

question = new Question();question.check()

check() : Grade

CheckingAlgorithm

check() { return CheckingAlgorythm.grade();}

• Encapsulate the behavior of the context class in a separate algorithm class.

• The context class keeps a reference to the algorithm class and passes on the messages.

• The algorithm class can be sub-classed to support multiple algorithms.

Page 20: Design patterns

Design Patterns – Eran TochMethodologies in Information System Development

20November 2003

Strategy: ConsequencesStrategy: Consequences

• Benefits– Provides an alternative to subclassing the Context

class to get a variety of algorithms or behaviors– Keeps the client clean from large conditional

statements– Provides a choice of implementations for the same

behavior

• Liabilities– Increases the number of objects– All algorithms must use the same Strategy interface– We still need conditional statements!

Page 21: Design patterns

Design Patterns – Eran TochMethodologies in Information System Development

21November 2003

Strategy: Known UsesStrategy: Known Uses

• QSIA Learning System

• Layout Manager in Java Swing

• Sorting algorithms, memory allocation algorithms and more…

Page 22: Design patterns

Design Patterns – Eran TochMethodologies in Information System Development

22November 2003

AgendaAgenda

• What is a Design Pattern?

• Structural Patterns

• Behavioral Patterns

• Creational Patterns

• Human Interaction Patterns

• Summary

Page 23: Design patterns

Design Patterns – Eran TochMethodologies in Information System Development

23November 2003

The Singleton PatternThe Singleton Pattern

• Intent.– Ensure a class only has one instance, and provide a

global point of access to it.

• Motivation.– Sometimes, an application needs one, and only one,

instance of an object. For example, we want just one window manager. Or just one factory for a family of products.

– We need to have that one instance easily accessible.– And we want to ensure that additional instances of the

class can not be created.

Page 24: Design patterns

Design Patterns – Eran TochMethodologies in Information System Development

24November 2003

Singleton: StructureSingleton: Structure

• Declare the single instance as a private static data member.

• Provide a public static member function that encapsulates all initialization code, and provides access to the instance.

+ static instance()- Singleton()+ getSingletonData()+ setSingletonData()

- static uniqueInstance : Singleton- data : int

Singleton

return uniqueInstance

Page 25: Design patterns

Design Patterns – Eran TochMethodologies in Information System Development

25November 2003

Singleton: Example CodeSingleton: Example Code

public class Singleton {// The private reference to the one and only instance.private static Singleton uniqueInstance = null;// An instance attribute.private int data = 0;

/** * Returns a reference to the single instance. * Creates the instance if it does not yet exist. */public static Singleton instance() {

if(uniqueInstance == null) uniqueInstance = new Singleton();

return uniqueInstance;}/** * The Singleton Constructor. Note that it is private! * No client can instantiate a Singleton object! */private Singleton() {}

}

Page 26: Design patterns

Design Patterns – Eran TochMethodologies in Information System Development

26November 2003

Singleton: Example Code – cont’dSingleton: Example Code – cont’d

public class TestSingleton {

public static void main(String args[]) {// Get a reference to the single instance of Singleton.

Singleton s = Singleton.instance();// Set the data value.s.setData(34);System.out.println("First reference: " + s);System.out.println("Singleton data value is: " +

s.getData());// Get another reference to the Singleton.// Is it the same object?s = null;s = Singleton.instance();System.out.println("\nSecond reference: " + s);System.out.println("Singleton data value is: " +

s.getData());}

}

What will be the output?

Page 27: Design patterns

Design Patterns – Eran TochMethodologies in Information System Development

27November 2003

AgendaAgenda

• What is a Design Pattern?

• Structural Patterns

• Behavioral Patterns

• Creational Patterns

• Human Interaction Patterns

• Summary

Page 28: Design patterns

Design Patterns – Eran TochMethodologies in Information System Development

28November 2003

Human Interaction PatternsHuman Interaction Patterns

• A Pattern language to describe human interaction situations

• Examples:– Go Back to a Safe Place– Form– Control Panel – WYSIWYG Editor– Hierarchical Set – Map of Navigable Spaces

– More…

Page 29: Design patterns

Design Patterns – Eran TochMethodologies in Information System Development

29November 2003

Go Back to a Safe PlaceGo Back to a Safe Place

• Problem:– How can the artifact make navigation easy, convenient, and

psychologically safe for the user?

• Forces:– A user may forget where they were, if they stop using the artifact

while they're in the middle of something and don't get back to it for a while.

– If the user gets into a space or a state that they don't want to be in, they will want to get out of it in a safe and predictable way.

– The user is more likely to explore an artifact if they are assured that they can easily get out of an undesired state or space; that assurance engenders a feeling of security.

Page 30: Design patterns

Design Patterns – Eran TochMethodologies in Information System Development

30November 2003

Go Back to a Safe PlaceGo Back to a Safe Place

• Solution:– Provide a way to go back to a checkpoint of the user's

choice.

• Examples: – The "Home" button on a Web browser – Turning back to the beginning of a chapter in a

physical book or magazine – The “undo" feature on some computer applications

Page 31: Design patterns

Design Patterns – Eran TochMethodologies in Information System Development

31November 2003

AgendaAgenda

• What is a Design Pattern?

• Structural Patterns

• Behavioral Patterns

• Creational Patterns

• Human Interaction Patterns

• Summary

Page 32: Design patterns

Design Patterns – Eran TochMethodologies in Information System Development

32November 2003

SummarySummary

• Patterns learned:– Composite, Strategy, Singleton, Go Back to a Safe

Place.

• Advantages of Design Patterns:– They capture expertise and make it accessible to non-

experts. – Their names collectively form a vocabulary that helps

developers communicate better. – They help people understand a system more quickly

when it is documented with the patterns it uses.

Page 33: Design patterns

Design Patterns – Eran TochMethodologies in Information System Development

33November 2003

ReferencesReferences

• Design Patterns: Elements of Reusable Object-Oriented Software, Gamma E. et el., 1995, Addison-Wesley.

• A course from Bob Tarr from UMBC Universityhttp://www.research.umbc.edu/~tarr/dp/fall00/cs491.html

• The Design Patterns Java Companion, James W. Cooper (an online version of the book)http://www.patterndepot.com/put/8/JavaPatterns.htm

• A Site dedicated to Design Patterns by Vince Hustonhttp://home.earthlink.net/~huston2/dp/patterns.html

• Seven Habits of Successful Pattern Writers, John Vlissideshttp://hillside.net/patterns/papers/7habits.html

• COMMON GROUND: A Pattern Language for Human-Computer Interface Design, Jenifer Tidwell, http://www.mit.edu/~jtidwell/common_ground_onefile.html

Page 34: Design patterns

Design Patterns – Eran TochMethodologies in Information System Development

34November 2003

Doesn’t it go too far?Doesn’t it go too far?

• Taken from Vince Huston’s site about Design Patterns:

Page 35: Design patterns

Design Patterns – Eran TochMethodologies in Information System Development

Thanks!Thanks!