Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets...

126
Effective GoF Patterns with C++11 and Boost Tobias Darm Effective GoF Patterns - Tobias Darm

Transcript of Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets...

Page 1: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Effective GoF Patterns

with C++11 and Boost

Tobias Darm

Effective GoF Patterns - Tobias Darm

Page 2: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

me

• no books

• no blogs

• no tweets

• no open source

Effective GoF Patterns - Tobias Darm

Page 3: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

me

• Dräger Lübeck

• C++ developer

• Subsystemdesigner

• Trainings/Workshops

Effective GoF Patterns - Tobias Darm

Page 4: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Motivation

Effective GoF Patterns - Tobias Darm

Page 5: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

My Approach

Effective GoF Patterns - Tobias Darm

Page 6: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Unfair

Effective GoF Patterns - Tobias Darm

Page 7: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Content

• Implementation classic vs. C++11

• Implementation classic vs. Boost

• Application classic vs. C++11/Boost

• Conclusion concerning patterns

Effective GoF Patterns - Tobias Darm

Page 8: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

CoffeeMachine (classic)

Application

Effective GoF Patterns - Tobias Darm

Page 9: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

CoffeeMachine (C++11/Boost)

Application

Effective GoF Patterns - Tobias Darm

Page 10: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

GoF-Patterns

• Common solution to a reoccurring problem

• GoF-Patterns = Micropatterns

• How many?

Effective GoF Patterns - Tobias Darm

Page 11: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

GoF-Patterns

• Creational– Abstract Factory

– Builder

– Factory Method

– Prototype

– (Singleton)

• Structural– Adapter

– Bridge

– Composite

– Decorator

– Facade

– Flyweight

– Proxy

• Behavioural– Chain

– Command

– Interpreter

– Iterator

– Mediator

– Memento

– Observer

– State

– Strategy

– Template

– Visitor

Effective GoF Patterns - Tobias Darm

Page 12: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

GoF-Patterns

• Creational

– Abstract Factory

• Structural

– Flyweight

• Behavioural

– Chain

– Command

– Observer

– Strategy

– State

Effective GoF Patterns - Tobias Darm

Page 13: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Strategy

• Capsules a family of algorithms and makes

them exchangeable

Effective GoF Patterns - Tobias Darm

Page 14: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Strategy

Example: CaffeineBeverage

• 1

• 2

• 3

Effective GoF Patterns - Tobias Darm

Page 15: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Strategy (classic)

Recipe Interface

Effective GoF Patterns - Tobias Darm

Page 16: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Strategy (classic)

Concrete Recipes

Effective GoF Patterns - Tobias Darm

Page 17: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Strategy (classic)

Application

Effective GoF Patterns - Tobias Darm

Page 18: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Strategy (C++11)

CaffeineBeverage/Recipes

Effective GoF Patterns - Tobias Darm

Page 19: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Strategy (C++11)

Application with bind

Effective GoF Patterns - Tobias Darm

Page 20: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Strategy (C++11)

Application with lambda

Effective GoF Patterns - Tobias Darm

Page 21: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Strategy

classic vs. C++11

• Classic

• C++11

Effective GoF Patterns - Tobias Darm

Page 22: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Chain

• Avoid coupling the sender of a request to its

receiver by giving more than one object a

chance to handle the request

Effective GoF Patterns - Tobias Darm

Page 23: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Chain

Example: Condiments

Effective GoF Patterns - Tobias Darm

Page 24: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Chain (classic)

Condiment

Effective GoF Patterns - Tobias Darm

Page 25: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Chain (classic)

Sugar and Milk

Effective GoF Patterns - Tobias Darm

Page 26: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Chain (classic)

Application

Effective GoF Patterns - Tobias Darm

Page 27: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Chain (C++11)

Condiment - Sugar and Milk

Effective GoF Patterns - Tobias Darm

Page 28: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Chain (C++11)

accu with function

Effective GoF Patterns - Tobias Darm

Page 29: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Chain (C++11)

Application with bind

Effective GoF Patterns - Tobias Darm

Page 30: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Chain (C++11)

accu for lambdas

Effective GoF Patterns - Tobias Darm

