6 introduction to design patterns - mississippi state...

37
Software Architecture – Intro to Design Patterns 1 Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission. Chapter 6 Introduction to Design Patterns 3 Types: Creational Structural Behavioral

Upload: others

Post on 23-Jul-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 6 Introduction to Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/6... · 2017-12-24 · Introduction to Design Patterns 3 Types: Creational

Software Architecture – Intro to Design Patterns 1

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Chapter 6 Introduction to Design Patterns

3 Types: Creational Structural Behavioral

Page 2: 6 Introduction to Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/6... · 2017-12-24 · Introduction to Design Patterns 3 Types: Creational

Software Architecture – Intro to Design Patterns 2

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Process Phase Discussed in This Chapter

Requirements Analysis

Design

Implementation

Architecture Framework Detailed Design

x Key: = secondary emphasis x = main emphasis

Page 3: 6 Introduction to Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/6... · 2017-12-24 · Introduction to Design Patterns 3 Types: Creational

Software Architecture – Intro to Design Patterns 3

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Sample Design Goals and Ways to Accomplish Them

q  Reusability, Flexibility, and Maintainability o  Reuse flexible designs o  Keep code at a general level o  Minimize dependency on other classes

q  Robustness o  Reuse reliable designs o  Reuse robust parts

q  Sufficiency / Correctness o  Modularize design o  Reuse trusted parts

Page 4: 6 Introduction to Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/6... · 2017-12-24 · Introduction to Design Patterns 3 Types: Creational

Software Architecture – Intro to Design Patterns 4

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

KitchenViewer Interface

Wall cabinet

Counter

Floor cabinet

Modern Classic Antique Arts & Crafts

menu

display area styles

Use case page 124 1. User clocks on the “wall cabinet” icon 2. Application displays a wall cabinet in

center of the work area 3. User resizes the wall cabinet 4. User drags the wall cabinet in the upper

half of the work area 5. User releases cursor 6. Application places the wall cabinet in a

position in the upper half of the work area 7. User clicks on the “floor cabinet” icon 8. Application displays a floor cabinet in the

center of the work area. ............

Page 5: 6 Introduction to Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/6... · 2017-12-24 · Introduction to Design Patterns 3 Types: Creational

Software Architecture – Intro to Design Patterns 5

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

KitchenViewer Example

Modern Classic Antique Arts & Crafts

Countertop

Page 6: 6 Introduction to Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/6... · 2017-12-24 · Introduction to Design Patterns 3 Types: Creational

Software Architecture – Intro to Design Patterns 6

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Selecting Antique Style

Modern Classic Antique Arts & Crafts

Page 7: 6 Introduction to Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/6... · 2017-12-24 · Introduction to Design Patterns 3 Types: Creational

Software Architecture – Intro to Design Patterns 7

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

KitchenViewer Without Design Patterns

Kitchen

Client renderKitchen()

FloorCabinet

ModernWallCabinet

ModernFloorCabinet AntiqueFloorCabinet

AntiqueWallCabinet

WallCabinet

Page 8: 6 Introduction to Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/6... · 2017-12-24 · Introduction to Design Patterns 3 Types: Creational

Software Architecture – Intro to Design Patterns 8

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

What are Design Patterns?

•  Design patterns are class combinations and accompanying algorithms that fulfill common design purposes.

•  A design pattern expresses an idea rather than a fixed class combination.

Page 9: 6 Introduction to Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/6... · 2017-12-24 · Introduction to Design Patterns 3 Types: Creational

Software Architecture – Intro to Design Patterns 9

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Design Goal At Work: à Flexibility ß

Our design should be flexible enough to produce any of several kitchen styles.

Page 10: 6 Introduction to Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/6... · 2017-12-24 · Introduction to Design Patterns 3 Types: Creational

Software Architecture – Intro to Design Patterns 10

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

AntiqueKStyle getWallCabinet() getFloorCabinet()

The Abstract Factory Idea

KitchenStyle getWallCabinet() getFloorCabinet()

ModernKStyle getWallCabinet() getFloorCabinet()

WallCabinet FloorCabinet

AntiqueWallCabinet AntiqueFloorCabinet

FloorCabinet getFloorCabinet() { return new AntiqueFloorCabinet(); }

… …

FloorCabinet getFloorCabinet() { return new ModernFloorCabinet(); }

Page 11: 6 Introduction to Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/6... · 2017-12-24 · Introduction to Design Patterns 3 Types: Creational

