![Page 1: Power of Patterns OR More Than Programming with Objects](https://reader036.vdocuments.us/reader036/viewer/2022070317/5563296fd8b42a61348b4e32/html5/thumbnails/1.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022070317/5563296fd8b42a61348b4e32/html5/thumbnails/2.jpg)
Design Patterns Defined
• Alexander’s Architecture Design Patterns
• Published in 1977
![Page 3: Power of Patterns OR More Than Programming with Objects](https://reader036.vdocuments.us/reader036/viewer/2022070317/5563296fd8b42a61348b4e32/html5/thumbnails/3.jpg)
“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](https://reader036.vdocuments.us/reader036/viewer/2022070317/5563296fd8b42a61348b4e32/html5/thumbnails/4.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022070317/5563296fd8b42a61348b4e32/html5/thumbnails/5.jpg)
WHY SHOULD I CARE ABOUT PATTERNS?
The question of the day!
![Page 6: Power of Patterns OR More Than Programming with Objects](https://reader036.vdocuments.us/reader036/viewer/2022070317/5563296fd8b42a61348b4e32/html5/thumbnails/6.jpg)
Guitar
Different implementations, but all recognized as “a guitar”
![Page 7: Power of Patterns OR More Than Programming with Objects](https://reader036.vdocuments.us/reader036/viewer/2022070317/5563296fd8b42a61348b4e32/html5/thumbnails/7.jpg)
Kitchen
• Preparing/Cooking food• Store cooking tools• Stove• Refrigerator• Sink• Counter space• Dishwasher
![Page 8: Power of Patterns OR More Than Programming with Objects](https://reader036.vdocuments.us/reader036/viewer/2022070317/5563296fd8b42a61348b4e32/html5/thumbnails/8.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022070317/5563296fd8b42a61348b4e32/html5/thumbnails/9.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022070317/5563296fd8b42a61348b4e32/html5/thumbnails/10.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022070317/5563296fd8b42a61348b4e32/html5/thumbnails/11.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022070317/5563296fd8b42a61348b4e32/html5/thumbnails/12.jpg)
GoF Classification
![Page 13: Power of Patterns OR More Than Programming with Objects](https://reader036.vdocuments.us/reader036/viewer/2022070317/5563296fd8b42a61348b4e32/html5/thumbnails/13.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022070317/5563296fd8b42a61348b4e32/html5/thumbnails/14.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022070317/5563296fd8b42a61348b4e32/html5/thumbnails/15.jpg)
![Page 16: Power of Patterns OR More Than Programming with Objects](https://reader036.vdocuments.us/reader036/viewer/2022070317/5563296fd8b42a61348b4e32/html5/thumbnails/16.jpg)
![Page 17: Power of Patterns OR More Than Programming with Objects](https://reader036.vdocuments.us/reader036/viewer/2022070317/5563296fd8b42a61348b4e32/html5/thumbnails/17.jpg)
Patterns are not created…
They arediscovered!
![Page 18: Power of Patterns OR More Than Programming with Objects](https://reader036.vdocuments.us/reader036/viewer/2022070317/5563296fd8b42a61348b4e32/html5/thumbnails/18.jpg)
LET’S DISCOVER A PATTERN!Object oriented principles sneak in
![Page 19: Power of Patterns OR More Than Programming with Objects](https://reader036.vdocuments.us/reader036/viewer/2022070317/5563296fd8b42a61348b4e32/html5/thumbnails/19.jpg)
Duck Simulator
![Page 20: Power of Patterns OR More Than Programming with Objects](https://reader036.vdocuments.us/reader036/viewer/2022070317/5563296fd8b42a61348b4e32/html5/thumbnails/20.jpg)
Adding “fly()”
![Page 21: Power of Patterns OR More Than Programming with Objects](https://reader036.vdocuments.us/reader036/viewer/2022070317/5563296fd8b42a61348b4e32/html5/thumbnails/21.jpg)
But RubberDuck now flies…
![Page 22: Power of Patterns OR More Than Programming with Objects](https://reader036.vdocuments.us/reader036/viewer/2022070317/5563296fd8b42a61348b4e32/html5/thumbnails/22.jpg)
Override fly?
One possible solution
But what happens when we have this?
![Page 23: Power of Patterns OR More Than Programming with Objects](https://reader036.vdocuments.us/reader036/viewer/2022070317/5563296fd8b42a61348b4e32/html5/thumbnails/23.jpg)
Interfaces?
![Page 24: Power of Patterns OR More Than Programming with Objects](https://reader036.vdocuments.us/reader036/viewer/2022070317/5563296fd8b42a61348b4e32/html5/thumbnails/24.jpg)
Encapsulate what varies
Important OOP Principle!
![Page 25: Power of Patterns OR More Than Programming with Objects](https://reader036.vdocuments.us/reader036/viewer/2022070317/5563296fd8b42a61348b4e32/html5/thumbnails/25.jpg)
Behaviors separated
![Page 26: Power of Patterns OR More Than Programming with Objects](https://reader036.vdocuments.us/reader036/viewer/2022070317/5563296fd8b42a61348b4e32/html5/thumbnails/26.jpg)
The New Duck
Important OOP Principle!
Program to an interface, not an implementation
![Page 27: Power of Patterns OR More Than Programming with Objects](https://reader036.vdocuments.us/reader036/viewer/2022070317/5563296fd8b42a61348b4e32/html5/thumbnails/27.jpg)
Delegate the behavior
![Page 28: Power of Patterns OR More Than Programming with Objects](https://reader036.vdocuments.us/reader036/viewer/2022070317/5563296fd8b42a61348b4e32/html5/thumbnails/28.jpg)
Implementation specifies behavior
![Page 29: Power of Patterns OR More Than Programming with Objects](https://reader036.vdocuments.us/reader036/viewer/2022070317/5563296fd8b42a61348b4e32/html5/thumbnails/29.jpg)
The Strategy Pattern
![Page 30: Power of Patterns OR More Than Programming with Objects](https://reader036.vdocuments.us/reader036/viewer/2022070317/5563296fd8b42a61348b4e32/html5/thumbnails/30.jpg)
Patterns are not created…
They arediscovered!
![Page 31: Power of Patterns OR More Than Programming with Objects](https://reader036.vdocuments.us/reader036/viewer/2022070317/5563296fd8b42a61348b4e32/html5/thumbnails/31.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022070317/5563296fd8b42a61348b4e32/html5/thumbnails/32.jpg)
Beyond
• Architectural• Application• Domain-Specific• Business Process• Organizational• User Interface Design
![Page 33: Power of Patterns OR More Than Programming with Objects](https://reader036.vdocuments.us/reader036/viewer/2022070317/5563296fd8b42a61348b4e32/html5/thumbnails/33.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022070317/5563296fd8b42a61348b4e32/html5/thumbnails/34.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022070317/5563296fd8b42a61348b4e32/html5/thumbnails/35.jpg)
![Page 36: Power of Patterns OR More Than Programming with Objects](https://reader036.vdocuments.us/reader036/viewer/2022070317/5563296fd8b42a61348b4e32/html5/thumbnails/36.jpg)
![Page 37: Power of Patterns OR More Than Programming with Objects](https://reader036.vdocuments.us/reader036/viewer/2022070317/5563296fd8b42a61348b4e32/html5/thumbnails/37.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022070317/5563296fd8b42a61348b4e32/html5/thumbnails/38.jpg)
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.
![Page 39: Power of Patterns OR More Than Programming with Objects](https://reader036.vdocuments.us/reader036/viewer/2022070317/5563296fd8b42a61348b4e32/html5/thumbnails/39.jpg)
My Contact Info
• @mdclement• [email protected]• http://blog.softwareontheside.com• Utah Software Craftsmanship Group– https://groups.google.com/forum/#!
forum/ut-software-craftsmanship– @utahsc