phpeveryday.com

The best tutorial of php, php framework, php strategies, object oriented oriented,


OOP Pattern - Factory: External Factory Class

Tag: oop, pattern, factory pattern, external class   Category: PHP Classes
post: 18 Feb 2008 read: 269


PHP Factory Pattern Step By Step Tutorial - Part 2: We have learned creating factory within the object itself at previous post. In this post, we will look how to implement factory pattern with external class. Please look following code:

<?
class Vehicle{

  function category($wheel = 0){
  	if($wheel == 2){
		return "Motor";
	}elseif($wheel == 4){
		return "Car";
	}
  }
}

class GetVehicle{
  
  function get($wheel){
    return Vehicle::category($wheel);
  }
}

class Spec{
	
  var $wheel = '';
  var $vc = '';
  	
  function Spec($wheel){
	$this->wheel = $wheel;	
	$this->vc =& GetVehicle::get($wheel);
  }
 	
  function name(){
  	return $this->vc;
  }

  function brand(){
	if($this->vc == "Motor"){
		return array('Harley','Honda');
	}elseif($this->vc = "Car"){
		return array('Nisan','Opel');	
	} 
  }
	
}

$kind = new Spec(2);
echo "Kind: ".$kind->name();
echo "<br>";
echo "Brand: ".implode(",",$kind->brand());
?>
using external factory class


Series this article:
OOP Pattern - Factory: Simple Factory Pattern
OOP Pattern - Factory: External Factory Class
OOP Pattern - Factory: Saving Energy for Repeating Steps

| Give Your Opinion | Recommend
Share and Bookmark to: These icons link to social bookmarking sites where readers can share and discover new web pages.
digg del.icio.us technorati Ma.gnolia BlinkList

Recommended articles by other readers:
Web Services: How PHP Kiss VB.NET? (Part 1)
Chart: How to Build Cool Animation Real Time Chart
Joomla: Fast Road to Understand Component Programming
Email: Send Attachement Mail
mod_rewrite - Part 1: create your "fantasy" URL

What do You Think?
Your Name *:
Email *:
(Will not be published)
Website/URL:
Your Comment *:
* Required


615
posting