PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Tue Mar 31, 2020 11:49 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Sun Aug 07, 2011 12:27 pm 
Offline
Forum Newbie

Joined: Sat Jul 16, 2011 12:13 am
Posts: 10
Hi
Can any one explain advantages and disadvantages of factory pattern.


Top
 Profile  
 
PostPosted: Sun Aug 07, 2011 8:29 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13592
Location: New York, NY, US
I think my problem with a Factory is that it create the false sense of adding flexibility while it only seems to add complexity. Where I have implemented them I have eventually removed them.

_________________
(#10850)


Top
 Profile  
 
PostPosted: Fri Sep 09, 2011 11:35 am 
Offline
Forum Contributor
User avatar

Joined: Wed Jun 29, 2005 10:04 pm
Posts: 277
Location: Iowa, USA
The main point of the Factory Pattern in PHP application is to centralize the creation logic to one location. If you simply do new ClassName everywhere, then you have hard coded that into your application and you have to locate and change every instance whenever you would need to change it in the future. If instead you encapsulate this somehow (function, class method, factory object) then you have a single location in your code where you can change this creation logic without affecting the other parts of your code.

The decision to use the pattern is similar to any other design pattern: does the problem you are trying to solve, for which the design pattern is a time tested solution, actually exist in your code? If not, you could be adding unnecessary complexity to your code base for flexibility you will never need. See the YAGNI principal :)


Top
 Profile  
 
PostPosted: Fri Sep 09, 2011 4:32 pm 
Offline
Site Admin
User avatar

Joined: Tue Dec 23, 2003 3:10 am
Posts: 11470
Location: Toronto
Factories play an important role in libraries with lots of dependencies. As sweatje put it, by implementing the factory pattern we can centralize the creation of the object, which understands how to build it in a ready to use state with it's dependencies. With that said, we can much more easily manage and mock factories than manually managing them in the underlying code itself several times over.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group