View Full Version : Factory design pattern?

10-10-2012, 07:19 PM
Hello all, I'm currently reading about the factory design pattern, and I understand it, whereby you can instanciate a class within a class like so

Class Factory {

public function doSomething($value) {
switch($value) {
case 'A' : return new ClassA; break;
case 'B' : return new ClassB; break;


$obj = new Factory();

However, I'm not too sure as and when to use it.

Say I was building something like a website or something involved in creating a website element, how could I use it here? Can anyone help me with an example to see if I can figure out how to use it in my projects?

Is it used often in projects?

Thank you

10-10-2012, 07:55 PM
Factories are simply convenience handling to let you build based on configuration data instead of hard coded structural data.
They only have real value if both ClassA and ClassB have a common ancestor (whilst used in a hard coded fashion like this). These objects should interface a specific type so that both can be used generically as that type. This way it doesn't matter which you get, so long as you get something that can be understood. Something like a storage engine would use a factory to construct its type based on a common interface. So if I made an IStorage interface and then wrote drivers for SQLServer, MySQLi, PDO, binary data files, XML, whatever, it doesn't make a difference what the work it does is, I know that I can ask the result of the factory for the $obj->query() method. It would be up to the drivers to do what they need to do, and give me something back that would be consistent regardless of the driver.

If they don't share a common interface or ancestor class, then its usefulness is literally a "convenience" and nothing more. I use convenience loosely here; I don't believe any IDE would be able to detect what that type of object is based on string input, and therefore would have no way to provide autocompletion. At least with common interface I can typehint the returns off of it and my IDE can continue with autocompletion on the results.

10-10-2012, 09:39 PM
Thanks that really helps. I've seen lots of examples already of the factory pattern being used to load different drivers as you say. That seems to be the example used in a lot of tutorials for the factory pattern. Are there any other common things it could be used for, any real world examples would be great. Thanks

10-10-2012, 11:12 PM
I use a factory to create my users. Since I took the approach to mirror windows ACL as closely as possible, I have a principal, users and groups. A principal is neither a user nor a group, it is simply an identity object, but both users and groups are principal. This lets me assign ACL privileges to either users or groups since they are specified as principals and not users or groups.
Given an id, I can't tell what the principal is until its been factoried. It wraps its type for construction based on what the claimed instanceof object is stored in the database. When it comes to security with the ACL though, it doesn't make a difference what the principal is.