Abstract Factory:
A software design pattern, the Abstract Factory Pattern provides a way to encapsulate a group of individual factories that have a common theme. In normal usage, the client software would create a concrete implementation of the abstract factory and then use the generic interfaces to create the concrete objects that are part of the theme. The client does not know (or care) about which concrete objects it gets from each of these internal factories since it uses only the generic interfaces of their products. This pattern separates the details of implementation of a set of objects from its general usage.
How to use it:
Class diagram of Amazon Online Book Shop:
In this example we have an abstract factory, AbstractBookFactory, that specifies two classes, AbstractPHPBook and AbstractMySQLBook, which will need to be created by the concrete factory.
The concrete class OReillyBookfactory extends AbstractBookFactory, and can create the OReillyMySQLBook and OReillyPHPBook classes, which are the correct classes for the context of OReilly. Thanks goes to FluffyCat.com as their article helps me a lot.
Examples Codes given below:
AbstractBookFactory.php
abstract class AbstractBookFactory {
abstract function makePHPBook();
abstract function makeMySQLBook();
}
download
source, use right-click and “Save Target As…” to save with a .php extension.
OReillyBookFactory.php
include_once('AbstractBookFactory.php');
include_once('OReillyPHPBook.php');
include_once('OReillyMySQLBook.php');
class OReillyBookFactory extends AbstractBookFactory {
private $context = "OReilly";
function makePHPBook() {return new OReillyPHPBook;}
function makeMySQLBook() {return new OReillyMySQLBook;}
}
download
source, use right-click and “Save Target As…” to save with a .php extension.
SamsBookFactory.php
include_once('AbstractBookFactory.php');
include_once('SamsPHPBook.php');
include_once('SamsMySQLBook.php');
class SamsBookFactory extends AbstractBookFactory {
private $context = "Sams";
function makePHPBook() {return new SamsPHPBook;}
function makeMySQLBook() {return new SamsMySQLBook;}
}
download
source, use right-click and “Save Target As…” to save with a .php extension.
AbstractBook.php
abstract class AbstractBook {
abstract function getAuthor();
abstract function getTitle();
abstract function getPicture();
}
download
source, use right-click and “Save Target As…” to save with a .php extension.
AbstractMySQLBook.php
include_once('AbstractBook.php');
abstract class AbstractMySQLBook {
private $subject = "MySQL";
}
download
source, use right-click and “Save Target As…” to save with a .php extension.
OReillyMySQLBook.php
include_once('AbstractMySQLBook.php');
class OReillyMySQLBook extends AbstractMySQLBook {
private $author;
private $title;
private $picture;
function __construct() {
$this->author = 'George Reese, Randy Jay Yarger, and Tim King';
$this->title = 'Managing and Using MySQL';
$this->picture = 'images/book3.jpg';
}
function getAuthor() {return $this->author;}
function getTitle() {return $this->title;}
function getPicture() {return $this->picture;}
}
download
source, use right-click and “Save Target As…” to save with a .php extension.
SamsMySQLBook.php
include_once('AbstractMySQLBook.php');
class SamsMySQLBook extends AbstractMySQLBook {
private $author;
private $title;
private $picture;
function __construct() {
$this->author = 'Paul Dubois';
$this->title = 'MySQL, 3rd Edition';
$this->picture = 'images/book6.jpg';
}
function getAuthor() {return $this->author;}
function getTitle() {return $this->title;}
function getPicture() {return $this->picture;}
}
download
source, use right-click and “Save Target As…” to save with a .php extension.
AbstractPHPBook.php
include_once('AbstractBook.php');
abstract class AbstractPHPBook {
private $subject = "PHP";
}
download
source, use right-click and “Save Target As…” to save with a .php extension.
OReillyPHPBook.php
include_once('AbstractPHPBook.php');
class OReillyPHPBook extends AbstractPHPBook {
private $author;
private $title;
private $picture;
private static $oddOrEven = 'odd';
function __construct() {
//alternate between 2 books
if ('odd' == self::$oddOrEven) {
$this->author = 'Rasmus Lerdorf and Kevin Tatroe';
$this->title = 'Programming PHP';
$this->picture = 'images/book2.jpg';
self::$oddOrEven = 'even';
} else {
$this->author = 'David Sklar and Adam Trachtenberg';
$this->title = 'PHP Cookbook';
$this->picture = 'images/book1.jpg';
self::$oddOrEven = 'odd';
}
}
function getAuthor() {return $this->author;}
function getTitle() {return $this->title;}
function getPicture() {return $this->picture;}
}
download
source, use right-click and “Save Target As…” to save with a .php extension.
SamsPHPBook.php
include_once('AbstractPHPBook.php');
class SamsPHPBook extends AbstractPHPBook {
private $author;
private $title;
private $picture;
function __construct() {
//alternate randomly between 2 books
mt_srand((double)microtime()*10000000);
$rand_num = mt_rand(0,1);
if (1 > $rand_num) {
$this->author = 'George Schlossnagle';
$this->title = 'Advanced PHP Programming';
$this->picture = 'images/book4.jpg';
} else {
$this->author = 'Christian Wenz';
$this->title = 'PHP Phrasebook';
$this->picture = 'images/book5.jpg';
}
}
function getAuthor() {return $this->author;}
function getTitle() {return $this->title;}
function getPicture() {return $this->picture;}
}
download
source, use right-click and “Save Target As…” to save with a .php extension.
testAbstractFactory.php
<?php
include_once('OReillyBookFactory.php');
include_once('SamsBookFactory.php');
echo tagins("html");
echo tagins("head");
echo tagins("/head");
echo tagins("body");
//
echo '<img src="images/header.gif" width="100%">';
//
echo "<h3>Amazon Online Book Shop</h3>";
echo tagins("br");
echo 'All OReillyBooks'.tagins("br");
$bookFactoryInstance = new OReillyBookFactory;
testConcreteFactory($bookFactoryInstance);
echo tagins("br");
echo 'All SamsBooks Factory'.tagins("br");
$bookFactoryInstance = new SamsBookFactory;
testConcreteFactory($bookFactoryInstance);
echo tagins("br");
echo "";
echo tagins("br");
//
echo '<img src="images/footer-toptile.gif">';
//
echo tagins("/body");
echo tagins("/html");
function testConcreteFactory($bookFactoryInstance) {
$phpBookOne = $bookFactoryInstance->makePHPBook();
echo '<img src="'.$phpBookOne->getPicture().'">'.tagins("br");
echo 'Author: '.
$phpBookOne->getAuthor().tagins("br");
echo 'Title: '.
$phpBookOne->getTitle().tagins("br");
//
echo '<p class="MsoNormal" style="text-align: center;
line-height: 12pt; margin-top: 2px;
margin-bottom: 2px;"><span style="font-family: Verdana;
font-size: xx-small;"> <a
href="http://www.edutechindia.org/currency.asp?id=1&catid=
1&p=m"> Add to Cart
</a></span></p>';
//
$phpBookTwo = $bookFactoryInstance->makePHPBook();
echo '<img src="'.$phpBookTwo->getPicture().'">'.tagins("br");
echo 'Author: '.
$phpBookTwo->getAuthor().tagins("br");
echo 'Title: '.
$phpBookTwo->getTitle().tagins("br");
//
echo '<p class="MsoNormal" style="text-align: center;
line-height: 12pt; margin-top: 2px;
margin-bottom: 2px;"><span style="font-family: Verdana;
font-size: xx-small;"> <a
href="http://www.edutechindia.org/currency.asp?id=1&catid=
1&p=m"> Add to Cart
</a></span></p>';
//
$mySqlBook = $bookFactoryInstance->makeMySQLBook();
echo '<img src="'.$mySqlBook->getPicture().'">'.tagins("br");
echo 'MySQL Author: '.
$mySqlBook->getAuthor().tagins("br");
echo 'MySQL Title: '.
$mySqlBook->getTitle().tagins("br");
//
echo '<p class="MsoNormal" style="text-align: center;
line-height: 12pt; margin-top: 2px;
margin-bottom: 2px;"><span style="font-family: Verdana; font-size:
xx-small;"> <a
href="http://www.edutechindia.org/currency.asp?id=1&catid=
1&p=m"> Add to Cart
</a></span></p>';
//
}
//doing this so code can be displayed without breaks
function tagins($stuffing) {
return "<".$stuffing.">";
}
?>
download
source, use right-click and “Save Target As…” to save with a .php extension.
output of testAbstractFactory.php
Filed under: design pattern, PHP | Tagged: Abstract Factory, concrete objects, design pattern, factories, Interfaces, PHP5 | 1 Comment »