Design Pattern: Factory Pattern
In Factory pattern, we create object without exposing the creation logic to the client and refer to newly created object using a common interface.
Blog Article: http://jyaasa.com/blog/factory-design-pattern-in-ruby/
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
Factory Design Pattern
1. 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
kapil@jyaasa.com
2. Intent
Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory
method lets class defer instantiation to subclasses
Structure
3. 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.
4. 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.
34. 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.
35. 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.