Software Architecture – Intro to Design Patterns 11

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Abstract Factory Design Pattern Applied to KitchenViewer

KitchenStyle getWallCabinet() getFloorCabinet()

Kitchen getWallCabinet() getFloorcabinet()

Client renderKitchen( KitchenStyle )

ModernKStyle getWallCabinet() getFloorCabinet()

AntiqueKStyle getWallCabinet() getFloorCabinet()

WallCabinet FloorCabinet

ModernWallCabinet

ModernFloorCabinet

AntiqueWallCabinet

AntiqueFloorCabinet

“reflecting polymorphism”

Page 12: 6 Introduction to Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/6... · 2017-12-24 · Introduction to Design Patterns 3 Types: Creational

Software Architecture – Intro to Design Patterns 12

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Abstract Factory Design Pattern

Style getComponentA() getComponentB()

Client doOperation( Style myStyle )

Style1 getComponentA() getComponentB()

Style2 getComponentA() getComponentB()

ComponentA ComponentB

Style1ComponentA

Style1ComponentB

Style2ComponentA

Style2ComponentB

Collection

Page 13: 6 Introduction to Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/6... · 2017-12-24 · Introduction to Design Patterns 3 Types: Creational

Software Architecture – Intro to Design Patterns 13

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Abstract Factory Design Pattern Alternative

Style getComponentA() getComponentB()

Client doOperation()

Style1 getComponentA() getComponentB()

Style2 getComponentA() getComponentB()

ComponentA ComponentB

Style1ComponentA

Style1ComponentB

Style2ComponentA

Style2ComponentB

Collection getComponentA() getComponentB()

does not reference style directly doOperation() takes no parameters Collection has methods for getting the various components

Page 14: 6 Introduction to Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/6... · 2017-12-24 · Introduction to Design Patterns 3 Types: Creational

Software Architecture – Intro to Design Patterns 14

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Design Patterns -- class combination and algorithm fulfilling a common design purpose. Key Concept: à Creational Design Patterns ß -- used to create objects in flexible or constrained ways. Key Concept: à Structural Design Patterns ß -- used to represent data structures such as trees, with uniform processing interfaces.

Key Concept: à Behavioral Design Patterns ß

-- to capture behavior among objects.

Page 15: 6 Introduction to Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/6... · 2017-12-24 · Introduction to Design Patterns 3 Types: Creational

Software Architecture – Intro to Design Patterns 15

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Creational Design Patterns

•  Creational design patterns help us to design applications involving collections of objects: –  They allow the creation of several possible creations

from a single block of code such as: •  Creating many versions of the collection at runtime •  Constraining the objects created: for example, ensuring

that there is only one instance of its class

•  Creational design patterns discussed in next lecture: –  Factory –  Abstract Factory –  Prototype –  Singleton

Page 16: 6 Introduction to Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/6... · 2017-12-24 · Introduction to Design Patterns 3 Types: Creational

Software Architecture – Intro to Design Patterns 16

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Structural Design Patterns

•  Structural design patterns help us to arrange collections of objects in forms such as linked lists or trees.

•  Structural design patterns discussed in lecture 8: –  Composite –  Decorator –  Adapter –  Facade –  Flyweight –  Proxy

Page 17: 6 Introduction to Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/6... · 2017-12-24 · Introduction to Design Patterns 3 Types: Creational

Software Architecture – Intro to Design Patterns 17

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Example of Behavioral Design Goal: Port Traffic

berth berth

to drydock à

obstacles

A systematic simulation of the transportation process

Page 18: 6 Introduction to Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/6... · 2017-12-24 · Introduction to Design Patterns 3 Types: Creational

Software Architecture – Intro to Design Patterns 18

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Design Goal At Work: à Reusability ß

We want to be able to use classes Ship and Tugboat separately in other applications

Page 19: 6 Introduction to Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/6... · 2017-12-24 · Introduction to Design Patterns 3 Types: Creational

Software Architecture – Intro to Design Patterns 19

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Ship Tugboat 1..n 1

Harbor application

Customs application: reuse Ship alone

Ship Longshoreman

×

Avoiding Dependencies

Page 20: 6 Introduction to Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/6... · 2017-12-24 · Introduction to Design Patterns 3 Types: Creational

Software Architecture – Intro to Design Patterns 20

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Core Mediator Concept Applied to The Harbor Problem

Ship

Tugboat

LeavingPort estimateTime()

