Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
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 Patter...
What is a Design Pattern?
• A problem that someone has already solved.
• A model or design to use as a guide
• More formal...
Why Study Design Patterns?
• Provides software developers a toolkit for handling
problems that have already been solved.
•...
History of Design Patterns
• Christopher Alexander (Civil Engineer) in 1977 wrote
o “Each pattern describes a problem whic...
The Gang of Four
• Defines a Catalog of different design patterns.
• Three different types
o Creational – “creating object...
The Gang of Four: Pattern Catalog
Creational
Abstract Factory
Builder
Factory Method
Prototype
Singleton
Structural
Adapte...
Singleton Pattern
-ParamiSoft Systems Pvt. Ltd.
Singleton
• Definition: “The Singleton Pattern ensures a class has
only one instance, and provides a global point of acces...
Example: Logger
What is wrong with this code?
public class Logger
{
public Logger() { }
public void LogMessage() {
//Open ...
Example: Logger
• Since there is an external Shared Resource
(“log.txt”), we want to closely control how we
communicate wi...
Logger – as a Singleton
public class Logger
{
private Logger{}
private static Logger uniqueInstance;
public static Logger ...
Lazy Instantiation
• Objects are only created when it is needed
• Helps control that we’ve created the Singleton just once...
Singleton Consequences
• Controlled access to sole instance facilitates strict
control over when and how the clients acces...
Singleton Limitations
• The main limitation of the singleton pattern is that is
permits the creation of only one instance ...
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 expe...
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 Patte...
KitchenFactory factory = new KitchenFactory();
Food dosa = factory.getFood("Dosa");
dosa.print();
Food noodles = factory.g...
Abstract Factory Consequences
• Isolate concrete classes.
• Make exchanging product families easy.
• Promote consistency a...
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 Pa...
If(Questions)
{
Ask;
}
else
{
Thank you;
}
-ParamiSoft Systems Pvt. Ltd.
Upcoming SlideShare
Loading in …5
×

Design pattern (Abstract Factory & Singleton)

6,890 views

Published on

Design pattern (Abstract Factory & Singleton)

Published in: Technology
  • Be the first to comment

Design pattern (Abstract Factory & Singleton)

  1. 1. Design Pattern Introduction Singleton & Abstract Factory Design Pattern -ParamiSoft Systems Pvt. Ltd.
  2. 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. 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. 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. 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. 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. 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.
  8. 8. Singleton Pattern -ParamiSoft Systems Pvt. Ltd.
  9. 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. 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. 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. 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. 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. 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. 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.
  16. 16. Abstract Factory Pattern -ParamiSoft Systems Pvt. Ltd.
  17. 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. 18. Abstract Factory Pattern in Java -ParamiSoft Systems Pvt. Ltd.
  19. 19. Factory Kitchen Real Life vs Java Object -ParamiSoft Systems Pvt. Ltd.
  20. 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. 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. 22. Abstract Factory Consequences • Isolate concrete classes. • Make exchanging product families easy. • Promote consistency among products -ParamiSoft Systems Pvt. Ltd.
  23. 23. Abstract Factory Limitation • Supporting new kinds of products is difficult. -ParamiSoft Systems Pvt. Ltd.
  24. 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.
  25. 25. If(Questions) { Ask; } else { Thank you; } -ParamiSoft Systems Pvt. Ltd.

×