Page 31: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Chain (C++11)

Application with lambda

Effective GoF Patterns - Tobias Darm

Page 32: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Chain

classic vs. C++11

• classic

• C++11

Effective GoF Patterns - Tobias Darm

Page 33: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Command

• Encapsulate a request as an object

Effective GoF Patterns - Tobias Darm

Page 34: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Command

Example: CoffeeMachine

Orders:

1.Coffee

2.Coffee

3.Tea

Effective GoF Patterns - Tobias Darm

Page 35: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Command (classic)

MakeCaffeineBeverage

Effective GoF Patterns - Tobias Darm

Page 36: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Command (classic)

CoffeeMachine

Effective GoF Patterns - Tobias Darm

Page 37: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Command (classic)

Application

Effective GoF Patterns - Tobias Darm

Page 38: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Command (C++11)

CoffeeMachine

Effective GoF Patterns - Tobias Darm

Page 39: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Command (C++11)

Application

Effective GoF Patterns - Tobias Darm

Page 40: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Command

classic vs. C++11

• classic

• C++11

Effective GoF Patterns - Tobias Darm

Page 41: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Benefits

Effective GoF Patterns - Tobias Darm

Page 42: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Benefits are maintained

• Loose coupling

• Extendable

• Unit testable

Effective GoF Patterns - Tobias Darm

Page 43: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Some More Benefits

• Less code

• Less coupling

• Easier to extend

Effective GoF Patterns - Tobias Darm

Page 44: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Criticism

Effective GoF Patterns - Tobias Darm

Page 45: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Criticism

Debugging

• Do not debug Library code

Effective GoF Patterns - Tobias Darm

Page 46: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Criticism

Too Loose

Effective GoF Patterns - Tobias Darm

Page 47: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Criticism

Too Loose

Effective GoF Patterns - Tobias Darm

Page 48: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Criticism

Performance

• http://probablydance.com/2013/01/13/a-faster-implementation-of-stdfunction/

Compiler Virtual [ms] Function [ms] MalteSkarupke[ms]

VS 2012 Debug 1917 4195 3262

VS 2012 Release 800 770 630

Clang 3.1 –O0 –g3 1864 3161 2513

Clang 3.1 –O3 564 474 456

GCC 4.7.2 –O0 –g3 1755 3363 2587

GCC 4.7.2 –O3 555 466 431

Effective GoF Patterns - Tobias Darm

Page 49: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Criticism

Size

• http://dl.dropbox.com/u/27990997/compare_functions.cpp

Compiler Virtual [bytes] Function [bytes]

Clang 3.3 –O0 –g3 58608 110808

Clang 3.3 –O3 24044 35096

Effective GoF Patterns - Tobias Darm

Page 50: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Criticism

Ownership

Effective GoF Patterns - Tobias Darm

Page 51: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Criticism

Ownership

Effective GoF Patterns - Tobias Darm

Page 52: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Criticism

Transfer of Ownership

• http://isocpp.org/files/papers/n3610.html

Effective GoF Patterns - Tobias Darm

Page 53: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Patterns and Boost

Effective GoF Patterns - Tobias Darm

Page 54: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Observer

• Define a one-to-many dependency between

objects so that when one object changes

state, all its dependents are notified and

updated automatically.

Effective GoF Patterns - Tobias Darm

Page 55: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Observer (classic)

Example: Finished!

Effective GoF Patterns - Tobias Darm

Page 56: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Observer (classic)

CoffeeMachineObserver

Effective GoF Patterns - Tobias Darm

Page 57: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Observer (classic)

CoffeeMachine

Effective GoF Patterns - Tobias Darm

Page 58: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Observer (classic)

Application

Effective GoF Patterns - Tobias Darm

Page 59: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Observer

Boost.Signals2

• Managed signals/slots system

• Controlling order of callbacks

• Connection tracking

Effective GoF Patterns - Tobias Darm

Page 60: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Signals Introduction

Slots

Effective GoF Patterns - Tobias Darm

Page 61: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Signals Introduction

Connect

Effective GoF Patterns - Tobias Darm

Page 62: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Signals Introduction

