Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Sep 2012
    Posts
    76
    Thanks
    61
    Thanked 0 Times in 0 Posts

    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.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    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.

  • Users who have thanked Fou-Lu for this post:

    Oatley (10-10-2012)

  • #3
    New Coder
    Join Date
    Sep 2012
    Posts
    76
    Thanks
    61
    Thanked 0 Times in 0 Posts
    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

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •