+ All Categories
Home > Documents > What Are Design Patterns and Why Do I Need Them? Software professionals may be familiar with the...

What Are Design Patterns and Why Do I Need Them? Software professionals may be familiar with the...

Date post: 31-Mar-2015
Category:
Upload: walker-carville
View: 219 times
Download: 0 times
Share this document with a friend
Popular Tags:
22
What Are Design Patterns and Why Do I Need Them? Software professionals may be familiar with the term "Design Patterns," but many have no idea of where they come from and what they truly are. Consequently, some do not see the value and benefits design patterns bring to the software development process, especially in the areas of maintenance and code reuse. This talk will provide an introductory to design patterns from a historical perspective.
Transcript
Page 1: What Are Design Patterns and Why Do I Need Them? Software professionals may be familiar with the term "Design Patterns," but many have no idea of where.

What Are Design Patterns and Why Do I Need Them?

• Software professionals may be familiar with the term "Design Patterns," but many have no idea of where they come from and what they truly are.

• Consequently, some do not see the value and benefits design patterns bring to the software development process, especially in the areas of maintenance and code reuse.

• This talk will provide an introductory to design patterns from a historical perspective.

Page 2: What Are Design Patterns and Why Do I Need Them? Software professionals may be familiar with the term "Design Patterns," but many have no idea of where.

Is This A New Idea?

• Town planners and architects talk of design patterns all the time.

• Design patterns have their roots in the work of Christopher Alexander an architect. When talking about patterns in buildings and towns in 1977, he wrote

• “Each pattern describes a problem which occurs over and over again in our environment and then describes the core of the solution to the problem, in such a way that you can use this solution a million times over, without doing it the same way twice.” Christopher Alexander,

Page 3: What Are Design Patterns and Why Do I Need Them? Software professionals may be familiar with the term "Design Patterns," but many have no idea of where.

• Shortly after, software professionals began to incorporate Alexander’s principle into the creation of early design pattern documentation as a guide to novice developers.

• Design patterns gained popularity in computer science after a certain book was published in 1994 by four authors, commonly know as Gang of Four or GOF

Page 4: What Are Design Patterns and Why Do I Need Them? Software professionals may be familiar with the term "Design Patterns," but many have no idea of where.

Gang of Four The GoF Book

• Gang of Four (GoF) Gamma, Helm, Johnson, Vlissides, - founders of movement.• Gamma et al, Design Patterns: Elements of

Reusable Object-Oriented Software, Addison Wesley, 1995.

• They offer advice and help for developing quality software

• An object-oriented design pattern systematically names, explains and evaluates an important and recurring design in object-oriented systems

Page 5: What Are Design Patterns and Why Do I Need Them? Software professionals may be familiar with the term "Design Patterns," but many have no idea of where.

Design Patterns Template

Term Description

Pattern Name Describes the essence of the pattern in a short, but expressive, name

Intent Describes what the pattern does

Also Known As List any synonyms for the pattern

Motivation Provides an example of a problem and how the pattern solves that problem

Applicability Lists the situations where the pattern is applicable

Structure Set of diagrams of the classes and objects that depict the pattern

Participants Describes the classes and objects that participate in the design pattern and their responsibilities

Collaborations Describes how the participants collaborate to carry out their responsibilities

Consequences Describes the forces that exist with the pattern and the benefits, trade-offs, and the variable that is isolated by the pattern

Page 6: What Are Design Patterns and Why Do I Need Them? Software professionals may be familiar with the term "Design Patterns," but many have no idea of where.

Name meaningful text that reflects the problem, e.g. Bridge, Mediator, Flyweight

Problem addressed intent of the pattern, objectives achieved within certain constraints

Context circumstances under which it can occur

Forces constraints or issues that solution must address, forces may conflict!

Solution the static and dynamic relationships among the pattern components. Structure, participants, collaboration. Solution must resolve all forces!

• Different authors use different templates to describe their patterns

• Information is not always presented in the same way.

•Consult your manual/source !!!

Page 7: What Are Design Patterns and Why Do I Need Them? Software professionals may be familiar with the term "Design Patterns," but many have no idea of where.

Types of Pattern

• There are 3 types of pattern …– Creational: Concerned with the process of

object creation – Structural: Concerned with the composition

of classes and objects– Behavioral: Concerned with the ways in

which classes and objects interact and distribute the work

Page 8: What Are Design Patterns and Why Do I Need Them? Software professionals may be familiar with the term "Design Patterns," but many have no idea of where.

Chain of ResponsibilityCommandInterpreterIteratorMediatorMementoObserverStateStrategyTemplate MethodVisitor

Adapter

Bridge

Composite

Decorator

Façade

Flyweight

Proxy

Abstract Factory

Builder

Factory

Prototype

Singleton

Creational Structural Behavioural

Types of Design Patterns

Page 9: What Are Design Patterns and Why Do I Need Them? Software professionals may be familiar with the term "Design Patterns," but many have no idea of where.

