11 - Better Living with Patterns
11 - Better Living with Patterns
3
This definition takes a while to sink in …
Here’s a little mnemonic you can repeat to yourself to remember it:
“If you find yourself in a context with a problem that has a
goal that is affected by a set of constraints, then you can
apply a design that resolves the goal and constraints and
leads to a solution.”
After all, you already know that a Design Pattern gives you a
solution to a common recurring design problem.
What is all this formality getting you?
This “force” terminology can be quite confusing when you first see it in
pattern discussions, but just remember that there are two sides of the force
(goals and constraints) and that they need to be balanced or resolved to create a pattern solution.
Don’t let the lingo get in your way and may the force be with you!
7
P
A
T C
T A
E T
R A
N L
O
G
U
E
S
8
9
Organizing Design Patterns Sharpen your pencil
The most well-known scheme was used by the
Adapter Factory Method first pattern catalog and partitions patterns into three
Abstract Factory State
Iterator Strategy distinct categories based on their purposes:
Observer Command Decorator
Template Method Creational, Behavioral and Structural.
Composite Singleton
Facade
Creational patterns involve
Any pattern that is a Behavioral
object instantiation and all
Pattern is concerned with how
provide a way to decouple a client
classes and objects interact and
from the objects it needs to
distribute responsibility.
instantiate.
Design Patterns aren’t a magic bullet; in fact they’re not even a bullet!
▪ Patterns are general solutions to recurring problems and have the benefit of being well tested by lots of developers.
▪ However, patterns aren’t a magic bullet. You also need to think through the consequences on the rest of your design.
12
Thinking in Patterns
Refactoring time is Patterns time!
▪ Refactoring is the process of making changes to your code to improve the way it is organized.
▪ not to change its behavior
▪ For instance, clear code with full of conditional statements with the State pattern or concrete dependencies with a Factory.
Take out what you don’t really need. Don’t be afraid to remove a Design Pattern from your design.
▪ So when do you remove a pattern? When your system has become complex and the flexibility you planned for isn’t needed.
▪ In other words, when a simpler solution without the pattern would be better.
13
Your Mind on Patterns
The Beginner uses patterns everywhere.
This is good: the beginner gets lots of
experience with and practice using As learning progresses, the Intermediate mind
patterns. The beginner also thinks, starts to see where patterns are needed and
“The more patterns I use, the better the design.” where they aren’t.
Complexity and patterns should only be The intermediate mind still tries to fit too
used for practical extensibility. many square patterns into round holes, but
also begins to see that patterns can be
adapted to fit situations where the canonical
The Zen mind is able to see patterns where they fit pattern doesn’t fit.
naturally.
The Zen mind looks for simple solutions that best
solve the problem. The Zen mind thinks in terms of
the object principles and their trade-offs. The Zen
mind also sees relationships to similar patterns
and understands the subtleties of differences in
the intent of related patterns. The Zen mind is also
a Beginner mind — it doesn’t let all that pattern
knowledge overly influence design decisions.
14
when not to use patterns
When a design solution calls for a pattern, you get the benefits of using a solution that has been
time tested by lots of developers (you know, that whole shared vocabulary thing).
However, when you use Design Patterns, there can also be a downside.
Patterns often introduce additional classes and objects, and they increase the complexity.
Patterns can also add more layers to your design, which also adds inefficiency.
Many times you can fall back on your design principles and find a much simpler solution.
If that happens, don’t fight it.
Use the simpler solution.
15
Don’t forget the power of the SHARED VOCABULARY
▪ Don’t underestimate the power of a shared
vocabulary, it’s one of the biggest benefits of
Patterns.
▪ Just think, since the last time we talked about
shared vocabularies; you’ve now started to build up
quite a vocabulary of your own!
▪ Not to mention, you have also learned a full set of
OO design principles from which you can easily
understand the motivation and workings of any
new patterns you encounter.
▪ Now spread the word to others. Why?
Because when your fellow developers know patterns and
use a shared vocabulary as well, it leads to better designs,
better communication and, best of all, it’ll save you a lot of
time that you can spend on cooler things.
16
Top five ways to share your vocabulary
1. In design meetings emretanriverdi.medium.com
Use design patterns to help stay “in the design” longer. Discussing designs from the perspective of Design Patterns and OO
principles keeps your team from getting bogged down in implementation details and prevent many misunderstandings.
Why on earth would anyone waste their time documenting bad solutions?
▪ if there is a recurring bad solution to a common problem, then by documenting it
we can prevent other developers from making the same mistake. After all,
avoiding bad solutions can be just as valuable as finding good ones!