Mediator Pattern: Captures the interaction between objects without having them reference each other (thus permitting reuse). Captures each interaction in a separate class that aggregates the objects involved.

The full Mediator Pattern allows for the fact that the mediated classes may need to communicate (e.g., to respond to events on either of them). To accomplish this, the mediated classes can be made to subclass a base class that aggregates a generic mediator class (PortMission).

Page 21: 6 Introduction to Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/6... · 2017-12-24 · Introduction to Design Patterns 3 Types: Creational

Software Architecture – Intro to Design Patterns 21

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

App

lyin

g th

e M

edia

tor D

esig

n Pa

ttern

to T

he H

arbo

r Pro

blem

Ship Tugboat

Vessel PortMission estimateTime()

LeavingPort estimateTime()

EnteringPort estimateTime()

BeingMaintained estimateTime()

Mediator base class

Page 22: 6 Introduction to Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/6... · 2017-12-24 · Introduction to Design Patterns 3 Types: Creational

Software Architecture – Intro to Design Patterns 22

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Chain of Responsibility Design Pattern

•  Goal: A collection of objects to exhibit functionality. –  At design time we want to be able to easily add or delete

objects that handle all or part of the responsibility. •  Example: Design a GUI for a web application to

view automobiles with requested color etc. –  The display is dynamic, depending on the model, color

etc. chosen. –  Reuse the GUI parts among the displays.

•  Solution: Link objects to each other via aggregation. Each performs some of the work, and then calls on the next object in the chain to continue the work.

Page 23: 6 Introduction to Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/6... · 2017-12-24 · Introduction to Design Patterns 3 Types: Creational

Software Architecture – Intro to Design Patterns 23

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

You have requested to view a luxury model with luxury body style, and black body color.

Model

Body Style

Body color

Choose an automobile to view.

OK

A D

esig

n Pr

oble

m S

olva

ble

by C

hain

of R

espo

nsib

ility

Basic Midsize Luxury SUV

Luxury Extra Basic

Black Brown Red White

This part is fixed

Display depends on model selected

Display depends on body style selected

Assume that each of these will eventually be a small, expensive animation.

Page 24: 6 Introduction to Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/6... · 2017-12-24 · Introduction to Design Patterns 3 Types: Creational

Software Architecture – Intro to Design Patterns 24

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Characteristics of Design Patterns 1

q  Viewpoints – ways to describe patterns 1.  Static: class model (building blocks) 2.  Dynamic: sequence or state diagram (operation)

q  Levels – decomposition of patterns 1.  Abstract level describes the core of the pattern 2.  Concrete (= non abstract) level describes the

particulars of this case

q  Roles – the “players” in pattern usage 1.  Application of the design pattern itself 2.  Clients of the design pattern application 3.  Setup code initializes and controls

Page 25: 6 Introduction to Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/6... · 2017-12-24 · Introduction to Design Patterns 3 Types: Creational

Software Architecture – Intro to Design Patterns 25

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Characteristics of Design Patterns 2 1. Client role

2. Setup role

A

C

A. Static viewpoint B. Dynamic viewpoint

3. Role: Application of the design pattern

D

B (i) Abstract level

(ii) Concrete level

(class model) (sequence or state diagram)

(class or classes)

(class or classes)

: Reference direction

Page 26: 6 Introduction to Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/6... · 2017-12-24 · Introduction to Design Patterns 3 Types: Creational

Software Architecture – Intro to Design Patterns 26

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

getWallCabinet()

Abstract Factory Application Sequence Diagram

myStyle:KitchenStyle Client

myStyle: ModernKStyle

myStyle: AntiqueKStyle

renderKitchen ( myStyle )

wallCabinet1: ModernWallCabinet

wallCabinet1: AntiqueWallCabinet

ModernWallCabinet() getWallCabinet()

AntiqueWallCabinet()

myStyle. getWallCabinet()

-- IF myStyle BELONGS TO ModernKStyle --

-- IF myStyle BELONGS TO AntiqueKStyle --

Page 27: 6 Introduction to Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/6... · 2017-12-24 · Introduction to Design Patterns 3 Types: Creational

Software Architecture – Intro to Design Patterns 27

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Key Concept: à Two Viewpoints ß

We consider design patterns from the static viewpoint (what they are made from) and the dynamic viewpoint (how they function).

Key Concept: à Two Levels ß

Design patterns usually have an abstract level and a non-abstract (“concrete”) level.

Page 28: 6 Introduction to Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/6... · 2017-12-24 · Introduction to Design Patterns 3 Types: Creational

