2. Agenda
⢠Introduction to Design Patterns
o What is a Design Pattern
o Why Study Design Patterns
o History of Design Patterns
o The Gang of Four
⢠The Singleton Pattern
o Introduction
o Logger Example
o Lazy Instantiation
o Limitations
Abstract Factory Pattern
o Abstract Factory in real life
o Example
o Real life vs Java Object
o Limitations
-ParamiSoft Systems Pvt. Ltd.
3. What is a Design Pattern?
⢠A problem that someone has already solved.
⢠A model or design to use as a guide
⢠More formally: âA proven solution to a common problem
in a specified context."
Real World Examples
⢠Blueprint for a house
⢠Manufacturing
-ParamiSoft Systems Pvt. Ltd.
4. Why Study Design Patterns?
⢠Provides software developers a toolkit for handling
problems that have already been solved.
⢠Provides a vocabulary that can be used amongst
software developers.
o The Pattern Name itself helps establish a vocabulary
⢠Helps you think about how to solve a software problem.
-ParamiSoft Systems Pvt. Ltd.
5. History of Design Patterns
⢠Christopher Alexander (Civil Engineer) in 1977 wrote
o â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.â
⢠Each pattern has the same elements
o Pattern Name â helps develop a catalog of common problems
o Problem â describes when to apply the pattern. Describes problem and its
context.
o Solution â Elements that make up the design, their
relationships, responsibilities, and collaborations.
o Consequences â Results and trade-offs of applying the pattern
-ParamiSoft Systems Pvt. Ltd.
6. The Gang of Four
⢠Defines a Catalog of different design patterns.
⢠Three different types
o Creational â âcreating objects in a manner suitable for the situationâ
o Structural â âease the design by identifying a simple way to realize relationships
between entitiesâ
o behaviouralâ âcommon communication patterns between objectsâ
-ParamiSoft Systems Pvt. Ltd.
7. The Gang of Four: Pattern Catalog
Creational
Abstract Factory
Builder
Factory Method
Prototype
Singleton
Structural
Adapter
Bridge
Composite
Decorator
Façade
Flyweight
Proxy
Behavioral
Chain of Responsibility
Command
Interpreter
Iterator
Mediator
Memento
Observer
State
Strategy
Template Method
Visitor
Patterns in red we will
discuss in this presentation
-ParamiSoft Systems Pvt. Ltd.
9. Singleton
⢠Definition: âThe Singleton Pattern ensures a class has
only one instance, and provides a global point of access
to it.â
⢠Best Uses
o Logging
o Caches
o Registry Settings
o Access External Resources
⢠Printer
⢠Device Driver
⢠Database
-ParamiSoft Systems Pvt. Ltd.
10. Example: Logger
What is wrong with this code?
public class Logger
{
public Logger() { }
public void LogMessage() {
//Open File "log.txt"
//Write Message
//Close File
}
}
-ParamiSoft Systems Pvt. Ltd.
11. Example: Logger
⢠Since there is an external Shared Resource
(âlog.txtâ), we want to closely control how we
communicate with it.
⢠We shouldnât have to create the Logger class every time
we want to access this Shared Resource. Is there any
reason to?
⢠We need ONE.
-ParamiSoft Systems Pvt. Ltd.
12. Logger â as a Singleton
public class Logger
{
private Logger{}
private static Logger uniqueInstance;
public static Logger getInstance()
{
if (uniqueInstance == null)
uniqueInstance = new Logger();
return uniqueInstance;
}
}
Note the
parameterless
constructor
-ParamiSoft Systems Pvt. Ltd.
13. Lazy Instantiation
⢠Objects are only created when it is needed
⢠Helps control that weâve created the Singleton just once.
⢠If it is resource intensive to set up, we want to do it once.
-ParamiSoft Systems Pvt. Ltd.
14. Singleton Consequences
⢠Controlled access to sole instance facilitates strict
control over when and how the clients access it
⢠The singleton patter is improvement over global
variables.
⢠It is easy to configure an instance of the application that
extends the functionality of singleton at run-time
⢠More flexible than class operations
-ParamiSoft Systems Pvt. Ltd.
15. Singleton Limitations
⢠The main limitation of the singleton pattern is that is
permits the creation of only one instance of the
class, while most practical applications require multiple
instances to be initialized.
⢠Furthermore, in case of singleton, the system threads
fight to access the single instance thereby degrading the
performance of the applications.
-ParamiSoft Systems Pvt. Ltd.
17. Abstract Factory Pattern is similar to Sub Contracting in real world.
Basically delegating the creation of Objects to expert Factories
-----------------------------------
Orders in a restaurant are received by a Kitchen.
Then are assigned to Special Chefs like
Chinese, Indian, Continental.
Abstract Factory Pattern is a Creational Pattern.
Similar to Factory Pattern it is Object Creation without exposing âHOWâ ?
Abstract Factory Pattern in Real Life
-ParamiSoft Systems Pvt. Ltd.