Software Design and
Architecture
WEEK 6
GANG OF FOUR (GOF)
COURSE INSTRUCTOR:
TAZZAINA MALIK
1
Categorization
2
Categorization
3
Gang of Four
“Design Patterns-Elements of
Reusable Object-Oriented Software
”, 1994
◦ Considered the bible of design
pattern books
◦ Describes 23 patterns for OO design
◦ Authored by four people and hence
called “Gang of Four”
◦ Erich Gamma
◦ Richard Helm
◦ Ralph Johnson
◦ John Vlissides
4
GoF Design Patterns
Creational patterns:
◦ Deal with initializing and configuring objects
◦ Hides complex creation logic
Structural patterns:
◦ Define structure of classes or objects
◦ Define how the relation can be defined between entities
Behavioral patterns:
◦ Deal with dynamic interactions among objects
◦ How they distribute responsibility
5
Creational Patterns
1. Abstract Factory
2. Builder
3. Factory Method
4. Prototype
5. Singleton
6
Structural Patterns
6. Adapter
7. Bridge
8. Composite
9. Decorator
10. Façade
11. Flyweight
12. Proxy
7
Behavioral Patterns
13. Chain of Responsibility
14. Command
15. Interpreter
16. Iterator
17. Mediator
18. Memento
19. Observer
20. State
21. Strategy
22. Template
23. Visitor
8
Singleton Pattern
9
What is Singleton Design Pattern?
10
Introduction:
11
Introduction:
12
Singleton
Intent: Ensure a class has only
Problem: Application needs
one instance and provide a
one, and only one, instance of
global point of access to it.
an object. Additionally, lazy
Encapsulated "just-in-time
initialization and global access
initialization" or "initialization
are necessary.
on first use".
13
Class Structure
14
Singleton Design Pattern
1. The singleton pattern is implemented by creating
a class with a method that creates a new
instance of the class if one does not exist.
2. If an instance already exists, it simply returns a
reference to that object.
3. To make sure that the object cannot be
instantiated any other way, the constructor is
made private
15
Static member : This contains the
instance of the singleton class.
Structure of Private constructor : This will
Singleton prevent anybody else to instantiate
Pattern the Singleton class.
Static public method : This provides
the global point of access to the
Singleton object and returns the
instance to the client calling class.
16
Singleton
Implementation
17
Example-1:
18
Create a static property of same class type and make it private
Make constructor private in order to stop creating its instances
Make Create a static method which will check if
there is already an instance is created, if yes then
this method will return same instance without
creating a new one and if no then this method will
create an instance and set it to above mentioned
property and return to the caller. private in order to
stop creating its instances
19
Example-2:
20
Uses of Singleton Pattern
In terms of practical use Singleton patterns are
used in:
✓ Logging
✓ Caches
✓ Database Access
✓ Configuration Settings
✓ Device Driver Objects
21
Eager Vs Lazy Loading:
Eager Loading:
- The instance is already initialized as soon as
the application is up.
Lazy Loading:
-The instance is initialized only when an
application module calls for it.
22
Uses of Singleton Pattern
Hardware interface access: Practically singleton can be used in
case of external hardware resource usage limitation required e.g.,
Hardware printers where the print spooler can be made a
singleton to avoid multiple concurrent accesses and creating
deadlock.
Logger : The Singleton pattern is used in the design of logger
classes. These classes are usually implemented as a singletons
and provide a global logging access point in all the application
components without being necessary to create an object each
time a logging operation is performed.
23
Uses of Singleton Pattern
Configuration File: Another potential candidate for Singleton pattern is
configuration files because this has a performance benefits. It prevents
multiple users to repeatedly access and read the configuration file or
properties file. It creates a single instance of the configuration file which can
be accessed by multiple calls concurrently as it will provide static config. data
loaded into in-memory objects. The application only reads from the
configuration file at the first time and there after from second call onwards the
client applications read the data from in-memory objects.
Cache: We can use the cache as a singleton object as it can have a global point
of reference and for all future calls to the cache object the client application
will use the in-memory object.
24
Threat Safety:
25
Pros
▪Neat way to handle access to share
global resources
▪Easy to implement
▪Guarantees 1 instance
▪Solves a well-defined problem
26
Cons
▪Sometimes abused
▪Confused with factory
▪Hard to write unit test cases
▪Thread safety is at stake
27
Factory Patterns
FACTORY PAT TERN (SIMPLE FACTORY)
28
Factory pattern is a creational pattern
and provides a factory to produce
objects.
We create objects without exposing
the creational logic to the client and
Factory refer to newly created object using a
Pattern common interface
Factories encapsulate object creation
and eliminate it from the client code
• Decoupled systems
• Extensions and modifications made easy
29
What is Factory Pattern?
30
Concept Behind:
31
Concept Behind:
32
33
Application Areas in Real World:
34
Examples
35
Example
36
37
38
Example
39
Example
Complex creational logic
40
Example
41
The factories completely abstracts the
creation and initialization of the product
from the client.
This indirection enables the client to
Summary: focus on its discrete role in the
application without concerning itself with
the details of how the product is created.
Thus, as the product implementation
changes over time, the client remains
unchanged.
42