power of patterns or more than programming with objects

39
Power of Patterns OR More Than Programming with Objects Intro to Design Patterns Mike Clement @mdclement [email protected] http://blog.softwareontheside.com Utah Code Camp Fall 2011

Upload: michael-clement

Post on 25-May-2015

1.319 views

Category:

Technology


1 download

DESCRIPTION

An Introduction to Design Patterns. What is a design pattern? Why should you care? What it the power of design patterns? How do design patterns tie into object oriented programming? If I'm using objects in my code, isn't that object oriented programming? (The answer is not necessarily!) We'll talk about why you should know common design patterns, why they are powerful and how to make sure you're leveraging object oriented programming principles, not just "programming with objects".

TRANSCRIPT

Page 1: Power of Patterns OR More Than Programming with Objects

Power of PatternsOR

More Than Programming with Objects

Intro to Design PatternsMike Clement@mdclement

[email protected]://blog.softwareontheside.com

Utah Code Camp Fall 2011

Page 2: Power of Patterns OR More Than Programming with Objects

Design Patterns Defined

• Alexander’s Architecture Design Patterns

• Published in 1977

Page 3: Power of Patterns OR More Than Programming with Objects

“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 ever doing it the same way twice.”

-Christopher Alexander

Page 4: Power of Patterns OR More Than Programming with Objects

A Pattern is a solution to a problem in a context

• Context – recurring situation• Problem – goal and constraints• Solution – general design to resolve the

problemIf it only happened once,

it’s not a pattern

Page 5: Power of Patterns OR More Than Programming with Objects

WHY SHOULD I CARE ABOUT PATTERNS?

The question of the day!

Page 6: Power of Patterns OR More Than Programming with Objects

Guitar

Different implementations, but all recognized as “a guitar”

Page 7: Power of Patterns OR More Than Programming with Objects

Kitchen

• Preparing/Cooking food• Store cooking tools• Stove• Refrigerator• Sink• Counter space• Dishwasher

Page 8: Power of Patterns OR More Than Programming with Objects

Power of a Pattern Language

• A shared vocabulary• Powerful• Say more with less• Stay “in the design” longer• Turbo charge the team• Gets new hires up to speed

Page 9: Power of Patterns OR More Than Programming with Objects

From Architecture to Software

• 1987 – Kent Beck and Ward Cunningham presented at OOPSLA

• 1994 – GoF book published

Page 10: Power of Patterns OR More Than Programming with Objects

GOF Pattern Template

• Pattern Name• Classification• Intent• Also Known As• Motivation• Applicability• Structure

• Participants• Collaborations• Consequences• Implementation• Sample Code• Known Uses• Related Patterns

Page 11: Power of Patterns OR More Than Programming with Objects

GoF Pattern CatalogCreational Patterns• Abstract Factory Creates an instance of several families of

classes• Builder Separates object construction from its

representation• Factory Method Creates an instance of several derived

classes• Prototype A fully initialized instance to be copied or

cloned• Singleton A class of which only a single instance can

exist

Structural Patterns• Adapter Match interfaces of different classes• Bridge Separates an object’s interface from its

implementation• Composite A tree structure of simple and composite

objects• Decorator Add responsibilities to objects dynamically• Facade A single class that represents an entire

subsystem• Flyweight A fine-grained instance used for efficient

sharing• Proxy An object representing another object

Behavioral Patterns• Chain of Resp. A way of passing a request between a

chain of objects• Command Encapsulate a command request as an object• Interpreter A way to include language elements in a

program• Iterator Sequentially access the elements of a

collection• Mediator Defines simplified communication between

classes• Memento Capture and restore an object's internal state• Observer A way of notifying change to a number of

classes• State Alter an object's behavior when its state changes• Strategy Encapsulates an algorithm inside a class• Template Method Defer the exact steps of an

algorithm to a subclass• Visitor Defines a new operation to a class without

change

Page 12: Power of Patterns OR More Than Programming with Objects

GoF Classification

Page 13: Power of Patterns OR More Than Programming with Objects

We need some code that…

Defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically

Page 14: Power of Patterns OR More Than Programming with Objects

Observer Pattern

• Intent: Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically

• Also Known As: Publish-Subscribe

Page 15: Power of Patterns OR More Than Programming with Objects
Page 16: Power of Patterns OR More Than Programming with Objects
Page 17: Power of Patterns OR More Than Programming with Objects

Patterns are not created…

They arediscovered!

Page 18: Power of Patterns OR More Than Programming with Objects

LET’S DISCOVER A PATTERN!Object oriented principles sneak in

Page 19: Power of Patterns OR More Than Programming with Objects

Duck Simulator

Page 20: Power of Patterns OR More Than Programming with Objects

Adding “fly()”

Page 21: Power of Patterns OR More Than Programming with Objects

But RubberDuck now flies…

Page 22: Power of Patterns OR More Than Programming with Objects

Override fly?

One possible solution

But what happens when we have this?

Page 23: Power of Patterns OR More Than Programming with Objects

Interfaces?

Page 24: Power of Patterns OR More Than Programming with Objects

Encapsulate what varies

Important OOP Principle!

Page 25: Power of Patterns OR More Than Programming with Objects

Behaviors separated

Page 26: Power of Patterns OR More Than Programming with Objects

The New Duck

Important OOP Principle!

Program to an interface, not an implementation

Page 27: Power of Patterns OR More Than Programming with Objects

Delegate the behavior

Page 28: Power of Patterns OR More Than Programming with Objects

Implementation specifies behavior

Page 29: Power of Patterns OR More Than Programming with Objects

The Strategy Pattern

Page 30: Power of Patterns OR More Than Programming with Objects

Patterns are not created…

They arediscovered!

Page 31: Power of Patterns OR More Than Programming with Objects

OO Principles

• Encapsulate what varies.• Favor composition over inheritance.• Program to interfaces, not implementations.• Strive for loosely coupled designs between objects that

interact.• Open for extension but closed for modification.• Depend on abstractions.• Only talk to your friends.• Don't call us, we'll call you.• A class should have only one reason to change.

Page 32: Power of Patterns OR More Than Programming with Objects

Beyond

• Architectural• Application• Domain-Specific• Business Process• Organizational• User Interface Design

Page 33: Power of Patterns OR More Than Programming with Objects

Anti-Patterns

• Why the bad solution is unattractive• Why in the long term it’s bad• Suggests other patterns for a good solution

Tells you how to go from a problem to a BAD solution.

Page 34: Power of Patterns OR More Than Programming with Objects

Catalog…

• Big Ball of Mud• Gold Plating• Interface Bloat• God Object• Coding by Exception• Copy and Paste• Golden Hammer• Cargo Cult

• Analysis Paralysis• Design by Committee• Vendor Lock-in• Groupthink• Mushroom

Management

Page 35: Power of Patterns OR More Than Programming with Objects
Page 36: Power of Patterns OR More Than Programming with Objects
Page 37: Power of Patterns OR More Than Programming with Objects

Share the Vocabulary

1) In design meetings2) With other developers3) In architecture documentation4) In code comments and naming conventions5) To groups of interested developers

Page 38: Power of Patterns OR More Than Programming with Objects

Learning more…I really like this one… some people find it annoying. Puts Design Patterns in the context of OOP.

Great reference. Definitive resource. Put me to sleep the first couple times I tried to read it though.