design patterns introduction. design patterns what is a pattern? "each pattern describes a...

20
DESIGN PATTERNS INTRODUCTION

Upload: hilary-berry

Post on 18-Jan-2016

224 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: DESIGN PATTERNS INTRODUCTION. Design Patterns  What is a Pattern?  "Each pattern describes a problem which occurs over and over again in our environment,

DESIGN PATTERNS INTRODUCTION

Page 2: DESIGN PATTERNS INTRODUCTION. Design Patterns  What is a Pattern?  "Each pattern describes a problem which occurs over and over again in our environment,

Design Patterns

What is a Pattern? "Each pattern describes a problem which occurs over

and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice"

Christopher Alexander A pattern has four essential elements

Pattern name Problem Solution

"the pattern provides an abstract description of a design problem and a general arrangement of elements solves it"

Consequences

Page 3: DESIGN PATTERNS INTRODUCTION. Design Patterns  What is a Pattern?  "Each pattern describes a problem which occurs over and over again in our environment,

Design Pattern SpacePurpose

Scope Creational Structural Behavioral

Class Factory Method Adapter InterpreterTemplate Method

Object Abstract factoryBuilderPrototypeSingleton

AdapterBridgeCompositeFacadeFlyweightProxy

Chain of ResponsibilityCommandIteratorMediatorMementoStateStrategyVisitor

Page 4: DESIGN PATTERNS INTRODUCTION. Design Patterns  What is a Pattern?  "Each pattern describes a problem which occurs over and over again in our environment,

OMT-Based Notation

Examples from Text

Class Diagram Object Diagram

Inheritance

Page 5: DESIGN PATTERNS INTRODUCTION. Design Patterns  What is a Pattern?  "Each pattern describes a problem which occurs over and over again in our environment,

OMT-Based Notation

Examples from Text

Abstract and Implementation

Class Relations

Creating

Page 6: DESIGN PATTERNS INTRODUCTION. Design Patterns  What is a Pattern?  "Each pattern describes a problem which occurs over and over again in our environment,

OMT-Based Notation

Examples from Text Object Interaction

Page 7: DESIGN PATTERNS INTRODUCTION. Design Patterns  What is a Pattern?  "Each pattern describes a problem which occurs over and over again in our environment,

Object, Classes and Type (Again)Signature

An operations name, parameters, and return typeInterface

Set of all signatures defined by an object's operationsType

A name used to denote a particular interface Objects may have many types Different objects in share a typeSubtype

A type is a subtype of another it its interface contains the interface of its supertype

Class A class defines an object's implementation

Page 8: DESIGN PATTERNS INTRODUCTION. Design Patterns  What is a Pattern?  "Each pattern describes a problem which occurs over and over again in our environment,

Design Principle 1

Program to an interface, not an implementation

Use abstract classes (and/or interfaces in Java) to define common interfaces for a set of classes

Declare variables to be instances of the abstract class not instances of particular classes

Benefits of programming to an interface Client classes/objects remain unaware of the classes of

objects they use, as long as the objects adhere to the interface the client expects

Client classes/objects remain unaware of the classes that implement these objects. Clients only know about the abstract classes (or interfaces) that define the interface.

Page 9: DESIGN PATTERNS INTRODUCTION. Design Patterns  What is a Pattern?  "Each pattern describes a problem which occurs over and over again in our environment,

Design Principle 2 Favor object composition over class inheritance

Composition Allows behavior changes at run time

Helps keep classes encapsulated and focused on one task

Reduce implementation dependencies

Page 10: DESIGN PATTERNS INTRODUCTION. Design Patterns  What is a Pattern?  "Each pattern describes a problem which occurs over and over again in our environment,

Designing for Change

Some common design problems with design patterns that address the problem

Creating an object by specifying a class explicitly Abstract factory, Factory Method, Prototype

Dependence on specific operations Chain of Responsibility, Command

Dependence on hardware and software platforms Abstract factory, Bridge

Page 11: DESIGN PATTERNS INTRODUCTION. Design Patterns  What is a Pattern?  "Each pattern describes a problem which occurs over and over again in our environment,

Designing for Change

Dependence on object representations/implementations Abstract factory, Bridge, Memento, Proxy

Algorithmic dependencies Builder, Iterator, Strategy, Template Method, Visitor

Tight Coupling Abstract factory, Bridge, Chain of Responsibility,

Command, Facade, Mediator, Observer

Page 12: DESIGN PATTERNS INTRODUCTION. Design Patterns  What is a Pattern?  "Each pattern describes a problem which occurs over and over again in our environment,

Designing for Change

Extending functionality by subclassing Bridge, Chain of Responsibility, Composite,

Decorator, Observer, Strategy

Inability to alter classes conveniently Adapter, Decorator, Visitor

Page 13: DESIGN PATTERNS INTRODUCTION. Design Patterns  What is a Pattern?  "Each pattern describes a problem which occurs over and over again in our environment,

Singleton

Page 14: DESIGN PATTERNS INTRODUCTION. Design Patterns  What is a Pattern?  "Each pattern describes a problem which occurs over and over again in our environment,

Singleton

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

MotivationThere are times when a class can only have one instance

ApplicabilityUse the Singleton pattern when there must be only 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 15: DESIGN PATTERNS INTRODUCTION. Design Patterns  What is a Pattern?  "Each pattern describes a problem which occurs over and over again in our environment,

Situations for Using a Singleton Java Security manager

All parts of a program must access the same security manager

Once set a security manager cannot be changed in a program

Logging the activity of a server All parts of the server should use the same

instance of the logging system The server should not be able to change the

instance of the logging system was it has been set

Page 16: DESIGN PATTERNS INTRODUCTION. Design Patterns  What is a Pattern?  "Each pattern describes a problem which occurs over and over again in our environment,

ImplementationJava

// Only one object of this class can be created

class Singleton {

private static Singleton _instance = null;

private Singleton() { fill in the blank }

public static Singleton getInstance() {

if ( _instance == null )

_instance = new Singleton();

return _instance;

}

public void otherOperations() { blank; }

}

class Program { public void aMethod() { X = Singleton.getInstance(); } }

Page 17: DESIGN PATTERNS INTRODUCTION. Design Patterns  What is a Pattern?  "Each pattern describes a problem which occurs over and over again in our environment,

ImplementationC++

// Only one object of this class can be created class Singleton {

private:

static Singleton* _instance;

void otherOperations();

protected:

Singleton();

public: static Singleton* getInstance();

} Implementation Singleton* Singleton::_instance = 0;

Singleton* Singleton::getInstance() {

if ( _instance == 0 ) _instance = new Singleton;

return _instance;

}

Page 18: DESIGN PATTERNS INTRODUCTION. Design Patterns  What is a Pattern?  "Each pattern describes a problem which occurs over and over again in our environment,

Singleton and Inheritance

Singleton has subclasses Each subclass needs to be a singleton A program can have one copy of each class

Solution Just make each subclass a singleton

Page 19: DESIGN PATTERNS INTRODUCTION. Design Patterns  What is a Pattern?  "Each pattern describes a problem which occurs over and over again in our environment,

Structure

Participants

Singleton •Defines an getInstance operation that

lets clients access its unique instance

•May be responsible for creating its own unique instance

Page 20: DESIGN PATTERNS INTRODUCTION. Design Patterns  What is a Pattern?  "Each pattern describes a problem which occurs over and over again in our environment,

Structure

CollaborationsClients access a Singleton instance solely through

Singleton's getInstance operation

Consequences•Controlled access to sole instance

•Reduced name space

•Permits subclassing

•Permits a variable number of instances

•More flexible than class operations