Awais Qarni
Awais Qarni

Reputation: 18006

understanding Factory method pattern

I am reading factory method pattern as I have some issues related to it but I am unable to understand it from core. As per definition stated here

The creation of an object often requires complex processes not appropriate to include within a composing object. The object's creation may lead to a significant duplication of code, may require information not accessible to the composing object, may not provide a sufficient level of abstraction, or may otherwise not be part of the composing object's concerns.

I can understand the concept of duplication of significant code, but I am unable to understand the other concepts like it states

It may require information not accessible to the composing object

How a class can contain the infomation which ic not accessible by composing object. As for as I understand it may be any private datamember of the class. But if any thing is private then how object creation process needs that information? Similarly other two point

It may not provide a sufficient level of abstraction, or may otherwise not be part of the composing object's concerns.

Can any body please here describe these precisely and show my some code stuff so that I can understand the concept

Upvotes: 2

Views: 274

Answers (3)

GETah
GETah

Reputation: 21409

Imagine you are writing an API through which users can create and use a certain object. Internally, in the API framework, you want to register your object in some services, listeners, database...

Here you have two different ways of dealing with the situation:

  • You either let the user create the object and take the responsibility of registering it in the services, listeners and database which should be exposed (public).

OR

  • You want to provide a public factory class that will create the object given certain parameters and will take care of doing all the necessary initialization for you.

The second scenario is the best way to hide all the complexity of creating such objects in your system. This also has a big benefit of hiding the services, listeners and databases needed to register the created object.

Upvotes: 1

Beatles1692
Beatles1692

Reputation: 5320

when it comes to use an external resource in our object there alternatives for its creation come to mind :

  1. To create the object using its constructor
  2. To ask another object to create it for our object (Factory and Factory method pattern) .This way our object doesn't know how to create the external resource but it should know who to ask for it.(it needs to hold a reference to the factory or knows the type of the factory in case of calling a static factory method)
  3. To inject the external resource using an IoC (inversion of control) container.This way our object doesn't to know nothing about neither how to create the external resource nor who is responsible for its creation.Actually this method is making factory patterns obsolete.

Upvotes: 1

kingmaple
kingmaple

Reputation: 4310

The idea of factory pattern is to create load classes and create new objects dynamically. Quite often it is done as a static class (such as here, in the official PHP documentation), but some frameworks use factory pattern as a way of loading objects within MVC objects, for example when you want to load some data in view through a model.

The idea of factory pattern is efficiency and resource management. It loads a file only when it's not been loaded yet and returns the newly created object.

(Note that the example in PHP documentation is not ideal, it would be better to check if the class has been defined and if not, then attempt to include the file instead of using include_once())

Upvotes: 1

Related Questions