design patterns - the good, the bad, and the anti-pattern
TRANSCRIPT
![Page 1: Design patterns - The Good, the Bad, and the Anti-Pattern](https://reader036.vdocument.in/reader036/viewer/2022062412/58aea2341a28abd43a8b5a79/html5/thumbnails/1.jpg)
Design PatternsThe Good, the Bad, and the Anti-Pattern
Barry O Sullivan 2016
![Page 2: Design patterns - The Good, the Bad, and the Anti-Pattern](https://reader036.vdocument.in/reader036/viewer/2022062412/58aea2341a28abd43a8b5a79/html5/thumbnails/2.jpg)
Who am me?
My name is Barry O Sullivan @barryosull
- Product Developer - Systems Architect - Development Manager - Freelancer
Current positionFinishing up as Development Manager for Hiup(Going to back to contracting, shhhhh)
![Page 3: Design patterns - The Good, the Bad, and the Anti-Pattern](https://reader036.vdocument.in/reader036/viewer/2022062412/58aea2341a28abd43a8b5a79/html5/thumbnails/3.jpg)
What is a Design pattern?
A design pattern is a general, repeatable solution to a commonly occurring problem in
software design
![Page 4: Design patterns - The Good, the Bad, and the Anti-Pattern](https://reader036.vdocument.in/reader036/viewer/2022062412/58aea2341a28abd43a8b5a79/html5/thumbnails/4.jpg)
What does this mean?
It means we can design our systems to support change, rather than fight it.
It’s about expressing your intent
• Changes become easier• Less hacks• Code is easier to read and understand• Faster development• Fewer bugs
![Page 5: Design patterns - The Good, the Bad, and the Anti-Pattern](https://reader036.vdocument.in/reader036/viewer/2022062412/58aea2341a28abd43a8b5a79/html5/thumbnails/5.jpg)
That sounds amazing!
So we should all use them right?
Well there’s a catch
Design patterns make it very easy to do one thing, by making it harder to do others
It’s architecture, which is about tradeoffs
![Page 6: Design patterns - The Good, the Bad, and the Anti-Pattern](https://reader036.vdocument.in/reader036/viewer/2022062412/58aea2341a28abd43a8b5a79/html5/thumbnails/6.jpg)
In other words
Using the wrong pattern is worse than using no pattern at all
![Page 7: Design patterns - The Good, the Bad, and the Anti-Pattern](https://reader036.vdocument.in/reader036/viewer/2022062412/58aea2341a28abd43a8b5a79/html5/thumbnails/7.jpg)
The Anti-Pattern
Patterns that seem like they’ll work but don’t
Anti-Patterns are usually emergent and are always bad
![Page 8: Design patterns - The Good, the Bad, and the Anti-Pattern](https://reader036.vdocument.in/reader036/viewer/2022062412/58aea2341a28abd43a8b5a79/html5/thumbnails/8.jpg)
Ball of Mud
• No discernable architecture
• Everything is different
• Extracting it piece by piece doesn’t work You just end up with mud everywhere.
![Page 9: Design patterns - The Good, the Bad, and the Anti-Pattern](https://reader036.vdocument.in/reader036/viewer/2022062412/58aea2341a28abd43a8b5a79/html5/thumbnails/9.jpg)
God Class
And God said “Let’s put all the functionality in the user
class”
Large, bloated classes that do everything and are impossible to understand and maintain.
![Page 10: Design patterns - The Good, the Bad, and the Anti-Pattern](https://reader036.vdocument.in/reader036/viewer/2022062412/58aea2341a28abd43a8b5a79/html5/thumbnails/10.jpg)
The Big Reveal
Using the wrong design patternIs an Anti-Pattern
![Page 11: Design patterns - The Good, the Bad, and the Anti-Pattern](https://reader036.vdocument.in/reader036/viewer/2022062412/58aea2341a28abd43a8b5a79/html5/thumbnails/11.jpg)
The Awkward Truth
A ball of mud is better than a system built with all the patterns and zero design
>
![Page 12: Design patterns - The Good, the Bad, and the Anti-Pattern](https://reader036.vdocument.in/reader036/viewer/2022062412/58aea2341a28abd43a8b5a79/html5/thumbnails/12.jpg)
Design patterns
The usual suspects
1. Singleton2. Fluent Interface
3. Command4. Observer
5. ActiveRecord6. MVC
![Page 13: Design patterns - The Good, the Bad, and the Anti-Pattern](https://reader036.vdocument.in/reader036/viewer/2022062412/58aea2341a28abd43a8b5a79/html5/thumbnails/13.jpg)
Singleton Pattern“There can only be one”
For when there should only be one instance of an object system wide
![Page 14: Design patterns - The Good, the Bad, and the Anti-Pattern](https://reader036.vdocument.in/reader036/viewer/2022062412/58aea2341a28abd43a8b5a79/html5/thumbnails/14.jpg)
Singleton PatternWhere to use it?
• Logging• DB connection• Message Bus• Command Handler
$handler= CommandHandler::instance();
try {$handler->handle(
new BeEgyptian($SeanConnery)
);} catch (NobodyIsBuyingItException $e) {
….}
![Page 15: Design patterns - The Good, the Bad, and the Anti-Pattern](https://reader036.vdocument.in/reader036/viewer/2022062412/58aea2341a28abd43a8b5a79/html5/thumbnails/15.jpg)
Singleton Pattern
Where not to use it?
• Globally accessing a class(Laravel Facades, I'm looking at you!)
Use DI instead (like the Laravel 5.1 request object)
![Page 16: Design patterns - The Good, the Bad, and the Anti-Pattern](https://reader036.vdocument.in/reader036/viewer/2022062412/58aea2341a28abd43a8b5a79/html5/thumbnails/16.jpg)
Fluent Interface
“You talking to me?”
Chain methods together to make complex programs easier to compose and read
$customer->add_to_basket($hat, 1)->add_to_basket($dress, 2)->apply_discount_code(“23453”)->purchase(“4242-4242-4242-4242”, “123”, “12/12”);
![Page 17: Design patterns - The Good, the Bad, and the Anti-Pattern](https://reader036.vdocument.in/reader036/viewer/2022062412/58aea2341a28abd43a8b5a79/html5/thumbnails/17.jpg)
Fluent Interface
Where to use it
For representing strongly defined grammars• SQL (eg Eloquent)
• Domain Specific Languages
![Page 18: Design patterns - The Good, the Bad, and the Anti-Pattern](https://reader036.vdocument.in/reader036/viewer/2022062412/58aea2341a28abd43a8b5a79/html5/thumbnails/18.jpg)
Fluent Interface
Where to not use it
Anywhere else
If you’re not representing a language, don’t use this interface
Main offender, jQuery$(“.elem”).parents(“.main”).find(“input.name”)
.data(“name”,”john”).addClass(“selected”).removeClass(“.meh_class”).val(“what the?”)
![Page 19: Design patterns - The Good, the Bad, and the Anti-Pattern](https://reader036.vdocument.in/reader036/viewer/2022062412/58aea2341a28abd43a8b5a79/html5/thumbnails/19.jpg)
Command pattern“Can I take your order sir?”
Encapsulate a command that can be handled now or later.
Contains all the data required to run the command
![Page 20: Design patterns - The Good, the Bad, and the Anti-Pattern](https://reader036.vdocument.in/reader036/viewer/2022062412/58aea2341a28abd43a8b5a79/html5/thumbnails/20.jpg)
Command pattern
Where to use it
Anywhere you just want something done and don’t care about the details
(Controller => Application Layer)
Message passing
Scheduled tasks
![Page 21: Design patterns - The Good, the Bad, and the Anti-Pattern](https://reader036.vdocument.in/reader036/viewer/2022062412/58aea2341a28abd43a8b5a79/html5/thumbnails/21.jpg)
Command pattern
Where not to use it
Anywhere it gets in the way
You’re creating a wall. Only use it if the wall is useful
![Page 22: Design patterns - The Good, the Bad, and the Anti-Pattern](https://reader036.vdocument.in/reader036/viewer/2022062412/58aea2341a28abd43a8b5a79/html5/thumbnails/22.jpg)
Observer PatternDecoupled systems that want to alert each other
of changes
An observer registers itself with a subject. When the subject changes, it alerts the observer
![Page 23: Design patterns - The Good, the Bad, and the Anti-Pattern](https://reader036.vdocument.in/reader036/viewer/2022062412/58aea2341a28abd43a8b5a79/html5/thumbnails/23.jpg)
Observer Pattern
Where to use it
Messaging systems
Anywhere that the relationships can change
Modular systems
![Page 24: Design patterns - The Good, the Bad, and the Anti-Pattern](https://reader036.vdocument.in/reader036/viewer/2022062412/58aea2341a28abd43a8b5a79/html5/thumbnails/24.jpg)
Observer Pattern
Where not to use it
Where the objects scope will never change Ie, it's created internally, used, then
discarded
Sometimes things should be coupled, and that’s ok
![Page 25: Design patterns - The Good, the Bad, and the Anti-Pattern](https://reader036.vdocument.in/reader036/viewer/2022062412/58aea2341a28abd43a8b5a79/html5/thumbnails/25.jpg)
Active Record (ORM)“We're all connected”
Make it easy to store your class hierarchy in a Database, without having to write all the
translation code (eg. to SQL)
$team= NewsTeam::find(‘Evening News Team);
$new_insult = new Insult();$new_insult ->target = ‘Their Clothes’;$new_insult->question =‘Purchase location’;$new_insult ->punchline = ‘The Toilet Store’;
$team->insults[] = $new_insult ;$team->status = ‘rekd’;$team->save();
![Page 26: Design patterns - The Good, the Bad, and the Anti-Pattern](https://reader036.vdocument.in/reader036/viewer/2022062412/58aea2341a28abd43a8b5a79/html5/thumbnails/26.jpg)
Active Record (ORM)
Where to use it
Simple CRUD based domains where your objects have simple relational structures
Not a lot of domain behavior
Mostly just key value pairs
![Page 27: Design patterns - The Good, the Bad, and the Anti-Pattern](https://reader036.vdocument.in/reader036/viewer/2022062412/58aea2341a28abd43a8b5a79/html5/thumbnails/27.jpg)
Active Record (ORM)
Where not to use it
Complex domains with multiple views on data, rather than just the basic view
Eg. A checkout process that has promotions and special offers based on your address
Anything with lots of analytics
![Page 28: Design patterns - The Good, the Bad, and the Anti-Pattern](https://reader036.vdocument.in/reader036/viewer/2022062412/58aea2341a28abd43a8b5a79/html5/thumbnails/28.jpg)
Active RecordAlternatives
The Repository pattern:Keep the domain logic separate from the storage logic
CQRS: Have a single data model, multiple views built from that core model
NoSQL:Worth a mention, store trees of data with dynamic shapes
![Page 29: Design patterns - The Good, the Bad, and the Anti-Pattern](https://reader036.vdocument.in/reader036/viewer/2022062412/58aea2341a28abd43a8b5a79/html5/thumbnails/29.jpg)
MVC“Divide and Conquer”
Structure your app into three distinct areas of responsibility, Model/View/Controller
![Page 30: Design patterns - The Good, the Bad, and the Anti-Pattern](https://reader036.vdocument.in/reader036/viewer/2022062412/58aea2341a28abd43a8b5a79/html5/thumbnails/30.jpg)
MVC
Where to use it
Any complex application that's more than a single form
You’re most likely already using it at work(If not, I’m feel your pain)
![Page 31: Design patterns - The Good, the Bad, and the Anti-Pattern](https://reader036.vdocument.in/reader036/viewer/2022062412/58aea2341a28abd43a8b5a79/html5/thumbnails/31.jpg)
MVCWhere it goes wrong
MVC doesn't explain the M part very well.
M is where Design patterns really come into play
All examples code use ORM, so people think that you just use ORM and your M part is sorted
That's not design, that's laziness. Learn patterns, git gud! (Dark Souls!)
![Page 32: Design patterns - The Good, the Bad, and the Anti-Pattern](https://reader036.vdocument.in/reader036/viewer/2022062412/58aea2341a28abd43a8b5a79/html5/thumbnails/32.jpg)
Is it worth it?
Definitely!
Imagine your code as network cables, now imagine going from this to this
=>
![Page 33: Design patterns - The Good, the Bad, and the Anti-Pattern](https://reader036.vdocument.in/reader036/viewer/2022062412/58aea2341a28abd43a8b5a79/html5/thumbnails/33.jpg)
Light at the end of the tunnel
A well designed system is a pleasure to change and test
It just takes time and effort to understand the patterns
“You have to get worse, before you can get better”
![Page 34: Design patterns - The Good, the Bad, and the Anti-Pattern](https://reader036.vdocument.in/reader036/viewer/2022062412/58aea2341a28abd43a8b5a79/html5/thumbnails/34.jpg)
Choosing the right pattern
Don't just pick one and assume it will be fine
Research them all, understand their shape
Look at the problem, figure out what changes and why it’s hard to change. Find the pattern.
Try to use them and fail (on a branch)
![Page 35: Design patterns - The Good, the Bad, and the Anti-Pattern](https://reader036.vdocument.in/reader036/viewer/2022062412/58aea2341a28abd43a8b5a79/html5/thumbnails/35.jpg)
Honourable mentions
• Factory pattern• Repository pattern• Interfaces (just in general)• Dependency injection• Façade• Iterator• Abstract classes
![Page 36: Design patterns - The Good, the Bad, and the Anti-Pattern](https://reader036.vdocument.in/reader036/viewer/2022062412/58aea2341a28abd43a8b5a79/html5/thumbnails/36.jpg)
Further Reading
“If I have seen further, it is by standing on the shoulders of giants.”
• http://www.martinfowler.com/• https://sourcemaking.com/• https://en.wikipedia.org/wiki/Software_design_pattern
Also, why not talk to the other developers here?(I’m sure we’ve all fucked up with patterns as much as I have . . . )
![Page 37: Design patterns - The Good, the Bad, and the Anti-Pattern](https://reader036.vdocument.in/reader036/viewer/2022062412/58aea2341a28abd43a8b5a79/html5/thumbnails/37.jpg)
Thank you
Any questions?