30

Women Who Code Belfast: Introduction to Design patterns

Embed Size (px)

DESCRIPTION

A brief overview of common design patterns including code samples as presented to Women Who Code Belfast on 26th Nov 2013

Citation preview

Page 1: Women Who Code Belfast: Introduction to Design patterns
Page 2: Women Who Code Belfast: Introduction to Design patterns

Colleen Crangle

• Woman in tech throughout the 70s and 80s• PhD in Philosophy (Logic and the Philosophy of

Language and Science), in follow-up to a BSc cum laude and an MSc in Computer Science and Mathematics from South Africa• Affiliated scholar with the Centre for the Study

of Language and Information at Stanford University and a partner in a small R&D business in Silicon Valley, California

Page 3: Women Who Code Belfast: Introduction to Design patterns

Introduction to Design Patterns

Jackie Pollock

Page 4: Women Who Code Belfast: Introduction to Design patterns

• Technical Consultant @ Kainos Software

• Joined June 2006 as Graduate Software Engineer

• Specialities:

• Document Management

• Workflow

• Scanning

• Integration

Who am I?

Page 5: Women Who Code Belfast: Introduction to Design patterns

• What are design patterns?

• Why are they useful

• Types of design pattern

• Creational

• Structural

• Behavioural

Overview

Page 6: Women Who Code Belfast: Introduction to Design patterns

• General repeatable solution to commonly occurring problem

• Not a finished design to be transformed directly into code

• Relationships and interactions between classes or objects without implementation specifics

What is a design pattern?

Page 7: Women Who Code Belfast: Introduction to Design patterns

• Architectural concept by Christopher Alexander in 1977

• Ward Cunningham and Kent Beck 1987 “Using pattern languages for object-orientated programmers”

• 1990s Gamma, Helm, Johnson, and Vlissides. AKA: Gang of Four (GoF) book “Design Patterns: Elements of Reusable Object-Oriented Software”

Where did they come from?

Page 8: Women Who Code Belfast: Introduction to Design patterns

• Design reuse

• Uniform design vocabulary

• Enhance understanding, restructuring, & team communication

• Basis for automation

• Transcends language-centric biases

• Abstracts away from many unimportant details

Benefits

Page 9: Women Who Code Belfast: Introduction to Design patterns

• Require significant tedious & error-prone human effort to handcraft pattern implementations / design reuse

• Can be deceptively simple

• May limit design options

• Leaves some important details unresolved

Drawbacks

Page 10: Women Who Code Belfast: Introduction to Design patterns

Creational Patterns

Page 11: Women Who Code Belfast: Introduction to Design patterns

Factory

Creating objects without having to specify the exact class of object that will be created – will be determined at runtime

Page 12: Women Who Code Belfast: Introduction to Design patterns

Abstract Factory

Creates family of objects at runtime – each of which will likely be a Factory in its own right

Page 13: Women Who Code Belfast: Introduction to Design patterns

Separates construction of the various elements from the representation of the whole therefore same process can create different variants of theme

Builder

Page 14: Women Who Code Belfast: Introduction to Design patterns

Only one instance of object across application. Used to provide single point of access. Should not be used to replace global variables

Singleton

Page 15: Women Who Code Belfast: Introduction to Design patterns

Create complex objects by cloning existing ones and changing required characteristics. Useful when cost of new objects is prohibitive

Prototype

Page 16: Women Who Code Belfast: Introduction to Design patterns

Structural Patterns

Page 17: Women Who Code Belfast: Introduction to Design patterns

Converts an existing interface into another that your application can use.

Adapter

Page 18: Women Who Code Belfast: Introduction to Design patterns

Treat a group of objects the same way as a single instance of object. Tree structures with a has-a relationship

Composite

Page 19: Women Who Code Belfast: Introduction to Design patterns

Add behaviour to object without affecting behaviour of other objects from the same class

Decorator

Page 20: Women Who Code Belfast: Introduction to Design patterns

Simplified interface to larger body of code. Reduces dependencies on inner class objects, wraps complex product APIs in manageable layer

Facade

Page 21: Women Who Code Belfast: Introduction to Design patterns

Placeholder for another object, controlling access. Used for lazy loading resource hungry objects

Proxy

Page 22: Women Who Code Belfast: Introduction to Design patterns

Behavioural Patterns

Page 23: Women Who Code Belfast: Introduction to Design patterns

Turns requests into objects, separating out the code making the request from the object performing it

Command

Page 24: Women Who Code Belfast: Introduction to Design patterns

Access elements of collection sequentially without focusing on underlying object types

Iterator

Page 25: Women Who Code Belfast: Introduction to Design patterns

When one object changes state, all dependent objects notified and updated. View element of Model-View-Controller pattern

Observer

Page 26: Women Who Code Belfast: Introduction to Design patterns

Object alters behaviour when internal state changes

State

Page 27: Women Who Code Belfast: Introduction to Design patterns

Word of Caution

Always go for simplistic approach rather than applying patterns to every situation

If you force-fit a pattern into a situation where the pattern doesn’t apply, you are, by definition, not following the pattern

Page 28: Women Who Code Belfast: Introduction to Design patterns

• Books

• Design Patterns

• Head First Design Patterns

• Design Patterns for Dummies

• Online

• Slideshare

• Pluralsight

• SourceMaking

• Wikipedia

• Image Credits

• Nelleke Verhoeff (Yepr)

Resources

Page 29: Women Who Code Belfast: Introduction to Design patterns

Thanks for listening

Any Questions?

[email protected]

@kievia

jackiepollock

Page 30: Women Who Code Belfast: Introduction to Design patterns

Finish

• Any queries?• [email protected]

• Twitter• #WomenWhoCodeBelfast• @WWCBelfast, @nirushika

Looking forward to seeing you at our next event!