Factory Design Pattern

36
Copyright 2016. Jyaasa Technologies. All Right Reserved ht tp://jyaasa.com Factory Design Pattern. How , Why and When to use it. Kapil Raj Nakhwa co-founder : jyaasa. github.com/xecutioner @xecutioner303 [email protected]

Transcript of Factory Design Pattern

Page 1: Factory Design Pattern

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

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

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

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

THANK YOU !QUESTIONS ?

Page 6: Factory Design Pattern
Page 7: Factory Design Pattern
Page 8: Factory Design Pattern
Page 9: Factory Design Pattern

Problem with initialize in pond ?

Page 10: Factory Design Pattern
Page 11: Factory Design Pattern
Page 12: Factory Design Pattern

Requirements Changes : Your users wants plants now in the pond.

Page 13: Factory Design Pattern
Page 14: Factory Design Pattern

Modify the class Pond to deal with plants as well

Page 15: Factory Design Pattern
Page 16: Factory Design Pattern
Page 17: Factory Design Pattern

But I still need those subclasses to continue adding as per the nature of the pond.

Page 18: Factory Design Pattern
Page 19: Factory Design Pattern
Page 20: Factory Design Pattern

Oh this is great! , I need more habitats than pond now. Let’s add a jungle

Page 21: Factory Design Pattern
Page 22: Factory Design Pattern

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
Page 24: Factory Design Pattern

What if our design now needs more extensions to fungi, insects etc

Page 25: Factory Design Pattern

Introducing abstract Factories

Page 26: Factory Design Pattern
Page 27: Factory Design Pattern
Page 28: Factory Design Pattern
Page 29: Factory Design Pattern
Page 30: Factory Design Pattern
Page 31: Factory Design Pattern
Page 32: Factory Design Pattern
Page 33: Factory Design Pattern

Advantages● Let’s discuss

Disadvantages● let’s discuss

Page 34: Factory Design Pattern

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

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

QUESTIONS ?