PHP
  Home arrow PHP arrow Page 3 - Design Patterns in PHP - Factory Metho...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
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: 5 stars5 stars5 stars5 stars5 stars / 62
    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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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


       · I'm pretty new to Design Patterns in General. On the first page, the two described...
       · DP is a reference to the book "Design Patterns". 87 and 107 are the pages where the...
       · To be more...
       · I see, thanks! ...
       · I really enjojed this article may be you do do some more on other patterns
       · My plan is to write a series on Design Patterns in PHP but at the moment, article...
       · On page 4/5 of the article, the methods in MazeFactory class are createMaze,...
       · Great article. I gave it 5 stars.Something I'm working on right now will have a...
       · very good article. Helped me understand the difference and see the benfits of the 2...
       · this article is really useful for those who are new to design patterns.
       · Very good article, I really understood something from it.
     

       

    PHP ARTICLES

    - Validating Web Forms with the Code Igniter P...
    - Output Buffering
    - Paginating Database Records with the Code Ig...
    - HTTP Headers in Web Development
    - Project Management: Administration
    - Building a Database-Driven Application with ...
    - User Authentication for a Project Management...
    - Introduction to the CodeIgniter PHP Framework
    - Adding Users for a Project Management Applic...
    - Migrating Class Code for a MIME Email to PHP...
    - Login and Logout Authentication for a Projec...
    - Composing Messages in HTML for MIME Email wi...
    - Project Management: Authentication
    - A Better Way to Determine MIME Types for MIM...
    - Project Management Overview





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway