Home arrow PHP arrow Page 3 - The Basics of Abstract Factory Classes in PHP 5

Creating small and large DIV objects - PHP

You have probably used the factory design pattern before. An abstract factory pattern helps you make sure you're creating the correct objects for your application according to the context. This article, the first one in a three-part series, gives you a taste of what you can do with the abstract factory pattern. As always, it includes plenty of examples.

TABLE OF CONTENTS:
  1. The Basics of Abstract Factory Classes in PHP 5
  2. Introducing the abstract factory pattern: defining an abstract web page element factory
  3. Creating small and large DIV objects
  4. Seeing the abstract factory pattern in action
By: Alejandro Gervasio
Rating: starstarstarstarstar / 8
January 24, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

After defining all the concrete factory classes that you learned in the previous section, the next step consists simply of creating a new set of classes that logically display small and large DIV elements. Of course, the pertinent definitions for these brand new classes are easy to follow, and are listed below. So, have a look at them, please:

// define abstract 'DivElement' class
abstract class DivElement{
   abstract function display();
}
// define concrete 'SmallWhiteDivElement' class
class SmallWhiteDivElement extends DivElement{
   private $style='width: 100px; background: #fff; border: 1px solid #999;';
   private $content='This is the content of the small DIV element';
   public function display(){
     return '<div style="'.$this->style.'">'.$this-
>content.'</div>';                      
   }
}
// define concrete 'SmallBlackDivElement' class
class SmallBlackDivElement extends DivElement{
   private $style='width: 100px; background: #000; color: #fff; border: 1px solid #999;';
   private $content='This is the content of the small DIV element';
   public function display(){
     return '<div style="'.$this->style.'">'.$this-
>content.'</div>';                      
   }
}
// define concrete 'LargeWhiteDivElement' class
class LargeWhiteDivElement extends DivElement{
   private $style='width: 500px; background: #fff; border: 1px solid #999;';
   private $content='This is the content of the large DIV element';
   public function display(){
     return '<div style="'.$this->style.'">'.$this-
>content.'</div>';                      
   }
}
// define concrete 'LargeBlackDivElement' class
class LargeBlackDivElement extends DivElement{
   private $style='width: 500px; background: #000; color: #fff; border: 1px solid #999;';
   private $content='This is the content of the large DIV element';
   public function display(){
     return '<div style="'.$this->style.'">'.$this-
>content.'</div>';                      
   }
}

As illustrated above, the four previous classes are responsible for returning (to calling code) the correct (X)HTML markup that renders a specific DIV element. In the first two cases, the aforementioned classes will display small DIVs, while on the other occasions, only large DIVs will be created. Pretty simple, right?

All right, now you have learned how to create an abstract factory class which is capable of indicating what DIV objects should be spawned by the respective concrete factories, in accordance with their corresponding contexts. Nevertheless, I believe firmly that you'll understand the functionality of all these classes more easily if I set up an illustrative example where they can be put to work conjunctly.

Taking this fact into account, I suggest that you go ahead and read the next few lines, since you're just about to see the real power of the abstract factory pattern!



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

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: