Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use factory patterns?

What is this factory all about and what are the advantages of using factory patterns? Does it give a plugable architecture? Why it is called "Factory"?

like image 381
RKh Avatar asked Nov 01 '10 07:11

RKh


People also ask

What is a factory method pattern good for?

In class-based programming, the factory method pattern is a creational pattern that uses factory methods to deal with the problem of creating objects without having to specify the exact class of the object that will be created.

What problem does factory pattern solve?

The factory pattern aims to solve a fundamental problem in instantiation – i.e., the creation of a concrete object of a class – in object-oriented programming. In principle, creating an object directly within the class that needs or should use this object is possible, but very inflexible.

What is the best reason to use a design pattern?

Why use a design pattern? The usefulness of using a design pattern is obvious. The design pattern can accelerate the development process. It provides proven development paradigms, which helps save time without having to reinvent patterns every time a problem arises.


2 Answers

It is factory since it is actually "Manufacturing" (Create) an object.

There's actually two kind of factory pattern defined by GoF:

  1. Factory Method
  2. Abstract Factory

Basically both of them tries to decouple the calling client from knowing the actual concrete implementation of the class/interface.

like image 50
xandy Avatar answered Oct 13 '22 00:10

xandy


You can find all you need by reading this PDF article

like image 27
Adnan Avatar answered Oct 13 '22 00:10

Adnan