Design Patterns 1. Design patterns are solutions to problems that arise when developing software...

32
Design Patterns 1

Transcript of Design Patterns 1. Design patterns are solutions to problems that arise when developing software...

Page 1: Design Patterns 1. Design patterns are solutions to problems that arise when developing software within a particular context Patterns capture the structure.

1

Design Patterns

Page 2: Design Patterns 1. Design patterns are solutions to problems that arise when developing software within a particular context Patterns capture the structure.

2

Design Patterns

• Design patterns are solutions to problems that arise when developing software within a particular context

• Patterns capture the structure and collaboration among key participants in software designs

• Patterns facilitate reuse of successful software architectures and designs

Page 3: Design Patterns 1. Design patterns are solutions to problems that arise when developing software within a particular context Patterns capture the structure.

3

Design Pattern Descriptions

• Name• Problem

– What is the problem and the context where we would use this pattern?

– Under what specific conditions should this pattern be used?• Solution

– A description of the elements that make up the design pattern– Emphasizes their relationships, responsibilities, and collaborations– Not a concrete design or implementation; rather an abstract

description• Positive and negative consequences of use

– The pros and cons of using the pattern– Includes impacts on reusability, portability, and extensibility

Page 4: Design Patterns 1. Design patterns are solutions to problems that arise when developing software within a particular context Patterns capture the structure.

4

Organizing Patterns

• Purpose: What a pattern does– Creational: creating, initializing and configuring classes and

objects– Structural patterns: composition of classes and objects– Behavioral patterns: dynamic interactions among classes and

objects• Scope: what the pattern applies to

– Class patterns:• Focus on the relationships between classes and their subclasses• Involve inheritance reuse

– Object patterns:• Focus on relationships between objects• Involve composition reuse

Page 5: Design Patterns 1. Design patterns are solutions to problems that arise when developing software within a particular context Patterns capture the structure.

Patterns and Language

• Most design patterns focus on OO– Assume inheritance, polymorphism,

encapsulation, etc.• In procedural languages, might add OO

features as “patterns”• Some languages provide or make it easier to

implement some patterns

5

Page 6: Design Patterns 1. Design patterns are solutions to problems that arise when developing software within a particular context Patterns capture the structure.

6

Abstract Factory

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

Page 7: Design Patterns 1. Design patterns are solutions to problems that arise when developing software within a particular context Patterns capture the structure.

7

Abstract Factory

MacintoshWidgetFactory

createScrollBar()createWindow()

LinuxWidgetFactory

createScrollBar()createWindow()

MacintoshWindow

MacintoshScrollBar

LinuxWindow

LinuxScrollBar

WidgetFactory

createScrollBar()...createWindow()

Window

Client

ScrollBar

Page 8: Design Patterns 1. Design patterns are solutions to problems that arise when developing software within a particular context Patterns capture the structure.

8

Abstract Factory

• Use the Abstract Factory pattern when– a system should be independent of how its products

are created, composed, and represented. – a system should be configured with one of multiple

families of products. – a family of related product objects is designed to be

used together, and you need to enforce this constraint.

– you want to provide a class library of products, and you want to reveal just their interfaces, not their implementations.

Page 9: Design Patterns 1. Design patterns are solutions to problems that arise when developing software within a particular context Patterns capture the structure.

9

Abstract Factory

• AbstractFactory (WidgetFactory) – declares an interface for operations that create abstract product

objects.• ConcreteFactory (MacintoshWidgetFactory, LinuxWidgetFactory)

– implements the operations to create concrete product objects.• AbstractProduct (Window, ScrollBar)

– declares an interface for a type of product object.• ConcreteProduct (MacintoshWindow, MacintoshScrollBar)

– defines a product object to be created by the corresponding concrete factory.

– implements the AbstractProduct interface.• Client

– uses only interfaces declared by AbstractFactory and AbstractProduct classes.

Page 10: Design Patterns 1. Design patterns are solutions to problems that arise when developing software within a particular context Patterns capture the structure.

10

Abstract Factory

• Collaborations – Normally a single instance of a ConcreteFactory

class is created at run-time. This concrete factory creates product objects having a particular implementation. To create different product objects, clients should use a different concrete factory.

– AbstractFactory defers creation of product objects to its ConcreteFactory subclass.

Page 11: Design Patterns 1. Design patterns are solutions to problems that arise when developing software within a particular context Patterns capture the structure.

11

Abstract Factory

• Consequences– It isolates concrete classes– It makes exchanging product families easy– It promotes consistency among products– Supporting new kinds of products is difficult

Page 12: Design Patterns 1. Design patterns are solutions to problems that arise when developing software within a particular context Patterns capture the structure.

12

Factory Method

• Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.

Page 13: Design Patterns 1. Design patterns are solutions to problems that arise when developing software within a particular context Patterns capture the structure.

13

Factory Method

MyDocument MyApplication

createDocument()

Application

createDocument()newDocument()openDocument()

Document

open()close()save()revert()

0..n

+docs Document* doc=createDocument();docs.add(doc);doc->open();

return new MyDocument();

Page 14: Design Patterns 1. Design patterns are solutions to problems that arise when developing software within a particular context Patterns capture the structure.

14

Factory Method

• Use the Factory Method pattern when– a class can't anticipate the class of objects it must

create. – a class wants its subclasses to specify the objects

it creates.

Page 15: Design Patterns 1. Design patterns are solutions to problems that arise when developing software within a particular context Patterns capture the structure.

15

Factory Method

