Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-10-2012, 06:19 PM   PM User | #1
Oatley
New Coder

 
Join Date: Sep 2012
Posts: 70
Thanks: 56
Thanked 0 Times in 0 Posts
Oatley is an unknown quantity at this point
Factory design pattern?

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

PHP Code:
Class Factory {

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

}

$obj = new Factory();
$obj->doSomething('B'); 
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

Last edited by Oatley; 10-10-2012 at 06:21 PM..
Oatley is offline   Reply With Quote
Old 10-10-2012, 06:55 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,648
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
Oatley (10-10-2012)
Old 10-10-2012, 08:39 PM   PM User | #3
Oatley
New Coder

 
Join Date: Sep 2012
Posts: 70
Thanks: 56
Thanked 0 Times in 0 Posts
Oatley is an unknown quantity at this point
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
Oatley is offline   Reply With Quote
Old 10-10-2012, 10:12 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,648
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:03 PM.


Advertisement
Log in to turn off these ads.