Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

benefit of having a factory for object creation?

I'm trying to understand the factory design pattern.

I don't understand why it's good to have a middleman between the client and the product (object that the client wants).

example with no factory:

$mac = new Mac();

example with a factory:

$appleStore = new AppleStore();
$mac = $appleStore->getProduct('mac');

How does the factory pattern decouple the client from the product?

Could someone give an example of a future code change that will impact on example 1 negative, but positive in example 2 so I understand the importance of decoupling?

Thanks.

like image 606
never_had_a_name Avatar asked Apr 24 '10 04:04

never_had_a_name


People also ask

What is the benefit of having a factory to make objects?

Advantage of Factory Design Pattern Factory Method Pattern allows the sub-classes to choose the type of objects to create. It promotes the loose-coupling by eliminating the need to bind application-specific classes into the code.

What is the use of an object factory?

Typically, an object factory is quite simple and small. Its main role is to collect the information necessary to create an instance of the intended object's class and then to invoke that class's constructor.

What is the benefit of using a factory class rather than just creating an object directly with new?

The advantage of a Factory Method is that it can return the same instance multiple times, or can return a subclass rather than an object of that exact type.

What is the advantage of factory constructor?

The factory method is a smart way to create objects in Java and provides several advantages over the traditional approach of creating objects using constructors in Java. It can also improve the quality of code by making the code more readable, less coupled, and improves performance by caching.


1 Answers

I think it has to do with the resources needed to construct some types of objects.

Informally, if you told someone to build a Mac, it would be a painstaking process that would take years of design, development, manufacturing, and testing, and it might not be done right. This process would have to be repeated for every single Mac. However, if you introduce a factory, all the hard work can be done just once, then Macs can be produced more cheaply.

Now consider Joomla's factory.php. From what I can tell, the main purpose of JFactory is to pool objects and make sure objects that should be the same aren't copied. For instance, JFactory::getUser() will return a reference to one and only one object. If something gets changed in that user object, it will appear everywhere. Also, note that JFactory::getUser() returns a reference, not a new object. That is something you simply cannot do with a constructor.

Often, you need local context when constructing an object, and that context may persist and possibly take on many forms. For instance, there might be a MySQL database holding users. If User objects are created with a constructor, you'll need to pass a Database object to the constructor (or have it rely on a global variable). If you decide to switch your application to PostgreSQL, the semantics of the Database object may change, causing all uses of the constructor to need review. Global variables let us hide those details, and so do factories. Thus, a User factory would decouple the details of constructing User objects from places where User objects are needed.

When are factories helpful? When constructing an object involves background details. When are constructors better? When global variables suffice.

like image 144
Joey Adams Avatar answered Sep 27 '22 21:09

Joey Adams