Software Architecture – Intro to Design Patterns 28

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Concrete and Abstract Layers

KitchenStyle

Kitchen

Client

ModernKStyle

AntiqueKStyle

WallCabinet

FloorCabinet

ModernWallCabinet

ModernFloorCabinet

AntiqueWallCabinet

AntiqueFloorCabinet

Abstract level

Concrete level

Page 29: 6 Introduction to Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/6... · 2017-12-24 · Introduction to Design Patterns 3 Types: Creational

Software Architecture – Intro to Design Patterns 29

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Design Goal At Work: à Correctness ß

We want to provide an interface to a design pattern so that its functionality is clear and separate.

Page 30: 6 Introduction to Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/6... · 2017-12-24 · Introduction to Design Patterns 3 Types: Creational

Software Architecture – Intro to Design Patterns 30

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Key Concept: à Three Roles ß

A part of a design either 1) applies a design pattern, 2) is a client of the pattern application, or 3) re/initializes these.

Page 31: 6 Introduction to Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/6... · 2017-12-24 · Introduction to Design Patterns 3 Types: Creational

Software Architecture – Intro to Design Patterns 31

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

2. Client Role 1. Design Pattern Application

Interface to the pattern

(frequently abstract;

possibly several classes)

DPClient DPInterface

Rest of the

design pattern

application

Interacts with the design pattern only through its interface

3. Setup Role

The Three Roles Involved in Design Pattern Usage

Rest of the Application

No limitations

Page 32: 6 Introduction to Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/6... · 2017-12-24 · Introduction to Design Patterns 3 Types: Creational

Software Architecture – Intro to Design Patterns 32

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Key Concept: à Two Forms ß

A design pattern’s form is usually a delegation of responsibility or a class that relates to itself (recursive).

Metapatterns – design patterns based on delegation or recursion

Page 33: 6 Introduction to Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/6... · 2017-12-24 · Introduction to Design Patterns 3 Types: Creational

Software Architecture – Intro to Design Patterns 33

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Basic Idea of Delegation

requiredFunction()

… intermediaryFunction( … ) { … requiredFunction() … }

Client clientFunction() intermediaryFunction()

replace

… clientFunction( … ) { … intermediaryFunction( … ) … }

The Abstract Factory design pattern replaces several direct method calls (constructor calls actually) with delegated calls to separate methods, which call the desired methods in turn.

Page 34: 6 Introduction to Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/6... · 2017-12-24 · Introduction to Design Patterns 3 Types: Creational

Software Architecture – Intro to Design Patterns 34

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Basic Design Pattern Form #1: Delegation

DoerBase doIt()

DPInterface interfaceMethod()

ConcreteDoer1 doIt()

ConcreteDoer2 doIt()

. . .

doerObject

… interfaceMethod( … ) { … doerObject.doIt() … }

Client

Page 35: 6 Introduction to Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/6... · 2017-12-24 · Introduction to Design Patterns 3 Types: Creational

Software Architecture – Intro to Design Patterns 35

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Basic Design Pattern Form #2: Recursion RecursionBase

doOperation()

NonrecursiveClass doOperation()

RecursiveClass doOperation()

void doOperation( … ) { … aggregate … }

aggregate

Client

note dual inheritance/aggregation relationship

Page 36: 6 Introduction to Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/6... · 2017-12-24 · Introduction to Design Patterns 3 Types: Creational

Software Architecture – Intro to Design Patterns 36

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

The Recursion Form Applied to an Organization Chart

Employee printOrganization()

IndividualContributor printOrganization()

Supervisor printOrganization()

void printOrganization( … ) { … supervisees.printOrganization() … }

supervises

Client

The printOrganization() method in Supervisor programmed to 1st print the supervisor’s name, then call the printOrganization() method in each of the aggregated employees. For employee objects in IndividualContributor, only the name of the employee is printed.

Page 37: 6 Introduction to Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/6... · 2017-12-24 · Introduction to Design Patterns 3 Types: Creational

Software Architecture – Intro to Design Patterns 37

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Summary of This Chapter

q  Design Patterns are recurring designs satisfying recurring design purposes

q  Described by Static and Dynamic Viewpoints o  Typically class models and sequence diagrams respectively

q  Use of a pattern application is a Client Role o  Client interface carefully controlled o  “Setup,” typically initialization, a separate role

q  Design patterns Forms usually Delegation or Recursion

q  Classified as Creational, Structural, or Behavioral