factory design pattern
TRANSCRIPT
![Page 1: Factory Design Pattern](https://reader031.vdocuments.mx/reader031/viewer/2022022123/58a5c4981a28ab0b068b6e51/html5/thumbnails/1.jpg)
Copyright 2016. Jyaasa Technologies. All Right Reservedht
tp://jyaasa.com
Factory Design Pattern. How , Why and When to use it.
Kapil Raj Nakhwaco-founder : jyaasa.github.com/xecutioner@[email protected]
![Page 2: Factory Design Pattern](https://reader031.vdocuments.mx/reader031/viewer/2022022123/58a5c4981a28ab0b068b6e51/html5/thumbnails/2.jpg)
IntentDefine an interface for creating an object, but let subclasses decide which class to instantiate. Factory method lets class defer instantiation to subclasses
Structure
![Page 3: Factory Design Pattern](https://reader031.vdocuments.mx/reader031/viewer/2022022123/58a5c4981a28ab0b068b6e51/html5/thumbnails/3.jpg)
Motivation
Factory method is primarily used to create an instance of a class in a more flexible way. Instead of creating instance of a class directly through new, it is created by another class called as factory.
![Page 4: Factory Design Pattern](https://reader031.vdocuments.mx/reader031/viewer/2022022123/58a5c4981a28ab0b068b6e51/html5/thumbnails/4.jpg)
Implementation● A factory class can be implemented in several ways, depending
on the type of problem you are trying to solve:● The Creator class can be an abstract class.● The Creator class can be a concrete class with the Factory
method as a virtual function.● The Factory method may be parameterized to allow creation of
different types of objects.● The Factory method can be a static method.● The Factory method can be an instance method.
![Page 5: Factory Design Pattern](https://reader031.vdocuments.mx/reader031/viewer/2022022123/58a5c4981a28ab0b068b6e51/html5/thumbnails/5.jpg)
THANK YOU !QUESTIONS ?
![Page 6: Factory Design Pattern](https://reader031.vdocuments.mx/reader031/viewer/2022022123/58a5c4981a28ab0b068b6e51/html5/thumbnails/6.jpg)
![Page 7: Factory Design Pattern](https://reader031.vdocuments.mx/reader031/viewer/2022022123/58a5c4981a28ab0b068b6e51/html5/thumbnails/7.jpg)
![Page 8: Factory Design Pattern](https://reader031.vdocuments.mx/reader031/viewer/2022022123/58a5c4981a28ab0b068b6e51/html5/thumbnails/8.jpg)
![Page 9: Factory Design Pattern](https://reader031.vdocuments.mx/reader031/viewer/2022022123/58a5c4981a28ab0b068b6e51/html5/thumbnails/9.jpg)
Problem with initialize in pond ?
![Page 10: Factory Design Pattern](https://reader031.vdocuments.mx/reader031/viewer/2022022123/58a5c4981a28ab0b068b6e51/html5/thumbnails/10.jpg)
![Page 11: Factory Design Pattern](https://reader031.vdocuments.mx/reader031/viewer/2022022123/58a5c4981a28ab0b068b6e51/html5/thumbnails/11.jpg)
![Page 12: Factory Design Pattern](https://reader031.vdocuments.mx/reader031/viewer/2022022123/58a5c4981a28ab0b068b6e51/html5/thumbnails/12.jpg)
Requirements Changes : Your users wants plants now in the pond.
![Page 13: Factory Design Pattern](https://reader031.vdocuments.mx/reader031/viewer/2022022123/58a5c4981a28ab0b068b6e51/html5/thumbnails/13.jpg)
![Page 14: Factory Design Pattern](https://reader031.vdocuments.mx/reader031/viewer/2022022123/58a5c4981a28ab0b068b6e51/html5/thumbnails/14.jpg)
Modify the class Pond to deal with plants as well
![Page 15: Factory Design Pattern](https://reader031.vdocuments.mx/reader031/viewer/2022022123/58a5c4981a28ab0b068b6e51/html5/thumbnails/15.jpg)
![Page 16: Factory Design Pattern](https://reader031.vdocuments.mx/reader031/viewer/2022022123/58a5c4981a28ab0b068b6e51/html5/thumbnails/16.jpg)
![Page 17: Factory Design Pattern](https://reader031.vdocuments.mx/reader031/viewer/2022022123/58a5c4981a28ab0b068b6e51/html5/thumbnails/17.jpg)
But I still need those subclasses to continue adding as per the nature of the pond.
![Page 18: Factory Design Pattern](https://reader031.vdocuments.mx/reader031/viewer/2022022123/58a5c4981a28ab0b068b6e51/html5/thumbnails/18.jpg)
![Page 19: Factory Design Pattern](https://reader031.vdocuments.mx/reader031/viewer/2022022123/58a5c4981a28ab0b068b6e51/html5/thumbnails/19.jpg)
![Page 20: Factory Design Pattern](https://reader031.vdocuments.mx/reader031/viewer/2022022123/58a5c4981a28ab0b068b6e51/html5/thumbnails/20.jpg)
Oh this is great! , I need more habitats than pond now. Let’s add a jungle
![Page 21: Factory Design Pattern](https://reader031.vdocuments.mx/reader031/viewer/2022022123/58a5c4981a28ab0b068b6e51/html5/thumbnails/21.jpg)
![Page 22: Factory Design Pattern](https://reader031.vdocuments.mx/reader031/viewer/2022022123/58a5c4981a28ab0b068b6e51/html5/thumbnails/22.jpg)
Okay so the class name pond is not any more appropriate ! let’s rename it to Habitat.
Why is this design still not correct ?
![Page 23: Factory Design Pattern](https://reader031.vdocuments.mx/reader031/viewer/2022022123/58a5c4981a28ab0b068b6e51/html5/thumbnails/23.jpg)
![Page 24: Factory Design Pattern](https://reader031.vdocuments.mx/reader031/viewer/2022022123/58a5c4981a28ab0b068b6e51/html5/thumbnails/24.jpg)
What if our design now needs more extensions to fungi, insects etc
![Page 25: Factory Design Pattern](https://reader031.vdocuments.mx/reader031/viewer/2022022123/58a5c4981a28ab0b068b6e51/html5/thumbnails/25.jpg)
Introducing abstract Factories
![Page 26: Factory Design Pattern](https://reader031.vdocuments.mx/reader031/viewer/2022022123/58a5c4981a28ab0b068b6e51/html5/thumbnails/26.jpg)
![Page 27: Factory Design Pattern](https://reader031.vdocuments.mx/reader031/viewer/2022022123/58a5c4981a28ab0b068b6e51/html5/thumbnails/27.jpg)
![Page 28: Factory Design Pattern](https://reader031.vdocuments.mx/reader031/viewer/2022022123/58a5c4981a28ab0b068b6e51/html5/thumbnails/28.jpg)
![Page 29: Factory Design Pattern](https://reader031.vdocuments.mx/reader031/viewer/2022022123/58a5c4981a28ab0b068b6e51/html5/thumbnails/29.jpg)
![Page 30: Factory Design Pattern](https://reader031.vdocuments.mx/reader031/viewer/2022022123/58a5c4981a28ab0b068b6e51/html5/thumbnails/30.jpg)
![Page 31: Factory Design Pattern](https://reader031.vdocuments.mx/reader031/viewer/2022022123/58a5c4981a28ab0b068b6e51/html5/thumbnails/31.jpg)
![Page 32: Factory Design Pattern](https://reader031.vdocuments.mx/reader031/viewer/2022022123/58a5c4981a28ab0b068b6e51/html5/thumbnails/32.jpg)
![Page 33: Factory Design Pattern](https://reader031.vdocuments.mx/reader031/viewer/2022022123/58a5c4981a28ab0b068b6e51/html5/thumbnails/33.jpg)
Advantages● Let’s discuss
Disadvantages● let’s discuss
![Page 34: Factory Design Pattern](https://reader031.vdocuments.mx/reader031/viewer/2022022123/58a5c4981a28ab0b068b6e51/html5/thumbnails/34.jpg)
When to use● When the client doesn't know which class it may require at runtime.● A class wants its subclasses to specify the objects it creates.● You want to encapsulate creation of objects.● Object instance needs to be initialized with some data not available to the client.● Object instantiation requires lot of data and there are lots of variations based on the data. Instead provide static
Factory methods that create the instances based on different variations.
![Page 35: Factory Design Pattern](https://reader031.vdocuments.mx/reader031/viewer/2022022123/58a5c4981a28ab0b068b6e51/html5/thumbnails/35.jpg)
Related Patterns● Factory Method has a lot of variations. It can return a new object or the same instance multiple times, or can return
a subclass object by extending a new class.● Factory Method is usually called through a Template Method and is usually a hook that subclasses can override
for custom implementation. E.g. override the Factory method and provide a different implementation.● Used to implement Abstract Factory.● It is a flexible replacement for new as it encapsulates creation.● Factory Method has to be subclassed if it has to return a new object. Prototypes don’t require a new class; it
requires a new object.● Prototypes require initialize operations after returning an instance; Factory Methods don’t require such operation.
![Page 36: Factory Design Pattern](https://reader031.vdocuments.mx/reader031/viewer/2022022123/58a5c4981a28ab0b068b6e51/html5/thumbnails/36.jpg)
QUESTIONS ?