• Participants– Product (Document)

• defines the interface of objects the factory method creates.

– ConcreteProduct (MyDocument) • implements the Product interface.

– Creator (Application) • declares the factory method, which returns an object of type

Product. Creator may also define a default implementation of the factory method that returns a default ConcreteProduct object.

• may call the factory method to create a Product object.

– ConcreteCreator (MyApplication) • overrides the factory method to return an instance of a

ConcreteProduct.

Page 16: Design Patterns 1. Design patterns are solutions to problems that arise when developing software within a particular context Patterns capture the structure.

16

Singleton

Singleton

static uniqueInstancedata

static Instance()operation()getData()

//Instance()return uniqueInstance;

Ensure a class only has one instance, and provide a global point of access to it.

Page 17: Design Patterns 1. Design patterns are solutions to problems that arise when developing software within a particular context Patterns capture the structure.

17

Singleton

• Use the Singleton pattern when– there must be exactly one instance of a class, and

it must be accessible to clients from a well-known access point.

– when the sole instance should be extensible by subclassing, and clients should be able to use an extended instance without modifying their code.

Page 18: Design Patterns 1. Design patterns are solutions to problems that arise when developing software within a particular context Patterns capture the structure.

18

Singleton

• Consequences– Controlled access to sole instance– Permits refinement of operations – Permits a variable number of instances– More flexible than class operations

Page 19: Design Patterns 1. Design patterns are solutions to problems that arise when developing software within a particular context Patterns capture the structure.

Adapter

• Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.

19

Page 20: Design Patterns 1. Design patterns are solutions to problems that arise when developing software within a particular context Patterns capture the structure.

20

Adapter

Page 21: Design Patterns 1. Design patterns are solutions to problems that arise when developing software within a particular context Patterns capture the structure.

21

Adapter

Page 22: Design Patterns 1. Design patterns are solutions to problems that arise when developing software within a particular context Patterns capture the structure.

22

Adapter

Page 23: Design Patterns 1. Design patterns are solutions to problems that arise when developing software within a particular context Patterns capture the structure.

23

Decorator

• Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.

Page 24: Design Patterns 1. Design patterns are solutions to problems that arise when developing software within a particular context Patterns capture the structure.

24

Decorator

TextView

draw()

ScrollDecorator

position

draw()scrollTo()

BorderDecorator

draw()drawBorder()

Decorator

draw()

VisualComponent

draw()

+component

//draw()component->draw();

//draw()Decorator::draw();drawBorder();

Page 25: Design Patterns 1. Design patterns are solutions to problems that arise when developing software within a particular context Patterns capture the structure.

25

Decorator

• Use Decorator– to add responsibilities to individual objects

dynamically and transparently, that is, without affecting other objects.

– for responsibilities that can be withdrawn. – when extension by subclassing is impractical.

Page 26: Design Patterns 1. Design patterns are solutions to problems that arise when developing software within a particular context Patterns capture the structure.

26

Decorator

• Participants– Component (VisualComponent)

• defines the interface for objects that can have responsibilities added to them dynamically.

– ConcreteComponent (TextView) • defines an object to which additional responsibilities can be

attached.

– Decorator • maintains a reference to a Component object and defines an

interface that conforms to Component's interface.

– ConcreteDecorator (BorderDecorator, ScrollDecorator) • adds responsibilities to the component.

Page 27: Design Patterns 1. Design patterns are solutions to problems that arise when developing software within a particular context Patterns capture the structure.

27

Decorator

• Consequences– More flexibility than static inheritance– Avoids feature-laden classes high up in the

hierarchy– A decorator and its component aren't identical– Lots of little objects

Page 28: Design Patterns 1. Design patterns are solutions to problems that arise when developing software within a particular context Patterns capture the structure.

28

Facade

• Toolkit vs. application• A facade is a class with a level of functionality

that lies between a toolkit and a complete application

• The intent of the facade pattern is to provide an interface that makes a subsystem easy to use

Page 29: Design Patterns 1. Design patterns are solutions to problems that arise when developing software within a particular context Patterns capture the structure.

29

Observer

ConcreteSubject

subjectState

getState()setState()

ConcreteObserver

observerState

update()

Subject

attach(Observer)detach(Observer)notify()

Observer

update()

+observers

//notify()for all o in observers{ o->update();}

//getState();return subjectState;

//update()observerState=subject->getState();

Page 30: Design Patterns 1. Design patterns are solutions to problems that arise when developing software within a particular context Patterns capture the structure.

30

Observer

• Use the Observer pattern in any of the following situations:– When a change to one object requires changing

others, and you don't know how many objects need to be changed.

– When an object should be able to notify other objects without making assumptions about who these objects are. In other words, you don't want these objects tightly coupled.

Page 31: Design Patterns 1. Design patterns are solutions to problems that arise when developing software within a particular context Patterns capture the structure.

31

Observer

• Participants– Subject

• knows its observers. Any number of Observer objects may observe a subject. • provides an interface for attaching and detaching Observer objects.

– Observer • defines an updating interface for objects that should be notified of changes in

a subject.

– ConcreteSubject • stores state of interest to ConcreteObserver objects. • sends a notification to its observers when its state changes.

– ConcreteObserver • maintains a reference to a ConcreteSubject object. • stores state that should stay consistent with the subject's. • implements the Observer updating interface to keep its state consistent with

the subject's.

Page 32: Design Patterns 1. Design patterns are solutions to problems that arise when developing software within a particular context Patterns capture the structure.

32