developer.com
Search EarthWeb
CodeGuru | Gamelan | Jars | Wireless | Discussions
Navigate developer.com
Architecture & Design  
Database  
Java
Languages & Tools
Microsoft & .NET
Open Source  
Project Management  
Security  
Techniques  
Voice  
Web Services  
Wireless/Mobile
XML  
New
 
Technology Jobs  

   Developer.com Webcasts:
  The Impact of Coding Standards and Code Reviews

  Project Management for the Developer

  Defining Your Own Software Development Methodology

  more Webcasts...




See The Winners!




Developer Jobs

Be a Commerce Partner














 


Developer News -
Why Firefox Doesn't Take Google Chrome Features    June 26, 2009
First Major PHP Update in Years Coming Soon    June 25, 2009
Red Hat CEO Calls on Oracle to Keep Java Open    June 25, 2009
Google Widens AdSense for iPhone, Android Apps    June 24, 2009
Free Tech Newsletter -

Implementing Patterns within PHP
By W. Jason Gilmore

Go to page: Prev  1  2  3  

Factory Method

Often, a particular application feature can take on a variety of forms. For example, suppose that your Web application offered a letter-writing wizard. Two letter formats are offered: formal and casual. Because its entirely up to the user as to which format is used, instantiation can only take place at runtime. However, how does our system know which class has been instantiated? You might be surprised to know that it doesn't even have to know.

The factory method offers a unified point for instantiating any of several classes, abstracting the process away from the system. These classes implement a certain set of methods which are made known to the system (either via an abstract class or an interface). By forcing this constraint, we can be rest assured that provided an object, the methods executed against it by the system will correspond accordingly. If we later decide to add additional implementations, for example a cover letter and memo format, all we need to do is modify the factory class accordingly. Listing 1-2 implements the letter-wizard application.

Listing 1-2. A PHP-based Factory Method Implementation

<?php

interface IDocument
{
   function outputHeader();
   function outputBody();
   function outputFooter();
}

class Formal implements IDocument
{
   public function outputHeader() {
      $header = "<p>".date("F d, Y")."</p>";
      $header .= "<p>Dear Sir or Madam:</p>";
      return $header;
   }

   public function outputBody($body) {
      $body = str_replace(":-)","",$body);
      $body = "<p>".$body."</p>";
      return $body;
   }

   public function outputFooter($name) {
      return "Sincerely,<br />$name";
   }

}

class Casual implements IDocument 
{
   public function outputHeader() {
      return "What's up!<br />";
   }

   public function outputBody($body) {
      $body = "<p>".$body."</p>";
      return $body;
   }

   public function outputFooter($name) {
      return "Cya,<br />$name";
   }

}

class DocumentMaker {

   function create($type) {
      switch($type) {
         case 'formal' :
            $doctype = new Formal();
            break;
         case 'casual' :
            $doctype = new Casual();
            break;
         default:
            $doctype = new Casual();
      }
      return $doctype;
   }
}

$docmaker = new DocumentMaker();
$docobj = $docmaker->create("formal");

echo $docobj->outputHeader();
echo $docobj->outputBody("Thank you for lunch today. I appreciate it. :-)");
echo $docobj->outputFooter("Jason");

?>

Passing "casual" into $docmaker create() method yields:

What's up!
Thank you for lunch today. I appreciate it. :-)
Cya,
Jason

Passing "formal" into the $docmaker create() method yields:

April 15, 2004
Dear Sir or Madam:
Thank you for lunch today. I appreciate it. 
Sincerely,
Jason

Conclusion

Doing anything cool with PHP and design patterns? Have a question or comment? E-mail me at jason@wjgilmore.com. I'd also like to hear more about your general experience experimenting with PHP 5! Thanks to Mehran Habibi for his valuable input on this article.

About the Author

W. Jason Gilmore (http://www.wjgilmore.com/) is the Open Source Editorial Director for Apress (http://www.apress.com/). He's the author of the upcoming book, PHP 5 and MySQL: Novice to Pro, due out by Apress in 2004. His work has been featured within many of the computing industry's leading publications, including Linux Magazine, O'Reillynet, Devshed, Zend.com, and Webreview. Jason is also the author of A Programmer's Introduction to PHP 4.0 (453pp., Apress). Along with colleague Jon Shoberg, he's co-author of "Out in the Open," a monthly column published within Linux magazine.

Go to page: Prev  1  2  3  


Tools:
Add www.developer.com to your favorites
Add www.developer.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed


Architecture & Design Archives






internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs