SlideShare a Scribd company logo
Design Pattern
Introduction
Singleton & Abstract Factory Design
Pattern
-ParamiSoft Systems Pvt. Ltd.
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.
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.
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.
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.
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.
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.
Singleton Pattern
-ParamiSoft Systems Pvt. Ltd.
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.
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.
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.
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.
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.
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.
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.
Abstract Factory Pattern
-ParamiSoft Systems Pvt. Ltd.
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.
Abstract Factory Pattern in Java
-ParamiSoft Systems Pvt. Ltd.
Factory
Kitchen
Real Life vs Java Object
-ParamiSoft Systems Pvt. Ltd.
1 Orders a Dish from Menu
2
Receives the Order
Creates the Dish
4 Delivers the Dish
3 Outsources to Chef
How Factory Pattern works in Real Life ?
KitchenFactory factory = new KitchenFactory();
Food dosa = factory.getFood("Dosa");
dosa.print();
Food noodles = factory.getFood("Noodles");
noodles.print();
public Food getFood(String name) {
if (name.equals("Dosa")) {
IndianFactory factory = new IndianFactory();
return factory.getFood(name);
} else if (name.equals("Noodles")) {
ChineseFactory factory = new ChineseFactory();
return factory.getFood(name);
}
Return null;}
1
4
Food
Dosa Noodles
2
3
Create food from Respective Factory Class
How Factory Pattern works in Java?
-ParamiSoft Systems Pvt. Ltd.
Abstract Factory Consequences
• Isolate concrete classes.
• Make exchanging product families easy.
• Promote consistency among products
-ParamiSoft Systems Pvt. Ltd.
Abstract Factory Limitation
• Supporting new kinds of products is difficult.
-ParamiSoft Systems Pvt. Ltd.
References
Links
o http://en.wikipedia.org/wiki/Design_pattern
o http://sourcemaking.com/design_patterns
Links
o Design Patterns in Ruby – Russ Olsen
o Head First Design Patterns – Elisabeth Freeman, Eric Freeman, Bert Bates and
Kathy Sierra
-ParamiSoft Systems Pvt. Ltd.
If(Questions)
{
Ask;
}
else
{
Thank you;
}
-ParamiSoft Systems Pvt. Ltd.

More Related Content

PPTX
The Singleton Pattern Presentation
PDF
Design Patterns - Factory Method & Abstract Factory
PPTX
Design Pattern - Factory Method Pattern
PPTX
Design Pattern - Singleton Pattern
PPT
Adapter pattern
PPTX
Abstract Factory Design Pattern
PPTX
Design Patterns - Abstract Factory Pattern
PDF
Design patterns
The Singleton Pattern Presentation
Design Patterns - Factory Method & Abstract Factory
Design Pattern - Factory Method Pattern
Design Pattern - Singleton Pattern
Adapter pattern
Abstract Factory Design Pattern
Design Patterns - Abstract Factory Pattern
Design patterns

What's hot (20)

PDF
Collections in Java Notes
PPT
Design Patterns
PPTX
java 8 new features
PPTX
Constructor in java
PDF
Java Course 11: Design Patterns
PPTX
Factory Method Pattern
PPT
Java Persistence API (JPA) Step By Step
PPT
Generics in java
PPT
Java collections concept
PPTX
Gof design patterns
PPT
Object Oriented Programming with Java
PPT
Iterator Design Pattern
PPTX
Creational pattern
PPTX
Composite design pattern
PPTX
Data Types, Variables, and Operators
PPT
Builder pattern
PPT
Abstract Factory Pattern
PPT
Facade pattern
PPTX
Java exception handling
PDF
Java threads
Collections in Java Notes
Design Patterns
java 8 new features
Constructor in java
Java Course 11: Design Patterns
Factory Method Pattern
Java Persistence API (JPA) Step By Step
Generics in java
Java collections concept
Gof design patterns
Object Oriented Programming with Java
Iterator Design Pattern
Creational pattern
Composite design pattern
Data Types, Variables, and Operators
Builder pattern
Abstract Factory Pattern
Facade pattern
Java exception handling
Java threads
Ad

Similar to Design pattern (Abstract Factory & Singleton) (20)

