overview of design patterns vijayan sugumaran department of dis oakland university

28
Overview of Design Patterns Vijayan Sugumaran Department of DIS Oakland University

Upload: frank-sharp

Post on 15-Jan-2016

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Overview of Design Patterns Vijayan Sugumaran Department of DIS Oakland University

Overview of Design Patterns

Vijayan Sugumaran

Department of DIS

Oakland University

Page 2: Overview of Design Patterns Vijayan Sugumaran Department of DIS Oakland University

Purpose

A design pattern captures design expertise –patterns are not created from thin air, but abstracted from existing design examples

Using design patterns is reuse of design expertise

Studying design patterns is a way of studying how the “experts” do design

Design patterns provide a vocabulary for talking about design

Page 3: Overview of Design Patterns Vijayan Sugumaran Department of DIS Oakland University

Background Erich Gamma, Richard Helm, Ralph Johnson,

and John Vlissides (the “Gang of Four”) – “Design Patterns: Elements of Reusable Object-Oriented Software” published in 1995, by Addison-Wesley

This book solidified thinking about patterns and became the seminal Design Patterns text

Software design patterns are based (somewhat) on work by the architect Christopher Alexander – work on pattern language

Page 4: Overview of Design Patterns Vijayan Sugumaran Department of DIS Oakland University

Design Patterns

Design pattern - a common solution to a give problem in a given context, which supports reuse of proven approaches and techniques.

Advantages Allow us to design with the experiences of those

who came before rather than having to "reinvent the wheel."

Provide designers a short-hand notation for discussing design issues.

Page 5: Overview of Design Patterns Vijayan Sugumaran Department of DIS Oakland University

Why design patterns in SA? If you’re a software engineer, you should

know about them anyway There are many architectural patterns

published, and the GoF Design Patterns is a prerequisite to understanding these: Mowbray and Malveau – CORBA Design Patterns Schmidt et al – Pattern-Oriented Software

Architecture

Design Patterns help you break out of first-generation OO thought patterns

Page 6: Overview of Design Patterns Vijayan Sugumaran Department of DIS Oakland University

The seven layers of architecture*

Global architectureEnterprise architecture

System architecture

Application architecture

Macro-architecture

Micro-architecture

Objects

* Mowbray and Malveau

ORB

OO architecture

Frameworks

Subsystem

Design patterns

OO programming

Page 7: Overview of Design Patterns Vijayan Sugumaran Department of DIS Oakland University

How patterns arise

Benefits

Related Patterns

Consequences

Forces

Problem

Context

Solution

Page 8: Overview of Design Patterns Vijayan Sugumaran Department of DIS Oakland University

Structure of a pattern Name Intent Motivation Applicability Structure Consequences Implementation Known Uses Related Patterns

Page 9: Overview of Design Patterns Vijayan Sugumaran Department of DIS Oakland University

Key patterns

The following patterns are considered to be a good “basic” set of design patterns

Competence in recognizing and applying these patterns will improve your low-level design skills

Page 10: Overview of Design Patterns Vijayan Sugumaran Department of DIS Oakland University

Gang-of-Four PatternsCreationalAbstract factor

Builder

Factory method

Prototype

Singleton

StructuralAdapter

Bridge

Composite

Decorator

Façade

Proxy

BehavioralChain of responsibility

Command

Flyweight

Interpreter

Iterator

Mediator

Memento

Observer

State

Strategy

Template method

Visitor

Page 11: Overview of Design Patterns Vijayan Sugumaran Department of DIS Oakland University

Adapter PatternPattern: AdapterCategory: StructuralProblem: How to provide a stable interface to similar classes with

different interfaces?Solution: Add a class that acts as an adapter to convert the interface

of a class into another interface that the client classes expect.

Page 12: Overview of Design Patterns Vijayan Sugumaran Department of DIS Oakland University

Composite Pattern

ComponentOperation()Add(Component)Remove(Component)

CompositeOperation()Add(Component)Remove(Component)

LeafOperation()

Client

children

0..*

For all c in children c.Operation();

Construct part-whole hierarchy Simplify client interface to leaves/composites Easier to add new kinds of components

Page 13: Overview of Design Patterns Vijayan Sugumaran Department of DIS Oakland University

Composite Pattern Example

Figurepaint()translate()getBounds()

CompositeFigurepaint()addFigure(Figure))removeFigure(Figure))

BasicFigurepaint()

Viewchildren

0..*

For all c in children c.paint();

Example: figures in a structured graphics toolkit

LabelFigurepaint()

0..*

Controller

parent

Page 14: Overview of Design Patterns Vijayan Sugumaran Department of DIS Oakland University

Strategy Pattern

StrategyOperation()

ConcreteStrategy2Operation()

Context

Make algorithms interchangeable---”changing the guts” Alternative to subclassing Choice of implementation at run-time Increases run-time complexity

ContextInterface()

ConcreteStrategy1Operation()

Page 15: Overview of Design Patterns Vijayan Sugumaran Department of DIS Oakland University

Strategy Pattern Example

ConnectorRouterShape recalculate(Pt, Pt)

ArcRouterShape recalculate(Pt, Pt)

Connectorroute()

StraightRouterShape recalculate(Pt, Pt)

ManhattanRouterShape recalculate(Pt, Pt)

shape=router.recalculate(start,end);redraw(shape);

Example: drawing different connector styles

