Lecture 21: Design Patterns III - University of North ... 2008... · • Dec. 4 (2 teams) ... The...

34
Lecture 21: Design Patterns III Software System Design and Implementation ITCS/ITIS 6112/8112 001 Fall 2008 Dr. Jamie Payton Department of Computer Science University of North Carolina at Charlotte Nov. 18, 2008

Transcript of Lecture 21: Design Patterns III - University of North ... 2008... · • Dec. 4 (2 teams) ... The...

Lecture 21:Design Patterns III

Software System Design and ImplementationITCS/ITIS 6112/8112 001

Fall 2008

Dr. Jamie PaytonDepartment of Computer Science

University of North Carolina at Charlotte

Nov. 18, 2008

Announcements

D3 due today at 11:59 pmProject assessments due by 11/20/08 at 11:59 pm

New links to corrected surveys!

D4: PresentationsDates• Dec. 2 (2 teams)• Dec. 4 (2 teams)• Dec. 9 (3 teams)

D4: Presentations

Professional presentationSlides/visual aidsPolished• Practice!

Length20 minutes5 additional minutes for questions

GradingPresentation ContentPresentation StylePresentation Time Management Project Progress

D4: Presentations

ContentProblem Statement• What you’re working on and why it is important to your

stakeholdersBig picture!

Design• Present an overview of your software architecture and mid-level

designDiscuss any consideration of patterns (architectural and/or design)

• Highlight important design decisions

D4: Presentations

Content…Progress• Can show live prototype, recorded video of prototype, or screenshots• If prototype is not viable, discuss your progress so far

Expected outcome and project plan• What you expect to be in your final prototype• How you plan to design/implement to achieve goal

Peer-assessmentHow did your teammates contribute to preparing the presentation?How did you teammates contribute to progress so far?How is your team dividing the remaining work?

Lecture Overview

Patterns Course evaluations

7

Types of Patterns

BehavioralObserverMediator

StructuralFaçadeAdapterProxy

CreationalAbstract Factory

Image Processing Application

RequirementsMust store many imagesAllows for image editingSupports animation

ProblemCopying images requires significant time, memory• Need to avoid copying images as much as possible

SolutionCopy-on-write• Have a placeholder keep a reference to original image, create a

copy only when needed

9

The Proxy PatternDescription

Provide a placeholder for another object Applicability

Remote proxies• Hide location of objectVirtual proxies • Create expensive objects on demandProtection proxies • Control access to an objectSmart proxies may: • Count number of references to object (so it can be freed)• Load persistent object into memory on first reference• Prevent changes by other objectsIn general:• Need a more versatile or sophisticated reference to an object than a

pointer

10

The Proxy Pattern

ParticipantsProxy• Maintains a reference that lets Proxy access RealSubject• Provides an interface identical to Subject’s so that it can

substitute for the real object• Controls access to RealSubject

May create and delete RealSubject

Subject• Defines the common interface for RealSubject and Proxy so that

Proxy can substitute for RealSubject

RealSubject• Defines the real object that the Proxy represents

Proxy Pattern Structure

RealSubjectProxy

«interface»Subject

request ()

Client

Proxy Pattern Behavior

:Client

request()

request() request()

:Proxy :RealSubject

sd ProxyBehavior

Example: Image Proxy

Proxy Pattern

ConsequencesRemote proxy can hide the fact that an object resides in a different address spaceVirtual proxy can perform optimizations such as creating an object on demandWhen used for copy-on-write, requires reference counting on real object

15

Proxy

AnalogyATM card substitutes for cashManager sends subordinate to meeting

Proxy vs. Adapter

AdapterProvides a different interface to object it adaptsProxy provide same interface to object

17

Types of Patterns

BehavioralDescribe how classes interact and distribute responsibility• Observer• Mediator

StructuralDescribe how classes or objects should be composed• Façade• Adapter• Proxy

CreationalConcern the process of object creation• Factory method• Abstract Factory

18

Traffic Simulator

Want to design a traffic simulator to study traffic patterns

We have Road and VehiclesVehicles are of different types• SUV• Compact• MiniVan• MidSizeVehicles should be generated to match expected distributionWhich class should do this?• Road?• Class?• Neither, really

19

The Factory Method Pattern

ApplicabilityClients do not need to know details of object implementation, just need it to implement particular interface

ConsequencesClient does not have to create objects• Created by generator

Allows choice of actual object type at runtime

20

Factory Method ExamplesAnalogy

Automobile production• SUVs: Ford Explorer, Acura MDX, Subaru OutbackRental car company• Ask for a compact car, but you don’t care if it’s a Civic

Design ExampleVehicle traffic simulator• Vehicle and Road classes• Must randomly create new Vehicle to adhere to expected distribution of

vehicle typesCompact, SUV, Minivan, Truck

• Which class chooses Vehicle type?Road? VehicleFactory!

Factory Method Pattern

DescriptionDefines an interface for creating an object, but lets subclasses decide which class to instantiate

ApplicabilityA class can’t anticipate the class of objects it must createA class wants its subclasses to specify the objects it creates

Factory Method Pattern

ParticipantsProduct• Defines the interface of objects the factory method creates

Concrete Product• Implements the Product interface

Creator (aka Factory)• Declares the factory method

Returns an object of type ProductMay also define a default implementation that returns a default ConcreteProduct

• May call the factory method to create a Product object

Factory Method Pattern Structure

Factory Method Behavior

Factory Method Pattern

ConsequencesEliminates need to bind application-specific classes into code• Code only deals with Product interface• Code can work with any user-defined ConcreteProduct

Clients might need to subclass Creator just to create a particular ConcreteProduct

The Iterator and Factory Method Patterns

27

Types of Patterns

BehavioralObserverMediator

StructuralFaçadeAdapterProxy

CreationalFactoryAbstract Factory

28

The Abstract Factory Pattern

DescriptionProvides an interface for creating families of related or dependent objects without specifying their concrete classes

29

The Abstract Factory Pattern (2)

ApplicabilityNeed to abstract from details of implementation of products Need to have multiple families of products Need to enforce families of products that must be used together Need to hide product implementations and just present interfaces

ConsequencesIsolates concrete classes Makes exchanging product families easy Promotes consistency among products Supporting new kinds (in each family) of products is difficult

30

The Abstract Factory Pattern

AbstractFactory

CreateProductA()

CreateProductB()

ConcreteFactory1

Client

ProductA1ProductA2

AbstractProductA

ProductB2 ProductB1

AbstractProductB

ConcreteFactory2

CreateProductA()

CreateProductB()

31

Abstract Factory Example

WidgetFactory

CreateScrollbar()

CreateWindow()

Window

ScrollBarWWidgetFactory

MacWidgetFactory

Client

WWindowMacWindow

MacScrollBar WScrollBar

One for each standard.

32

Factory Method versus Abstract Factory

Abstract Factory uses Factory Method

33

Design patterns are generic, reusable design templatesDesign patterns provide a vocabulary for discussing designThree types of patterns

BehavioralStructuralCreational

Summary

34

Resources

Introduction to Software Engineering DesignChristopher Fox

GoF bookPoSA bookArticles posted on course website