Power of Patterns: Refactoring to (or away from) Patterns
-
Upload
michael-clement -
Category
Technology
-
view
979 -
download
1
description
Transcript of Power of Patterns: Refactoring to (or away from) Patterns
Power of Patterns:Refactoring to (or away from)
Patterns
Mike Clement@mdclement
[email protected]://blog.softwareontheside.com
http://agilecodegames.com
Design Patterns Defined
• Alexander’s Architecture Design Patterns
• Published in 1977
“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
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
WHY SHOULD I CARE ABOUT PATTERNS?
Different implementations, but all recognized as “a guitar”
Kitchen
• Preparing/Cooking food• Stove• Refrigerator• Sink• Counter space• Storage/Cabinets/Drawers• Dishwasher
Power of a Pattern Language
• Say more with less• Better communication• Stay “in the design” longer• A shared vocabulary
From Architecture to Software
• 1987 – Kent Beck and Ward Cunningham presented at OOPSLA
• 1994 – GoF book published
GOF Pattern Template
• Pattern Name• Classification• Intent• Also Known As• Motivation• Applicability• Structure
• Participants• Collaborations• Consequences• Implementation• Sample Code• Known Uses• Related Patterns
We need some code that…
When weather/stock/employee data is updated, all the clients are notified of the change.
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
Patterns are not created…
They arediscovered!
LET’S DISCOVER A PATTERN!Object oriented principles sneak in
Duck Simulator
Adding “fly()”
But RubberDuck now flies…
Override fly?
One possible solution
But what happens when we have this?
Interfaces?
Encapsulate what varies
Important OOP Principle!
Behaviors separated
The New Duck
Important OOP Principle!
Program to an interface, not an implementation
Delegate the behavior
The Strategy Pattern
Patterns are not created…
They arediscovered!
WHEN?
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.
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
WHEN?
XP Simple Design
• Passes all tests• Clear, expressive, consistent• No duplication•Minimal
WHEN?
SO WHAT ABOUT REFACTORING?
Code Smells• Duplicated Code• Long Method• Large Class• Long Parameter List• Divergent Change• Shotgun Surgery• Feature Envy• Data Clumps• Primitive Obsession• Switch Statements• Parallel Inheritance Hierarchies• Lazy Class• Speculative Generality• Temporary Field
• Message Chains• Middle Man• Inappropriate Intimacy• Alternative Classes with Different
Interfaces• Incomplete Library Class• Data Class• Refused Bequest• Comments• Conditional Complexity• Indecent Exposure• Solution Sprawl• Combinatorial Explosion• Oddball Solution
From Refactoring and Refactoring to Patterns
• Extract Method• Inline Method• Move Method• Extract Class• Encapsulate Field• Rename Method• Add Parameter• Remove Parameter• Encapsulate Downcast• Pull Up Method• Push Down Method• Extract Superclass• Extract Subclass• Extract Interface
“There is a natural relation between patterns and refactoring. Patterns are where you want to be; refactorings are ways to get there from somewhere else.”
Refactoring p. 107
Pattern To Towards AwayAdapter Extract Adapter (258),Unify Interfaces with Adapter(247) Unify Interfaces with
Adapter(247)
Builder Encapsulate Composite with Builder(96)
Collecting Parameter Move Accumulation to Collecting Parameter(313)
Command Replace Conditional Dispatcher with Command(191) Replace Conditional Dispatcher with Command(191)
Composed Method Compose Method (123) Composite Replace One/Many Distinctions with Composite(224), Extract
Composite(214), Replace Implicit Tree with Composite(178) Encapsulate Composite with
Builder(96)
Creation Method Replace Constructors with Creation Methods (57)
Decorator Move Embellishment to Decorator(144) Move Embellishment to Decorator(144)
Factory Move Creation Knowledge to Factory (68),Encapsulate Classes with Factory (80)
Factory Method Introduce Polymorphic Creation with Factory Method (88)
Interpreter Replace Implicit Language with Interpreter(269)
Iterator Move Accumulation to Visitor(320)
Null Object Introduce Null Object (301) Observer Replace Hard-Coded Notifications with Observer(236) Replace Hard-Coded
Notifications with Observer(236)
Singleton Limit Instantiation with Singleton(296) Inline Singleton(114)
State Replace State-Altering Conditionals with State(166) Replace State-Altering Conditionals with State(166)
Strategy Replace Conditional Logic with Strategy (129) Replace Conditional Logic with Strategy (129)
Template Method Form Template Method (205) Visitor Move Accumulation to Visitor (320) Move Accumulation to
Visitor (320)
Smell RefactoringDuplicated Code (39) Form Template Method(205)Introduce Polymorphic Creation with Factory Method(88)
Chain Constructors(340)Replace One/Many Distinctions with Composite (224)Extract Composite(214)Unify Interfaces with Adapter (247)Introduce Null Object(301)
Long Method(40) Compose Method (123)Move Accumulation to Collecting Parameter (313)Replace Conditional Dispatcher with Command (191)Move Accumulation to Visitor (320)Replace Conditional Logic with Strategy(129)
Conditional Complexity(41) Replace Conditional Logic with Strategy(129)Move Embellishment to Decorator (144)Replace State-Altering Conditionals with State (166)Introduce Null Object(301)
Primitive Obsession (41) Replace Type Code with Class (286)Replace State-Altering Conditionals with State ( 166)Replace Conditional Logic with Strategy(129)Replace Implicit Tree with Composite (178)Replace Implicit Language with Interpreter (269)Move Embellishment to Decorator (144)Encapsulate Composite with Builder (96)
Indecent Exposure (42) Encapsulate Classes with Factory (80)Solution Sprawl (43) Move Creation Knowledge to Factory(68)Alternative Classes with Different Interfaces (43)
Unify Interfaces with Adapter (247)
Lazy Class (43) Inline Singleton (114)Large Class(44) Replace Conditional Dispatcher with Command (191)Replace State-Altering Conditionals with State (166)
Replace Implicit Language with Interpreter (269)
Switch Statements(44) Replace Conditional Dispatcher with Command (191) Move Accumulation to Visitor (320)
Combinatorial Explosion (45) Replace Implicit Language with Interpreter (269)
Oddball Solution (45) Unify Interfaces with Adapter (247)
ENCAPSULATE CREATION WITH FACTORY
Code!
CONDITIONAL TO STRATEGYCode!
WHEN?
XP Simple Design
• Passes all tests• Clear, expressive, consistent• No duplication•Minimal?
Beyond
• Application• Domain-Specific• Business Process• Organizational• User Interface Design
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.
Learning more…The “Original” Refactoring book. All your classic refactorings!
Great content and catalog of composite refactorings to help with patterns.
Share the Vocabulary
1) In design meetings2) In documentation3) In naming conventions4) To groups of interested developers
Action Items!
• Play with Patterns• Share the Vocabulary– Study HFDP with your team
• Build a common language
• Participate in a local Software Craftsmanship group near you!
My Contact Info
• @mdclement• [email protected]• http://blog.softwareontheside.com• http://agilecodegames.com• https://github.com/mdclement• Utah Software Craftsmanship Group– http://utahsc.org– @utahsc