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.
Next: Drawbacks of the Example >>
More PHP Articles
More By David Fells