Order

Effective GoF Patterns - Tobias Darm

Page 63: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Signals Introduction

Tracking

Effective GoF Patterns - Tobias Darm

Page 64: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

More about

Boost.Signals2

• Explicit connection management

• Combining multiple return values

• Thread-Safe

• Header-Only

Effective GoF Patterns - Tobias Darm

Page 65: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Observer (Signals)

CoffeeMachine

Effective GoF Patterns - Tobias Darm

Page 66: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Observer (Signals)

Application

Effective GoF Patterns - Tobias Darm

Page 67: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Observer

classic vs. Signals

• classic

• Boost

Effective GoF Patterns - Tobias Darm

Page 68: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Factory

• Provide an interface for creating families of

related or dependent objects without

specifying their concrete classes

Effective GoF Patterns - Tobias Darm

Page 69: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Factory (classic)

Example: BeverageFactory

Effective GoF Patterns - Tobias Darm

Page 70: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Factory (classic)

CaffeineBeverageFactory

Effective GoF Patterns - Tobias Darm

Page 71: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Factory (classic)

BeverageFactory

Effective GoF Patterns - Tobias Darm

Page 72: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Factory (classic)

Application

Effective GoF Patterns - Tobias Darm

Page 73: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Factory

Boost.Functional.Factory

• Lets you encapsulate a new expression as a

function object

Effective GoF Patterns - Tobias Darm

Page 74: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Introduction

Functional.Factory

Effective GoF Patterns - Tobias Darm

Page 75: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Introduction

Functional.Factory

• Forwarding arguments to the constructor

• Member functions to create different kinds of

objects

• Factory base class might not be necessary

• Allows use of customized memory

management

Effective GoF Patterns - Tobias Darm

Page 76: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Factory (Boost)

BeverageFactory

Effective GoF Patterns - Tobias Darm

Page 77: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Factory (Lambda)

BeverageFactory

Effective GoF Patterns - Tobias Darm

Page 78: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Factory (Boost/Lambda)

Application

Effective GoF Patterns - Tobias Darm

Page 79: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Factory

classic vs. Boost.Functional.Factory

• Classic

• Boost

Effective GoF Patterns - Tobias Darm

Page 80: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Library

Benefits

• Has been done for you

• Just using the library

• Many shortcomings addressed

Effective GoF Patterns - Tobias Darm

Page 81: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Library

Benefits

• Development speed up

• GoF-Patterns focused on OO-paradigm

• Use of generic prgramming model

Effective GoF Patterns - Tobias Darm

Page 82: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Boost and Patterns

Function

Bind

Phoenix

Lambda

Signals Statechart

MSM

Flyweight

Chain

Command

Observer

Strategy

State

Proxy

Prototype

Effective GoF Patterns - Tobias Darm

Page 83: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Boost and Patterns

Flyweight

Effective GoF Patterns - Tobias Darm

Page 84: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Boost.Flyweight

Example: Colour

Effective GoF Patterns - Tobias Darm

Page 85: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Boost.Flyweight

Boost.Hash

Effective GoF Patterns - Tobias Darm

Page 86: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Boost.Flyweight

Boost.Hash

Effective GoF Patterns - Tobias Darm

Page 87: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Boost.Flyweight

• Tagging

• Hash- or Set-based Tablelookup

• Trackingpolicy for Values

• Lockingpolicy for Factory

Effective GoF Patterns - Tobias Darm

Page 88: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Boost.MSM

State

• EDSL for transitiontable

• State, Transition, Event

• Submachines, Orthogonal Regions,

Pseudostates

• History

• Completion/Anonymous transition

• Internal transitions (Action in State)

Effective GoF Patterns - Tobias Darm

Page 89: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

MSM

Example: Ping - Pong

Effective GoF Patterns - Tobias Darm

Page 90: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

MSM

States

Effective GoF Patterns - Tobias Darm

Page 91: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

MSM

Actions

Effective GoF Patterns - Tobias Darm

Page 92: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

MSM

Transitiontable

Effective GoF Patterns - Tobias Darm

Page 93: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

MSM

Application

Effective GoF Patterns - Tobias Darm

