design patterns - kennesaw state...

34
CS 8532: Advanced Software Engineering Dr. Hisham Haddad Design Patterns Introduction to Design Patterns and Common Patterns in OO Systems Lecture Notes Source: Design Patterns: Elements of Reusable Object-Oriented Software. By Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, Addison-Wesley, 2000.

Upload: others

Post on 06-Aug-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Design Patterns - Kennesaw State Universityksuweb.kennesaw.edu/~hhaddad/Spring2007/CS8532/Design_Patterns_Slides.pdfDesign patterns make proven solutions more accessible to designers,

CS 8532: Advanced Software Engineering

Dr. Hisham Haddad

Design Patterns

Introduction to Design Patterns and Common Patterns in OO Systems

Lecture Notes Source:Design Patterns: Elements of Reusable Object-Oriented Software. By Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, Addison-Wesley, 2000.

Page 2: Design Patterns - Kennesaw State Universityksuweb.kennesaw.edu/~hhaddad/Spring2007/CS8532/Design_Patterns_Slides.pdfDesign patterns make proven solutions more accessible to designers,

CS 8532: Advanced Software Engineering

Dr. Hisham Haddad

Design Patterns and OO Systems

“It takes a long time for novices to learn what good object-oriented design is all about. Experienced designers evidently know something inexperienced ones don’t. What is it? … One thing expert designers know not to do is solve every problem from first principles. Rather, they reuse solutions that have worked for them in the past… Consequently, you’ll find recurring patterns of classes and communicating objects in many object-oriented system… They help designers reuse successful designs by basing new designs on prior experience… apply them immediately to design problems without having to rediscover them.”

Page 3: Design Patterns - Kennesaw State Universityksuweb.kennesaw.edu/~hhaddad/Spring2007/CS8532/Design_Patterns_Slides.pdfDesign patterns make proven solutions more accessible to designers,

CS 8532: Advanced Software Engineering

Dr. Hisham Haddad

What is a Design Pattern?

A design pattern is a recurring design experiences captured and presented in a form that other designers can reuse in new systems.

A design pattern names, explains, and evaluates an important design (a solution to a problem) that has been applied more than once in OO systems.

Design patterns make proven solutions more accessible to designers, help designers choose designs that make the system more reusable, and help designers develop the “right” design faster.

Page 4: Design Patterns - Kennesaw State Universityksuweb.kennesaw.edu/~hhaddad/Spring2007/CS8532/Design_Patterns_Slides.pdfDesign patterns make proven solutions more accessible to designers,

CS 8532: Advanced Software Engineering

Dr. Hisham Haddad

Elements of a Design Pattern - 1

1. Pattern name: A meaningful and descriptive name that capturesthe problem, its solution, and consequence.

2. Problem: description of the problem, its context, and when toapply the pattern.

3. Solution: description of elements that make up the design, their relationships, responsibilities, and collaborations. The solution is a template - it doesn’t describe specific design or implementation.

4. Consequences: description of results and trade-offs of applying the pattern. This helps designers evaluate design alternatives and their cost and benefits (e.g., impact on system flexibility,extensibility, reusability, portability, etc…)

Page 5: Design Patterns - Kennesaw State Universityksuweb.kennesaw.edu/~hhaddad/Spring2007/CS8532/Design_Patterns_Slides.pdfDesign patterns make proven solutions more accessible to designers,

CS 8532: Advanced Software Engineering

Dr. Hisham Haddad

Elements of a Design Pattern - 2Notes:

- A design pattern is a description of communicating objects and classes that solve a general design problem in a particulate context.

- It identifies classes and instances, their roles and collaborations, and responsibilities.

- It is not a solution for an entire system or subsystem.

- Design patterns are based on implemented solutions, and thus they are “tied” to OOP languages. The source book uses Smalltalk and C++ language features.

Page 6: Design Patterns - Kennesaw State Universityksuweb.kennesaw.edu/~hhaddad/Spring2007/CS8532/Design_Patterns_Slides.pdfDesign patterns make proven solutions more accessible to designers,

CS 8532: Advanced Software Engineering

Dr. Hisham Haddad

Elements of a Design Pattern - 3

Examples:

Abstract Factory: a design pattern used to create families of objects without specifying their concrete classes.(an application is a user interface toolkit that support multiple look-and-feel standards for different systems) (page 87).

Builder: a design pattern used to separate the construction of a complex object from its representation so that the same construction process can create different representations.(an application is RTF reader that converts RTF to other format. This pattern allows adding other formats without modifying the reader) (page 97).