Memento

IntentWithout violating encapsulation, capture and externalize an object’s internal state so that the object can be restored to this state later.

Applicability – use Memento when:

A snapshot of (some portion of) an object’s state must be saved so that it can be restored to that state later, and

A direct interface to obtaining the state would expose implementation details and break the object’s encapsulation.

Page 10: What Are Design Patterns and Why Do I Need Them? Software professionals may be familiar with the term "Design Patterns," but many have no idea of where.

Memento

Page 11: What Are Design Patterns and Why Do I Need Them? Software professionals may be familiar with the term "Design Patterns," but many have no idea of where.

Memento

RectCaretakerMemento

x,y

Memento save()void restore(Memento)

Rect

x,y

Memento save()void restore(Memento)

Page 12: What Are Design Patterns and Why Do I Need Them? Software professionals may be familiar with the term "Design Patterns," but many have no idea of where.

Proxy

IntentProvide a surrogate or placeholder for another object to control access to it.

ApplicabilityYou want a normally inaccessible or unavailable object to appear like an ordinary object. For example, the object might be: in a different process in a disk archive compressed not computed yet

ForcesTurning an object into a Proxy, or vice versa, should be transparent

Page 13: What Are Design Patterns and Why Do I Need Them? Software professionals may be familiar with the term "Design Patterns," but many have no idea of where.

Proxy

Page 14: What Are Design Patterns and Why Do I Need Them? Software professionals may be familiar with the term "Design Patterns," but many have no idea of where.

Additional Behaviour Leads to Different Types of Proxies

A remote proxy provides a local representative for an object in a different address space.

A virtual proxy creates expensive objects on demand. (lazy loading)

A protection proxy controls access to the original object Protection proxies are useful when objects should have different access rights.

A cache proxy caches the output of the Subject class. A Proxy can cache stable information about the subject to postpone accessing it.

Some Proxy Flavours!

Page 15: What Are Design Patterns and Why Do I Need Them? Software professionals may be familiar with the term "Design Patterns," but many have no idea of where.

Cached Proxy

Client

CalculatorBase

Calculate()

NumberMultiplier

Calculate()

NumberMultiplierProxy

Calculate()

Page 16: What Are Design Patterns and Why Do I Need Them? Software professionals may be familiar with the term "Design Patterns," but many have no idea of where.

Virtual Proxy

Client

Imager

GetImage()ImageName

QuickImage

GetImage()ImageName

ImageProxy

GetImage()ImageName

FinalImage

GetImage()ImageName

Page 17: What Are Design Patterns and Why Do I Need Them? Software professionals may be familiar with the term "Design Patterns," but many have no idea of where.

Participants of Proxy pattern

• Proxy• Maintains a reference that lets the proxy access

the real object• Provides an interface identical to the Subject’s

so that a proxy can be substituted for the real subject.

• Controls access to the real subject and may be responsible for creating and deleting it.

• Other responsibilities depend on the kind of proxy

Page 18: What Are Design Patterns and Why Do I Need Them? Software professionals may be familiar with the term "Design Patterns," but many have no idea of where.

State• Intent

– Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.

• Applicability – use in either of the following cases:

– An object’s behavior depends on its state, and it must change its behavior at run-time depending on that state.

– Operations have large, multipart conditional statements which depends on the object’s state. This state is usually represented by one or more enumerated constants. Often, several operations will contain this same conditional structure. The State pattern puts each branch of the conditional in a separate class. This lets you treat the object’s state as an object in its own right that can vary independently from other objects.

Page 19: What Are Design Patterns and Why Do I Need Them? Software professionals may be familiar with the term "Design Patterns," but many have no idea of where.

State Design Pattern

Page 20: What Are Design Patterns and Why Do I Need Them? Software professionals may be familiar with the term "Design Patterns," but many have no idea of where.

State

Invoice

IStatusIssue()Pay()

Cancel()

PendingStateIssue()Pay()

Cancel()

PaidStateIssue()Pay()

Cancel()

CancelledStateIssue()Pay()

Cancel()

Page 21: What Are Design Patterns and Why Do I Need Them? Software professionals may be familiar with the term "Design Patterns," but many have no idea of where.

Summary

• Design patterns assist developers by:

• Providing templates for common problems

• Creating a common nomenclature

• Creating components that are more reusable and flexible

• The .NET Framework includes features that makes some of the patterns easier to implement.

Page 22: What Are Design Patterns and Why Do I Need Them? Software professionals may be familiar with the term "Design Patterns," but many have no idea of where.

For more Information

• Websites• Microsoft patterns & practices• http://ww.microsoft.com/patterns

• http://www.dofactory.com/Patterns/Patterns.aspx

• Books• Design patterns: Elements of Reusable Object Oriented

Software (Addison-Wesley, 1995)

• Head First Design Patterns - O’Reilly Media


Recommended