Page 16: Overview of Design Patterns Vijayan Sugumaran Department of DIS Oakland University

Chain of Responsibility Pattern

HandlerhandleRequest()

ConcreteHandler2handleRequest()

ClientContextInterface()

ConcreteHandler1handleRequest()

Decouple sender of a request from receiver Give more than one object a chance to handle Flexibility in assigning responsibility Often applied with Composite

successor

Page 17: Overview of Design Patterns Vijayan Sugumaran Department of DIS Oakland University

Chain of Responsibility Pattern Example

FigurehandleEvent(Event)

CompositeFigure

Interactorchildren

0..*

If interactor != null interactor.handle(event,this)else parent.handleEvent(event)

0..1

parent

Example: handling events in a graphical hierarchy

handle(Event,Figure)

0..*

Page 18: Overview of Design Patterns Vijayan Sugumaran Department of DIS Oakland University

Abstract Factory Pattern

1. Consider a user interface toolkit to support multiple look-and-feel standards.

2. For portability an application must not hard code its widgets for one look and feel.

How to design the application so that incorporating new look and feel requirements will be easy?

Problem Context:

Page 19: Overview of Design Patterns Vijayan Sugumaran Department of DIS Oakland University

Abstract Factory Pattern Example

Define an abstract WidgetFactory class This class declares an interface to create

different kinds of widgets There is one abstract class for each kind of

widget and concrete subclasses implement widgets for different standards

WidgetFactory offers an operation to return a new widget object for each abstract widget class. Clients call these operations to obtain instances of widgets without being aware of the concrete classes they use

Page 20: Overview of Design Patterns Vijayan Sugumaran Department of DIS Oakland University

Abstract Factory Pattern Example

WidgetFactory

CreateScrollbar()

CreateWindow()

Window

ScrollBarWWidgetFactory

MacWidgetFactory

Client

WWindowMacWindow

MacScrollBar WScrollBar

One for each standard.

Page 21: Overview of Design Patterns Vijayan Sugumaran Department of DIS Oakland University

Abstract Factory Pattern Example

AbstractFactory

CreateScrollbar()

CreateWindow()

ConcreteFactory1

Client

ProductA1ProductA2

AbstractProductA

ProductB2 ProductB1

AbstractProductB

ConcreteFactory2

CreateProductA()

CreateProductB()

Page 22: Overview of Design Patterns Vijayan Sugumaran Department of DIS Oakland University

Abstract Factory Pattern: Participants and Communication

ConcreteFactory: Implements the operations to create concrete product objects.

AbstractProduct: Declares an interface for a type of product object.

ConcreteProduct: Defines a product object to be created by the corresponding factory.

AbstractFactory: Declares the interface for operations to create abstract product objects

Client: Uses only the interface declared by the abstractFactory and AbstractProduct classes.

Page 23: Overview of Design Patterns Vijayan Sugumaran Department of DIS Oakland University

Abstract Factory Pattern Codeclass MazeFactory {

public:

MazeFactory();

virtual Maze* MakeMaze() const

{ return new Maze;}

// Creates components of mazes. // Builds rooms, walls, and doors.

virtual Wall* MakeWall() const

{ return new Wall;}

virtual Wall* MakeRoom(int n) const

{ return new Room;}

}// more methods.

// This factory is a collection of // factory methods. Also, this class// acts both as Abstract and Concrete // Factory

Page 24: Overview of Design Patterns Vijayan Sugumaran Department of DIS Oakland University

Abstract Factory Pattern CodeMaze* MazeGame:: CreateMaze (MazeFactory& factory)

Maze* aMaze = factory.MakeMaze();

// Builds a maze.

}

Room* myroom = factory.MakeRoom(1);

Door* aDoor = factory.MakeDoor(myRoom,herRoom)

Room* herroom = factory.MakeRoom(2);

aMaze AddRoom(myRoom)

aMaze AddRoom(herRoom)

// More code to add walls.

// One can also create a// BombedMazeFactory with // different types of Rooms // and Walls.

Page 25: Overview of Design Patterns Vijayan Sugumaran Department of DIS Oakland University

Patterns vs “Design”

Patterns are design But: patterns transcend the “identify

classes and associations” approach to design

Instead: learn to recognize patterns in the problem space and translate to the solution

Patterns can capture OO design principles within a specific domain

Patterns provide structure to “design”

Page 26: Overview of Design Patterns Vijayan Sugumaran Department of DIS Oakland University

Patterns vs Frameworks Patterns are lower-level than frameworks

Frameworks typically employ many patterns: Factory

Strategy

Composite

Observer

Done well, patterns are the “plumbing” of a framework

Page 27: Overview of Design Patterns Vijayan Sugumaran Department of DIS Oakland University

Patterns vs Architecture Design Patterns (GoF) represent a lower level of system

structure than “architecture” (cf: seven levels of A) Patterns can be applied to architecture:

Mowbray and Malveau Buschmann et al Schmidt et al

Architectural patterns tend to be focussed on middleware. They are good at capturing: Concurrency Distribution Synchronization

Page 28: Overview of Design Patterns Vijayan Sugumaran Department of DIS Oakland University

Summary Design Patterns (GoF) provide a foundation

for further understanding of: Object-Oriented design

Software Architecture

Understanding patterns can take some time Re-reading them over time helps

As does applying them in your own designs!