Page 7: Design Patterns - Kennesaw State Universityksuweb.kennesaw.edu/~hhaddad/Spring2007/CS8532/Design_Patterns_Slides.pdfDesign patterns make proven solutions more accessible to designers,

CS 8532: Advanced Software Engineering

Dr. Hisham Haddad

Describing a Design Pattern - 1

Template Elements:

- Intent: Short statement of what it does, its intent, and the design issue it addresses.

- Also Known As: other well-known names for the pattern.

- Motivation: A scenario that illustrates a design problem and howthe design pattern solves it.

- Applicability: Description of when to apply the pattern and examples of (poor) designs it addresses.

- Structure: Ggraphical representation of class organizations and their interactions.

Page 8: Design Patterns - Kennesaw State Universityksuweb.kennesaw.edu/~hhaddad/Spring2007/CS8532/Design_Patterns_Slides.pdfDesign patterns make proven solutions more accessible to designers,

CS 8532: Advanced Software Engineering

Dr. Hisham Haddad

Describing a Design Pattern - 2

- Participants: Listing of classes/objects participating in the design pattern and their responsibilities.

- Collaborations: How the participants collaborate to carry out their responsibilities.

- Consequences: How does the pattern support its intended objectives and the trade-offs that result from using the pattern.

- Implementation: What hints, techniques, and language-features the user should be aware of to implement the pattern.

- Sample Code: Code segments to show how the pattern may be implemented (source book uses C++).

Page 9: Design Patterns - Kennesaw State Universityksuweb.kennesaw.edu/~hhaddad/Spring2007/CS8532/Design_Patterns_Slides.pdfDesign patterns make proven solutions more accessible to designers,

CS 8532: Advanced Software Engineering

Dr. Hisham Haddad

Describing a Design Pattern - 3

- Know uses: Examples of real systems (from different application domains) in which the pattern is used.

- Related patterns: What patterns are closely related? what are the important differences? and with which other patterns this pattern should be used?

Page 10: Design Patterns - Kennesaw State Universityksuweb.kennesaw.edu/~hhaddad/Spring2007/CS8532/Design_Patterns_Slides.pdfDesign patterns make proven solutions more accessible to designers,

CS 8532: Advanced Software Engineering

Dr. Hisham Haddad

Identified Design Patterns

The source book identifies and fully describes 23 design patterns for OO systems. Sample ones are provided below.

Adapter (wrapper) (139): Allows classes that have incompatible interfaces to work together.

Singleton (127): Ensures that a class has only one instance and it provides global point of access to it.

Factory Method (virtual constructor) (107): Defines an interface for creating an object and let subclasses decide which class to instantiate. This pattern lets classes defer instantiation to subclasses.

Iterator (cursor) (257): Provides a way to sequentially access elements of an aggregate object (list of objects) without exposing its underlying representation.

Page 11: Design Patterns - Kennesaw State Universityksuweb.kennesaw.edu/~hhaddad/Spring2007/CS8532/Design_Patterns_Slides.pdfDesign patterns make proven solutions more accessible to designers,

CS 8532: Advanced Software Engineering

Dr. Hisham Haddad

Organizing Design Patterns - 1

Since patterns can be at different levels of abstraction, they can be organized in different ways, such as families of related patterns (source book approach), patterns used together,and by potential relationships among patterns.

Design pattern space (classification):

Purpose: what the pattern does.- creational: patterns concerned with object creation process.- structural: patterns concerned with composition of classes or

objects.- behavioral: patterns concerned with the way classes and

objects interact and share responsibilities.

Page 12: Design Patterns - Kennesaw State Universityksuweb.kennesaw.edu/~hhaddad/Spring2007/CS8532/Design_Patterns_Slides.pdfDesign patterns make proven solutions more accessible to designers,

CS 8532: Advanced Software Engineering

Dr. Hisham Haddad

Organizing Design Patterns - 2

Scope: whether the pattern is applicable to classes or objects.- class: patterns deal with inheritance among classes (static).- object: patterns deal with object relationships at run-time

(dynamic)

See next slide for pattern space illustration.

Page 13: Design Patterns - Kennesaw State Universityksuweb.kennesaw.edu/~hhaddad/Spring2007/CS8532/Design_Patterns_Slides.pdfDesign patterns make proven solutions more accessible to designers,

CS 8532: Advanced Software Engineering

Dr. Hisham Haddad

Organizing Design Patterns - 3

PurposeCreational Structural Behavioral

Scope class Factory Method Adapter (class) InterpreterTemplate Method

object Abstract FactoryBuilderPrototypeSingleton

