Home arrow PHP arrow Page 3 - Design Patterns in PHP - Factory Method and Abstract Factory

Abstract Factory - PHP

Normally, in object oriented programming, object creation is not difficult. But what if your object needs to be created based on different conditions or other matters of context? Then you will spend hours in debugging and updating--unless you know about design patterns. David Fells explains how they work, and uses the creation of a maze to illustrate his points.

TABLE OF CONTENTS:
  1. Design Patterns in PHP - Factory Method and Abstract Factory
  2. Factory Method
  3. Abstract Factory
  4. Drawbacks of the Example
  5. Conclusion
By: David Fells
Rating: starstarstarstarstar / 79
December 20, 2004

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: