Upload
paramisoft
View
813
Download
0
Tags:
Embed Size (px)
DESCRIPTION
Design pattern (Abstract Factory & Singleton)
Citation preview
Design Pattern
Introduction Singleton & Abstract Factory Design
Pattern
-ParamiSoft Systems Pvt. Ltd.
Agenda• Introduction to Design Patterns
o What is a Design Patterno Why Study Design Patternso History of Design Patternso The Gang of Four
• The Singleton Patterno Introductiono Logger Exampleo Lazy Instantiationo Limitations
Abstract Factory Patterno Abstract Factory in real lifeo Exampleo Real life vs Java Objecto 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 wroteo “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 elementso Pattern Name – helps develop a catalog of common problemso 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 typeso 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 CatalogCreational
Abstract FactoryBuilderFactory MethodPrototypeSingleton
StructuralAdapterBridgeCompositeDecoratorFaçadeFlyweightProxy
BehavioralChain of ResponsibilityCommandInterpreterIteratorMediatorMementoObserverStateStrategyTemplate MethodVisitor
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 Useso Loggingo Cacheso Registry Settingso Access External Resources
• Printer• Device Driver• Database
-ParamiSoft Systems Pvt. Ltd.
Example: LoggerWhat 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 Singletonpublic 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
2Receives the OrderCreates the Dish
4 Delivers the Dish3 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
Linkso http://en.wikipedia.org/wiki/Design_patterno http://sourcemaking.com/design_patterns
Linkso Design Patterns in Ruby – Russ Olseno 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.