DESIGN PATTERNS INTRODUCTION. Design Patterns What is a Pattern? "Each pattern describes a problem...

Post on 18-Jan-2016

231 views 0 download

Tags:

Transcript of DESIGN PATTERNS INTRODUCTION. Design Patterns What is a Pattern? "Each pattern describes a problem...

DESIGN PATTERNS INTRODUCTION

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

Design Pattern SpacePurpose

Scope Creational Structural Behavioral

Class Factory Method Adapter InterpreterTemplate Method

Object Abstract factoryBuilderPrototypeSingleton

AdapterBridgeCompositeFacadeFlyweightProxy

Chain of ResponsibilityCommandIteratorMediatorMementoStateStrategyVisitor

OMT-Based Notation

Examples from Text

Class Diagram Object Diagram

Inheritance

OMT-Based Notation

Examples from Text

Abstract and Implementation

Class Relations

Creating

OMT-Based Notation

Examples from Text Object Interaction

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

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.

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

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

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

Designing for Change

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

Decorator, Observer, Strategy

Inability to alter classes conveniently Adapter, Decorator, Visitor

Singleton

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

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

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(); } }

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;

}

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

Structure

Participants

Singleton •Defines an getInstance operation that

lets clients access its unique instance

•May be responsible for creating its own unique instance

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