Adapter (object)BridgeCompositeDecoratorFaçadeFlyweightProxy

Chain of ResponsibilitiesCommandIteratorMediatorMementoObserverStateStrategyVisitor

All 23 patterns and their intent statements are posted on the course website.

Page 14: Design Patterns - Kennesaw State Universityksuweb.kennesaw.edu/~hhaddad/Spring2007/CS8532/Design_Patterns_Slides.pdfDesign patterns make proven solutions more accessible to designers,

CS 8532: Advanced Software Engineering

Dr. Hisham Haddad

Most Common Patterns

Simple common patterns that can be found in most OO systems:- Abstract Factory- Adapter- Composite- Decorator- Factory Method- Observer- Strategy- Template Method

Page 15: Design Patterns - Kennesaw State Universityksuweb.kennesaw.edu/~hhaddad/Spring2007/CS8532/Design_Patterns_Slides.pdfDesign patterns make proven solutions more accessible to designers,

CS 8532: Advanced Software Engineering

Dr. Hisham Haddad

Benefits of Design Patterns - 1

Design patterns can solve design problems, such as:

1- Identifying “less-obvious” Objects: Those are objects that have no counterpart in real world system and are not identified during analysis phase. (e.g., processes and algorithms)

Strategy pattern describes how to implement interchangeable families of algorithms.

State pattern represents each stat of an entity as an object.

Page 16: Design Patterns - Kennesaw State Universityksuweb.kennesaw.edu/~hhaddad/Spring2007/CS8532/Design_Patterns_Slides.pdfDesign patterns make proven solutions more accessible to designers,

CS 8532: Advanced Software Engineering

Dr. Hisham Haddad

Benefits of Design Patterns - 2

2- Object Granularity: A number of patterns help determine what can be an Object.

Façade pattern describes how to represent complete subsystem as objects.

Flyweight pattern describes how to support large number of objects at the finest granularity.

Abstract Factory and Builder patterns describe how to decompose an object into smaller objects (object responsibility is to create other objects).

Visitor and Command patterns define objects to only implement a request to another object (or group of objects).

Page 17: Design Patterns - Kennesaw State Universityksuweb.kennesaw.edu/~hhaddad/Spring2007/CS8532/Design_Patterns_Slides.pdfDesign patterns make proven solutions more accessible to designers,

CS 8532: Advanced Software Engineering

Dr. Hisham Haddad

Benefits of Design Patterns - 3

3- Object Interfaces: Some patterns helps define object interfaces and their parameters and data types.

Memento pattern describes how to encapsulate and save the internal state of an object so that the object can be restored to that state. (i.e, client interface to access the object state and object interface for restoration)

Decorator and Proxy patterns requires the interfaces of a Decorator and Proxy objects be identical to those of the decorated and proxied objects.

Visitor pattern requires that a visitor interface must reflect all classes of objects that visitors can visit.

Page 18: Design Patterns - Kennesaw State Universityksuweb.kennesaw.edu/~hhaddad/Spring2007/CS8532/Design_Patterns_Slides.pdfDesign patterns make proven solutions more accessible to designers,

CS 8532: Advanced Software Engineering

Dr. Hisham Haddad

Benefits of Design Patterns - 4

4- Object Implementation: Some patterns helps describe object implementation (abstract class interface and implementation inheritance).

Chain of Responsibilities pattern requires that objects have common interface, but they don’t share common implementation.

Composite pattern requires that components define common interface.

Command, Observer, State, and Strategy patterns often use abstract classes in their implementations.

Page 19: Design Patterns - Kennesaw State Universityksuweb.kennesaw.edu/~hhaddad/Spring2007/CS8532/Design_Patterns_Slides.pdfDesign patterns make proven solutions more accessible to designers,

CS 8532: Advanced Software Engineering

Dr. Hisham Haddad

Benefits of Design Patterns - 5

Other benefits for design patterns:

- Facilitate and implement reuse techniques (inheritance, parameterized types (generics), and delegation)

- Relating run-time and compile-time structures.

- Support for changes to existing requirements (system evolution)

Page 20: Design Patterns - Kennesaw State Universityksuweb.kennesaw.edu/~hhaddad/Spring2007/CS8532/Design_Patterns_Slides.pdfDesign patterns make proven solutions more accessible to designers,

CS 8532: Advanced Software Engineering

Dr. Hisham Haddad

How to Select a Design Pattern

Few tips for new users:

- Consider how design patterns solve design problems (see previous slides on benefits).

- Scan intent sections to find patterns related to the problem.- Study how pattern interrelate. Some problems may require

