The document discusses software design patterns, including definitions, types (creational, behavioral, structural), components, benefits, and examples. It provides examples of patterns like facade, observer, decorator, state, strategy, template method, iterator, bridge, and singleton from both non-software (e.g. auction, vending machine) and software domains (Temple Run 2 game). For each pattern, it explains the concept, provides a non-software diagram, describes an implementation in Temple Run 2, and diagrams the software example. The patterns allow code reuse, extensibility, and flexibility to vary behaviors without changing interfaces.
2. Overview
In this section I will be discussing about
Basics of design patterns
Types
Different design patterns with non-
software and software examples
For software example, I have
explained each pattern based on a
very popular game Temple Run2.
3. Definitions
Christopher Alexander defined a
pattern as a solution to a problem in a
context:
Each pattern describes a problem
which occurs over and over again in
our environment and then describes
the core of the solution to that
problem, in such a way that you can
use this solution a million times over,
without ever doing it the same way
twice.
4. Components required for
patterns:
Name
Purpose, the problem it solves
Way to accomplish this
Constraints and forces we have to
consider in order to accomplish it
5. Why study design patterns?
Because design patterns-
Help reuse existing high quality solutions
to commonly occurring problems
Improve individual and team learning
Shift level of thinking to a high
perspective
Illustrate basic object-oriented principles
Improves modifiability and maintainability
of the code
Improve team communication n
individual learning
8. 1) Fa巽ade pattern
Provide a unified interface to a set of
interfaces in a subsystem.
Defines a higher-level interface that
makes the subsystem easier to use.
9. Non-software example for fa巽ade
The customer service representative acts as a
Facade, providing an interface to the order
fulfillment department, the billing department,
and the shipping department.
Object Diagram for facade using Phone
Order
10. Software example
In temple run game the upgrade
option acts as a fa巽ade, giving an
interface for selecting a player, for
abilities and powerups.
12. 2) Observer pattern
Define a one-to-many dependency
between objects where a state change
in one object results in all its
dependents being notified and
updated automatically.
oservers delegate the responsibility for
monitoring for an event to a central
object(the subject) so that it can notify
a varying list of objects that an event
has occurred.
13. Non-software example
The following figure shows an Auction
example of an observer
The auctioneer starts the bidding, and
"observes" when a paddle is raised to
accept the bid. The acceptance of the
bid changes the bid price, which is
broadcast to all of the bidders in the form
of a new bid.
14. Software example
In temple run game ,when the player
completes certain objective(s)
following needs to be updated/notified
automatically-
1. Current list(list of objectives to be
achieved)
2. Complete list(list of objectives
completed)
3. Increase multiplier
4. Increase score
16. 3) Decorator pattern
Attach additional responsibilities to an
object dynamically keeping the same
interface.
Decorators provide a flexible alternative
to subclassing for extending functionality.
The object that we want to use does the
basic function.
However, we need to add some
additional functionality occurring before
or after the objects base function.
Allows extension of functionalities
without resorting subclassing.
17. Non-software example:
The Decorator attaches additional
responsibilities to an object
dynamically.
Although paintings can be hung on a
wall with or without frames, frames are
often added, and it is the frame which
is actually hung on the wall.
Prior to hanging, the paintings may be
matted and framed, with the
painting, matting, and frame forming a
single visual component.
19. Software example
In temple run game, run is a basic
object. The player can simply go on
running without taking any
powerups(boost, shield,coin magnet).
Additional functionality like Boost(runs
faster),shield(protects from fire n other
obstacles),coin magnet(attracts the coins
automatically while running) will be
added if the player takes any of the
above mentined powerups which will be
appearing on the screen while running.
21. 4) State pattern
Allow an object to alter its behavior
when its internal state changes.
The object will appear to change its
class.
22. Non-software example
This pattern can be observed in a
vending machine.
Vending machines have states based on
the inventory, amount of currency
deposited, the ability to make change,
the item selected, etc.
When currency is deposited and a
selection is made, a vending machine
will either deliver a product and no
change, deliver a product and change,
deliver no product due to insufficient
currency on deposit, or deliver no
25. 5) Strategy pattern
Define a family of algorithms,
encapsulate each one, and make
them interchangeable.
Strategy lets the algorithm vary
independently from clients that use it.
It enables us to use different rules or
algorithms depending on the context
in which they occur.
Separates the selection of algorithms
from the implementation of the
26. Non-software example
Modes of transportation to an airport
is an example of a Strategy.
Any of these modes of transportation
will get a traveler to the airport, and
they can be used interchangeably.
The traveler must chose the Strategy
based on tradeoffs between
cost, convenience, and time.
28. Software example for strategy
In temple run game, when a use double clicks on
himself the following changes happen:-
If the power tank is not filled-no changes
If the power tank is filled ,one of the following
happens until the tank gets emptied
1. If Powerup=boost, engages boost instantly
2. If Powerup=score bonus, then instant 500 point
bonus
3. If Powerup=coin magnet, then engages coin
magnet instantly
4. If Powerup=shield, then engages shield instantly
There are four strategies a user can select for
operation to occur when we double click (when the
29. (cont.)
In the above example we can observe
that there is a change of state in tank
from empty to full and vice versa.
And the player can select any of the
strategies(boost, coin magnet, shield or
score bonus) depending on his need.
They all have same interface
(Note: the user needs to select any of
the strategies(in powerup menu) before
he starts playing so that he can use that
just by double clicking when the tank is
full.
31. 6) Template method pattern
The Template Method defines a
skeleton of an algorithm in an
operation, and defers some steps to
subclasses.
Template method lets subclasses
redefine certain steps of an algorithm
without changing the algorithm's
structure
Allow definition of subsets that vary
while maintaining a consistent basic
process
32. Non-software example
Home builders use the Template Method
when developing a new subdivision.
A typical subdivision consists of a limited
number of floor plans, with different
variations available for each floor plan.
Within a floor plan, the
foundation, framing, plumbing, and
wiring will be identical for each house.
Variation is introduced in the latter
stages of construction to produce a wider
variety of models.
34. Software example
In temple run game the basic process
is running.
Variations can be introduced when the
player takes boost, coin magnet or
shield while running which will not
affect the basic process i.e running.
36. 7) Iterator pattern
Provide a way to access the elements of an
aggregate object sequentially without
exposing its underlying representation.
Example:
When the viewer selects the "next"
button, the next tuned channel will be
displayed.
Consider watching television in a hotel room
in a strange city.
When surfing through channels, the channel
number is not important, but the
programming is. If the programming on one
channel is not of interest, the viewer can
request the next channel, without knowing its
number.
38. Software example
In temple run game, the player has to run on the
animated path which is designed in such a way
that the different animations are repeated by
selecting them randomly.
Animation selector selects one of the following in
an iterative manner throughout the game.
1. Normal rock road on mountains
2. Rope
3. wooden bridge
4. broken bridge
5. Tunnel
6. water falls
7. fallen trees and other obstacles
40. 8) Bridge pattern
The Bridge pattern decouples an
abstraction from its implementation, so
that the two can vary independently.
Example: A household switch controlling
lights, ceiling fans, etc. is an example of
the Bridge.
The purpose of the switch is to turn a
device on or off. s
The actual switch can be implemented
as a pull chain, a simple two position
switch, or a variety of dimmer switches.
42. Software example
In temple run game we can have
following options under the caption play
1. Swipe
2. 際際滷
3. Tilt
4. Duck
5. Double tap
Using bridge pattern we are switching
between one of the above operation
without even knowing the actual
implementation.
44. 9) Singleton pattern
Ensure a class has only one instance, and
provide a global point of access to it.
Example:
The office of the President of the United States is
a Singleton.
The United States Constitution specifies the
means by which a president is elected, limits the
term of office, and defines the order of
succession.
As a result, there can be at most one active
president at any given time.
Regardless of the personal identity of the active
president, the title, "The President of the United
States" is a global point of access that identifies
the person in the office.
46. Software example
In temple run game, the player can
play well and set high score
This high score is a single instance of
a class
It is a global point of access that
identifies highest scored points.