Page 94: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Putting it all together

• Writing a Coffeemachine application

• Putting all the patterns together

Effective GoF Patterns - Tobias Darm

Page 95: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Putting it all together

classic

Effective GoF Patterns - Tobias Darm

Page 96: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Putting it all together

classic

Effective GoF Patterns - Tobias Darm

Page 97: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Putting it all together

classic

Effective GoF Patterns - Tobias Darm

Page 98: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Putting it all together

C++11

Effective GoF Patterns - Tobias Darm

Page 99: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Putting it all together

C++11

Effective GoF Patterns - Tobias Darm

Page 100: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Putting it all together

C++11

Effective GoF Patterns - Tobias Darm

Page 101: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Putting it all together (classic)

Dependencies

Effective GoF Patterns - Tobias Darm

Page 102: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Putting it all together (C++11/Boost)

Dependencies

Effective GoF Patterns - Tobias Darm

Page 103: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Putting it all together

Analysis

classic C++11

CountDeclClass 25 12

CountDeclMethodAll 168 58

CountLineCode 522 278

CountPath 74 31

MaxInheritanceTree 1 0

SumCyclomatic 74 31

Effective GoF Patterns - Tobias Darm

Page 104: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Conclusion

3 Observations

Effective GoF Patterns - Tobias Darm

Page 105: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Patterns are crutches ...

• ... for features that the language does not

have

Effective GoF Patterns - Tobias Darm

Page 106: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Peter Norvig

16 of the 23 GoF patterns are simpler or even

invisible in higher-level languages

http://norvig.com/design-patterns/ppframe.htm

Effective GoF Patterns - Tobias Darm

Page 107: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Where is the Pattern?

Strategy

Effective GoF Patterns - Tobias Darm

Page 108: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Where is the Pattern?

Chain

Effective GoF Patterns - Tobias Darm

Page 109: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Where is the Pattern?

Command

Effective GoF Patterns - Tobias Darm

Page 110: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Jeff Atwood

Design patterns are a form of complexity

http://www.codinghorror.com/blog/2007/07/rethinking-design-patterns.html

Effective GoF Patterns - Tobias Darm

Page 111: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Patterns for Communication

Effective GoF Patterns - Tobias Darm

Page 112: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Patterns for Communication

Solving Problems

Strategy

Effective GoF Patterns - Tobias Darm

Page 113: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Patterns for Communication

Solving Problems

Strategy

Effective GoF Patterns - Tobias Darm

Page 114: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Patterns for Communication

Solving Problems

• Classic

• C++11

Effective GoF Patterns - Tobias Darm

Page 115: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Patterns for Communication

Implementation is a detail

Effective GoF Patterns - Tobias Darm

Page 116: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Patterns for Communication

Flyweight

Effective GoF Patterns - Tobias Darm

Page 117: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Patterns for Communication

Chain

Effective GoF Patterns - Tobias Darm

Page 118: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Patterns for Communication

Observer

Effective GoF Patterns - Tobias Darm

Page 119: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Patterns for Communication

Command

Effective GoF Patterns - Tobias Darm

Page 120: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

It is not a requirement for a

pattern to be visible in a class

diagram

Effective GoF Patterns - Tobias Darm

Page 121: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

2nd Edition – GoF book ?

Effective GoF Patterns - Tobias Darm

Page 122: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Evolution

• Assembler � if/else, do-while, ...

• C � class, polymorphism, ...

• C++ � Strategy, Command, ...

• C++11 � ...

Effective GoF Patterns - Tobias Darm

Page 123: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Pattern Lifecycle

• Discovered

• Published

• Test of time

• Adopted by language/library

• Disapears

Effective GoF Patterns - Tobias Darm

Page 124: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

PPG

• Pattern

• Preservation

• Group

Effective GoF Patterns - Tobias Darm

Page 125: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Books

Effective GoF Patterns - Tobias Darm

Page 126: Effective GoF Patterns - ACCU · PDF fileme • no books • no blogs • no tweets • no open source Effective GoF Patterns - Tobias Darm

Thank you for your attention

Effective GoF Patterns - Tobias Darm