0% found this document useful (0 votes)
26 views15 pages

Design Patterns Explained With Sketches

This document introduces various design patterns organized into three categories: creational, structural, and behavioral. Creational patterns provide mechanisms for object creation. Structural patterns deal with relationships between entities. Behavioral patterns are about communication between objects. Specific patterns like singleton, factory method, adapter, and strategy are explained with short examples.

Uploaded by

rjsinha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views15 pages

Design Patterns Explained With Sketches

This document introduces various design patterns organized into three categories: creational, structural, and behavioral. Creational patterns provide mechanisms for object creation. Structural patterns deal with relationships between entities. Behavioral patterns are about communication between objects. Specific patterns like singleton, factory method, adapter, and strategy are explained with short examples.

Uploaded by

rjsinha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Design Patterns

explained

with sketches
by Steven Giesel
Table of contents
Prolog Structural
1 What is a “Design Pattern”?
10 Adapter

2 Disclaimer
11 Proxy

Behavioral Epilog
3 Mediator

12 Thanks

4 Strategy

13 Version
5 Memento

6 Chain of Command

Creational
7 Singleton

8 Builder

9 Factory method

What is a

“Design Pattern”?
Design patterns describe a solution to a common set of problems. Like a blueprint, which
you can customize for your specific problem you want to solve. There are basically 3
categories we can put those design patterns in

Creational gives a mechanism how we can create objects with the most flexibilit

Structural is about giving flexibility and efficiency to a larger structure of object

Behavioral is about how objects or components communicate with each other as well
as how they share responsibilites.

This small eBook gives you a nice introduction or refresher over some of them.
Disclaimer

Once you have learned a new design pattern you want to use it everywhere. But there is a
certain risk that you make your code more complex and less readable just by "over-
introducing" certain patterns. Sometimes a new MyObject( ) is easier than using a factory.

I will just show some examples in this small eBook. As always practice makes the master.

“If all you have is a hammer, ever ything looks like a nail .”

Mediator
Behavioral

The waiter mediates between you (the client) and the kitchen. Neither the kitchen nor you
know directly from each other. He takes your order and goes with that to the kitchen,
which then will (hopefully) come back with food.

You order food Places the order

You (client)
 Serves the dish Gives the meal The kitchen aka

aka a component another component
Waiter aka

Mediator

Take Away
Reduces dependencies between component
Change interactions between objects independently
More info
Strategy
Behavioral

You have different strategies to go to the airport (by taxi, bus or bicycle). You decide in
the moment, depending on some conditions like traffic and cost, which means of
transportation you use. All of them are similar and they surf the same purpose.

Wants to go 

the airport

You

Take Away
Easy to swap out algorithmn. Also they are decoupled from the other logi
Goes hand in hand with Open/Closed Principle as well as “favor composition over
inheritance
Isolate details away (seperation of concerns)
Memento
Behavioral

Paint memorizes the last actions we did, so if we accidentally add a dinosaur to our
picture, Paint can revert or undo to the last state. So Paint stores every state of our nice
picture!

Paint
Damn it, I accidently drew a

dinosaur! Let’s revert the


last action! Undo Redo

Restore last state

Tools

Take Away
Store a snapshot of the state without revealing the state to the outside worl
Every concern is encapsulated in its own objec
The state can grow quite fast if not controlle
Undo-Redo can be done with two stack’s with the memento pattern
Chain of Responsibility
Behavioral

There is a clear chain of people involved when calling the IT suppoirt. First we have a
chatbot, which tries to solve our issue. If that doesn’t help, we go further with a IT
support person, which tries to resolve our issue.
Hey Support, my
Windows is not
working! Did you reboot? Did you try Linux?

Request Handler Handler

Take Away
Perfect if we want to process something in specific orde
Seperation of concerns - Each command handler can do one thin
We can introduce new handlers with ease - Open/Closed Principle
Singleton
Creational
Imagine your office has one printer, and one printer only. You get the same printer
instance independent if you choose the printer from your PC or from your coworkers PC.

You Coworker

Office Printer

Only a single one available
Coworker Coworker

Take Away
Only one instance, which is only initialized once
Can be useful for things like loggin
Be careful though, as it can hide bad design and violates the “Single
Responsibility” principle
Builder
Creational

A house is a very complex object to build. It involves walls, doors, and a rooftop. So when
we built one, we can hire a builder for us, which does the job one by one, piece by piece
until we have our beautiful house!

Take Away
Perfect to get rid of big constructors, which take a huge amount of parameter
Encapsulate code for construction and representatio
The builder needs “access” to the internal representation and creatio
StringBuilder is a famous example for the pattern More info
Factory method
Creational
Tom want’s to get some candy. So he goes to the vending machine, which has candy for
him. With a press of a button he can get different kinds of candy, which the machine
creates for him.

I want some Candy!!! gets

on and
a butt
re sse s
P dy
a ca n

Creates different

Candy

Tom

Take Away
Avoid tight coupling between creator and the concrete implementation
Can be extended to Abstract Factory patter
Factory only exposes a common shared type - perfect for the Open-Close
Principle More info
Adapter
Structural
An EU power plug does not fit into an US wall socket, they are incompatible. With the EU
to US power adapter , we operate seemingly incompatible interfaces to operate together.
It is a translator between two objects.

EU Power Plug EU to US US Power Plug US Wall Socket


Adapter

Take Away
Holds up Single-Responsibility and the Open-Close principle as we decouple the
logic how different objects have to communicate with each other
Every Mapper in your code is an implementation of the adapter pattern.
Proxy
Structural

When we pay with Apple Pay and similar services the merchant does not see the credit
card which is linked to Apple Pay. Apple Pay acts as a proxy for the “real” credit card.

I want to pay with Apple Pay! That makes 15.99$


Pay Only
pple sees
ith A Appl
s w not t e Pay

Pay he Cr
edit

Acts instead of it
Uses the credit card


Card

Take Away
Extending a class without subclassing it (Open-Close Principle
Is transparent from the outside world. Your user doesn’t know it is a proxy
Entity Framework uses them to lazy load entities. (It sits on top of your getter’s.)
Thanks
A big shout out to Mahdiye Ijavi for the design template.

Also thanks to flaticon.com as I am using their icons all over the place.

A big shout out goes to you dear reader. If you have any input or requests let me know.

Down there you will find some links how you can reach out to me. I will be more than
happy to get some feedback and new ideas to continue the journey!


As you might know creating those free eBooks takes time and resources. If you want to
support me in any way you also find opportunities on my GitHub account or on my
webpage.

Steven Giesel steven-giesel.com linkdotnet


Version
1.0 - 2022-11-16
Initial Release

Steven Giesel steven-giesel.com linkdotnet

You might also like