PPTX
Software Architecture and Design Patterns Notes.pptx
PPTX
PPTX
Introduction to Design Patterns
PDF
Design patterns in Java - Monitis 2017
PPTX
Desing pattern prototype-Factory Method, Prototype and Builder
PPT
Introduction to design_patterns
PPTX
OOPSDesign PPT ( introduction to opps and design (
PPTX
Creational Design Patterns.pptx
PPTX
CREATIONAL Pattern .pptx
PDF
Benefits of using software design patterns and when to use design pattern
PPTX
PATTERNS02 - Creational Design Patterns
PPTX
Typescript design patterns applied to sharepoint framework - Sharepoint Satur...
PDF
E1803023637
PPT
Design patterns
PPTX
JS Design patterns in Web technologies including oop techniques.pptx
PDF
Why Design Patterns Are Important In Software Engineering
PDF
GOF Design pattern with java
PPTX
Design pattern of software words computer .pptx
PPT
Design Patterns
PDF
Gang of Four in Java
Software Architecture and Design Patterns Notes.pptx
Introduction to Design Patterns
Design patterns in Java - Monitis 2017
Desing pattern prototype-Factory Method, Prototype and Builder
Introduction to design_patterns
OOPSDesign PPT ( introduction to opps and design (
Creational Design Patterns.pptx
CREATIONAL Pattern .pptx
Benefits of using software design patterns and when to use design pattern
PATTERNS02 - Creational Design Patterns
Typescript design patterns applied to sharepoint framework - Sharepoint Satur...
E1803023637
Design patterns
JS Design patterns in Web technologies including oop techniques.pptx
Why Design Patterns Are Important In Software Engineering
GOF Design pattern with java
Design pattern of software words computer .pptx
Design Patterns
Gang of Four in Java
Ad

More from paramisoft (8)

PDF
Go language presentation
PPTX
Ruby on Rails Introduction
PPTX
Introduction To Backbone JS
PPT
Git essentials
PPTX
iOS development introduction
PPTX
Introduction to HAML
PDF
ParamiSoft Systems Pvt. Ltd. Profile
PPTX
Garden City Ruby Conference - lightening talk
Go language presentation
Ruby on Rails Introduction
Introduction To Backbone JS
Git essentials
iOS development introduction
Introduction to HAML
ParamiSoft Systems Pvt. Ltd. Profile
Garden City Ruby Conference - lightening talk

Recently uploaded (20)

PDF
Machine learning based COVID-19 study performance prediction
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
Big Data Technologies - Introduction.pptx
PDF
Getting Started with Data Integration: FME Form 101
PPTX
Tartificialntelligence_presentation.pptx
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
SOPHOS-XG Firewall Administrator PPT.pptx
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
cuic standard and advanced reporting.pdf
Machine learning based COVID-19 study performance prediction
“AI and Expert System Decision Support & Business Intelligence Systems”
Programs and apps: productivity, graphics, security and other tools
20250228 LYD VKU AI Blended-Learning.pptx
Big Data Technologies - Introduction.pptx
Getting Started with Data Integration: FME Form 101
Tartificialntelligence_presentation.pptx
The Rise and Fall of 3GPP – Time for a Sabbatical?
SOPHOS-XG Firewall Administrator PPT.pptx
Diabetes mellitus diagnosis method based random forest with bat algorithm
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Encapsulation_ Review paper, used for researhc scholars
NewMind AI Weekly Chronicles - August'25-Week II
Mobile App Security Testing_ A Comprehensive Guide.pdf
Spectral efficient network and resource selection model in 5G networks
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Assigned Numbers - 2025 - Bluetooth® Document
Agricultural_Statistics_at_a_Glance_2022_0.pdf
cuic standard and advanced reporting.pdf

Design pattern (Abstract Factory & Singleton)

  • 1. Design Pattern Introduction Singleton & Abstract Factory Design Pattern -ParamiSoft Systems Pvt. Ltd.
  • 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.
  • 18. Abstract Factory Pattern in Java -ParamiSoft Systems Pvt. Ltd.
  • 19. Factory Kitchen Real Life vs Java Object -ParamiSoft Systems Pvt. Ltd.
  • 20. 1 Orders a Dish from Menu 2 Receives the Order Creates the Dish 4 Delivers the Dish 3 Outsources to Chef How Factory Pattern works in Real Life ?
  • 21. KitchenFactory factory = new KitchenFactory(); Food dosa = factory.getFood("Dosa"); dosa.print(); Food noodles = factory.getFood("Noodles"); noodles.print(); public Food getFood(String name) { if (name.equals("Dosa")) { IndianFactory factory = new IndianFactory(); return factory.getFood(name); } else if (name.equals("Noodles")) { ChineseFactory factory = new ChineseFactory(); return factory.getFood(name); } Return null;} 1 4 Food Dosa Noodles 2 3 Create food from Respective Factory Class How Factory Pattern works in Java? -ParamiSoft Systems Pvt. Ltd.
  • 22. Abstract Factory Consequences • Isolate concrete classes. • Make exchanging product families easy. • Promote consistency among products -ParamiSoft Systems Pvt. Ltd.
  • 23. Abstract Factory Limitation • Supporting new kinds of products is difficult. -ParamiSoft Systems Pvt. Ltd.
  • 24. References Links o http://en.wikipedia.org/wiki/Design_pattern o http://sourcemaking.com/design_patterns Links o Design Patterns in Ruby – Russ Olsen o Head First Design Patterns – Elisabeth Freeman, Eric Freeman, Bert Bates and Kathy Sierra -ParamiSoft Systems Pvt. Ltd.