using multiple patterns.- Study patterns of similar purpose (creational, structural, and

behavioral). Comparison of patters are available in the source book.

- Study the cause of re-design and then find a pattern that helps avoid that cause.

Page 21: Design Patterns - Kennesaw State Universityksuweb.kennesaw.edu/~hhaddad/Spring2007/CS8532/Design_Patterns_Slides.pdfDesign patterns make proven solutions more accessible to designers,

CS 8532: Advanced Software Engineering

Dr. Hisham Haddad

How to use a Design Pattern

Once a pattern is selected, tips on how to use it:

- Read the pattern and focus on the Applicability and Consequences sections.

- Study its elements (classes and objects) and understand how they relate.

- Examine some sample code and see how you can implement the pattern.

- Choose meaningful names (in the context of the application) for pattern classes.

- Define the classes (interfaces, inheritance, attributes).- Define meaningful names for operations.- Implement the operations.

Page 22: Design Patterns - Kennesaw State Universityksuweb.kennesaw.edu/~hhaddad/Spring2007/CS8532/Design_Patterns_Slides.pdfDesign patterns make proven solutions more accessible to designers,

CS 8532: Advanced Software Engineering

Dr. Hisham Haddad

The Catalog - 1

Intent statements of design patterns and aspects that can change.

Abstract Factory (Kit) (87): Provide an interface for creating families of related or dependent objects without specifying their concrete classes. Families of product objects.

Example:Interface toolkit that supports multiple look-and-feel standards, such as Motif and Presentation Manager.

Uses abstract WidgetFactory class to define interface basic widgets. Subclasses implement widgets for different look-and-fee standards.

Page 23: Design Patterns - Kennesaw State Universityksuweb.kennesaw.edu/~hhaddad/Spring2007/CS8532/Design_Patterns_Slides.pdfDesign patterns make proven solutions more accessible to designers,

CS 8532: Advanced Software Engineering

Dr. Hisham Haddad

The Catalog - 2

Factory Method (Virtual Constructor) (107): Define an interface for creating an object, but let the subclasses decide which classes to instantiate. Factory Method lets a class defer instantiation to subclasses. Subclass of object that is instantiated.

Example:Framework for application that presents the user with multiple documents.

Uses two abstract classes: Document and Application. A subclass of the Application class defines the factory method call CreatDocument() that encapsulates the knowledge about document subclass and moves this knowledge out of the framework (i.e., creates application-specific subclasses).

Page 24: Design Patterns - Kennesaw State Universityksuweb.kennesaw.edu/~hhaddad/Spring2007/CS8532/Design_Patterns_Slides.pdfDesign patterns make proven solutions more accessible to designers,

CS 8532: Advanced Software Engineering

Dr. Hisham Haddad

The Catalog - 3

Adapter (Wrapper) (139): Convent the interface of a class into another interface clients expect. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces. Interface to an object.

Example:Drawing editor (class Shapes) that allows the user to edit and arrange graphical elements including text objects. Subclasses ofclass Shape define the various graphical shapes. Class TextView manipulate text, but how can it work (reused) with the drawing editor? Class TextShape is defined as an adapter to interface class TextView with class Shapes.

Page 25: Design Patterns - Kennesaw State Universityksuweb.kennesaw.edu/~hhaddad/Spring2007/CS8532/Design_Patterns_Slides.pdfDesign patterns make proven solutions more accessible to designers,

CS 8532: Advanced Software Engineering

Dr. Hisham Haddad

The Catalog - 4

Builder (97): Separate the construction of a complex object from its representation so that the same construction process can create different representations. How a composite object gets created.

Example:RTF reader that converts RTF to other text formats such as plaintext and TeXText. Other conversions should be added without modifying the reader.

RFTReader call class TextConverter that uses subclasses specialized in different conversions and formats.

Page 26: Design Patterns - Kennesaw State Universityksuweb.kennesaw.edu/~hhaddad/Spring2007/CS8532/Design_Patterns_Slides.pdfDesign patterns make proven solutions more accessible to designers,

CS 8532: Advanced Software Engineering

Dr. Hisham Haddad

The Catalog - 5

Composite (163): Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and composition of objects uniformly. Structure and composition of an object.

Example: Grouping basic shapes and use that without having to distinguish between the individual element and their grouping.

Page 27: Design Patterns - Kennesaw State Universityksuweb.kennesaw.edu/~hhaddad/Spring2007/CS8532/Design_Patterns_Slides.pdfDesign patterns make proven solutions more accessible to designers,

CS 8532: Advanced Software Engineering

Dr. Hisham Haddad

The Catalog - 6

