PHP
  Home arrow PHP arrow Page 3 - Design Patterns in PHP - Factory Method and Abstract Factory
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Design Patterns in PHP - Factory Method and Abstract Factory
By: David Fells
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 71
    2004-12-20


    Table of Contents:
  • Design Patterns in PHP - Factory Method and Abstract Factory
  • Factory Method
  • Abstract Factory
  • Drawbacks of the Example
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Design Patterns in PHP - Factory Method and Abstract Factory - Abstract Factory
    ( Page 3 of 5 )

    The Abstract Factory pattern provides an interface for creating families of related or dependent objects without specifying their concrete classes [DP87]. This pattern takes the abstraction displayed in the example above to the next level by providing a common factory interface for a given family of objects. The code that actually uses the factory to create objects only expects an object that conforms to the interface of the abstract factory and does not know any details about concrete factory classes.

    Using the example from Design Patterns, consider a game that creates a maze. We have a method that knows what to do to create a maze and it will instantiate all the objects we need to construct the maze - components such as rooms, doors and walls. The example below defines the classes involved - method code is left out deliberately since it is not important to this discussion.

    class MapSite {
      function enter() {}
    }

    define('North', 0);
    define('East', 1);
    define('South', 2);
    define('West', 3);

    class Room extends MapSite {
      var $mapSite = array();
      var $roomNumber;
     
      function Room($roomNumber) {}
      function getSide($direction) {}
      function setSide($direction, $mapSite) {}
    }

    class Wall extends MapSite {
      function Wall() {}
      function enter() {}
    }

    class Door extends MapSite {
      var $room1;
      var $room2;
      var $isOpen;
     
      function Door() {}
      function enter() {}
      function otherSideFrom($room);
    }

    class Maze {
      function Maze() {}
      function addRoom($room) {}
      // Etc…
    }

    class MazeGame {
      function createMaze() {
        $aMaze = new Maze();
        $room1 = new Room(1);
        $room2 = new Room(2);
        $aDoor = new Door($room1, $room2);
       
        $room1->setSide(North, new Wall());
        $room1->setSide(East, $aDoor);
        $room1->setSide(South, new Wall());
        $room1->setSide(West, new Wall());           

        $room2->setSide(North, new Wall());
        $room2->setSide(East, new Wall());
        $room2->setSide(South, new Wall());
        $room2->setSide(West, $aDoor);               
       
        $aMaze->addRoom($room1);
        $aMaze->addRoom($room2);
      }
    }

    In this example, we define a MapSite class that will act as a base class for anything that could apear in a maze, such as a door, a room, or a wall. We then define the constants North, East, South, and West to be used for tracking the sides or orientation of these MapSite objects. Following the constant definitions are the definitions for the Wall, Room and Door classes. These objects should be obvious in intent.

    The setSide() method of the Room class expects the direction of the side and the object to be placed there - any object derived of the class MapSite. This would typically be a Wall or a Door, but it could support more objects easily. The constructor of the Door class expects two Room objects - the door must be aware of the rooms it is connecting. Next we define the Maze class, which is used to represent our actual maze object in code. We use the addRoom() method to attach rooms to the maze.

    Finally, we look at the MazeGame class and its createMaze() method. The createMaze() method creates a maze object, two rooms and a door, then defines what objects occupy the sides of the two rooms and attaches them to our Maze object. At this point, we have successfully created a Maze and put some rooms into it.



     
     
    >>> More PHP Articles          >>> More By David Fells
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    Stay green...Green IT