Factory Design Pattern

Post on 16-Feb-2017

155 views 0 download

Transcript of 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@xecutioner303kapil@jyaasa.com

IntentDefine an interface for creating an object, but let subclasses decide which class to instantiate. Factory method lets class defer instantiation to subclasses

Structure

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.

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.

THANK YOU !QUESTIONS ?

Problem with initialize in pond ?

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

Modify the class Pond to deal with plants as well

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

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

Okay so the class name pond is not any more appropriate ! let’s rename it to Habitat.

Why is this design still not correct ?

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

Introducing abstract Factories

Advantages● Let’s discuss

Disadvantages● let’s discuss

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.

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.

QUESTIONS ?