Decorator (Wrapper) (175): Attach additional responsibilities to an object dynamically (not the entire class). Decorators provide a flexible alternative to subclassing for extending functionality.Responsibilities of an object without subclassing.

Example: Adding border and scrolling to user interface object.

Page 28: Design Patterns - Kennesaw State Universityksuweb.kennesaw.edu/~hhaddad/Spring2007/CS8532/Design_Patterns_Slides.pdfDesign patterns make proven solutions more accessible to designers,

CS 8532: Advanced Software Engineering

Dr. Hisham Haddad

The Catalog - 7

Observer (Dependents, Publish-Subscribe) (293): Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. Number of objects that depend on one another object; how the dependent objects stay up to date.

Example: Graphical representation of data on a spreadsheet (Pie and Bar charts). When the user changes the data, the charts getsupdated automatically.

Page 29: Design Patterns - Kennesaw State Universityksuweb.kennesaw.edu/~hhaddad/Spring2007/CS8532/Design_Patterns_Slides.pdfDesign patterns make proven solutions more accessible to designers,

CS 8532: Advanced Software Engineering

Dr. Hisham Haddad

The Catalog - 8

Strategy (Policy) (315): Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that us it. An algorithm.

Example: Breaking a stream of text into lines using different line breaking algorithms such as simple line breaks, TeX line breaks,and array (equal length) line breaks. Each line breaking algorithm is implement by individual classes.

Page 30: Design Patterns - Kennesaw State Universityksuweb.kennesaw.edu/~hhaddad/Spring2007/CS8532/Design_Patterns_Slides.pdfDesign patterns make proven solutions more accessible to designers,

CS 8532: Advanced Software Engineering

Dr. Hisham Haddad

The Catalog - 9

Prototype (117): Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype. Class of object that is instantiated.

Bridge (151): Decouple an abstraction from its implementation so that the two can vary independently. Implementation of an object.

Chain of Responsibilities (223): Avoid coupling the sender and a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it. Object that fulfill a request.

Page 31: Design Patterns - Kennesaw State Universityksuweb.kennesaw.edu/~hhaddad/Spring2007/CS8532/Design_Patterns_Slides.pdfDesign patterns make proven solutions more accessible to designers,

CS 8532: Advanced Software Engineering

Dr. Hisham Haddad

The Catalog - 10

Command (233): Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations. When and how a request is fulfilled.

Facade (185): Provide a unified interface to a set of interfaces in a subsystem. Façade defines a higher-level interface that makes the subsystem easier to use. Interface to a subsystem.

Flyweight (195): Use sharing to support large numbers of fine-grained objects efficiently with minimal cost. Each object can be used in multiple context simultaneously. Storage costs of objects.

Page 32: Design Patterns - Kennesaw State Universityksuweb.kennesaw.edu/~hhaddad/Spring2007/CS8532/Design_Patterns_Slides.pdfDesign patterns make proven solutions more accessible to designers,

CS 8532: Advanced Software Engineering

Dr. Hisham Haddad

The Catalog - 11

Interpreter (243): Given a language, define representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language. Grammar and interpretation of a language.

Mediator (273): Define an object that encapsulates how a set of objects interacts. A Mediator promotes loose coupling by keepingobjects from referring to other explicitly, and it lets you vary their interaction independently. How and which objects interact with each other.

Page 33: Design Patterns - Kennesaw State Universityksuweb.kennesaw.edu/~hhaddad/Spring2007/CS8532/Design_Patterns_Slides.pdfDesign patterns make proven solutions more accessible to designers,

CS 8532: Advanced Software Engineering

Dr. Hisham Haddad

The Catalog - 12

Iterator (257): Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation. How an aggregate’s elements are accessed and traversed.

Memento (283): Without violating encapsulation, capture and externalize an object’s internal state so that the object can berestored to this state later. What private information is stored outside an object, and when.

Proxy (207): Provide a surrogate or placeholder for another object to control access to it. How an object is accessed; its location.

Page 34: Design Patterns - Kennesaw State Universityksuweb.kennesaw.edu/~hhaddad/Spring2007/CS8532/Design_Patterns_Slides.pdfDesign patterns make proven solutions more accessible to designers,

CS 8532: Advanced Software Engineering

Dr. Hisham Haddad

The Catalog - 13

Singleton (127): Ensure a class only has one instance, and provide a global point of access to it. The sole instance of a class.

State (305): Allow an object to alter its behavior when its internal state changes. The object will appear to change its class. States of an object.

Template Method (325): Define the skeleton on an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm’s structure. Steps of an algorithm.