PHP Hacks Free Open Book

PHP Hacks

Previous Page
Next Page

Hack 71. Separate What from How with Strategies

Use the Strategy pattern to abstract the code that traverses structures from the code that operates on those structures.

You use the Strategy pattern to abstract the processing of an object, allowing how an object is processed to be separated from where the object is located.

In this hack, I'll use a car chooser as an example. This code will recommend a car based on some search criteria. In this case, I will provide my specs for an ideal car and let the code pick the car that most closely matches my dream specs. The value of the Strategy pattern is that I can alter the car comparison code independently of the car selection code.

The UML for this hack is shown in Figure 7-5. The CarChooser object uses a CarWeighter object to compare each Car to the ideal model. Then the best Car match is returned to the client.

7.6.1. The Code

Save the code in Example 7-5 as strategy.php.

Figure 7-5. The relationship between the CarChooser, CarWeighter, and Car objects


Example 7-5. An example of the Strategy pattern
	<?php
	class Car
	{
	  public $name;
	  public $speed;
	  public $looks;	
	  public $mileage;
	  public function Car( $name, $speed, $looks, $mileage )
	  {
	    $this->name = $name;
		$this->speed = $speed;
		$this->looks = $looks;
		$this->mileage = $mileage;
	  }
	}	

	class CarWeighter
	{
	  private function diff( $a, $b )
	  {
	    return abs( $a - $b );
	  }
	  
	  public function weight( $a, $b )
	  {
	    $d = 0;
		$d += $this->diff( $a->speed, $b->speed );
		$d += $this->diff( $a->looks, $b->looks );
		$d += $this->diff( $a->mileage, $b->mileage );
		return ( 0 - $d );
	  }
	}

	class CarChooser
	{
	  private $ideal;
	  private $alg;

	  function CarChooser( $ideal, $alg )
	  {
	    $this->ideal = $ideal;
		$this->alg = $alg;
  	  }
	  
	  public function choose( $carlist )
	  {
	    $minrank = null;
		$found = null;
		$alg = $this->alg;
		
		foreach( $carlist as $car )
		{
		  $rank = $alg->weight( $this->ideal, $car );
		  if ( !isset( $minrank ) ) $minrank = $rank;
		  if ( $rank >= $minrank )
		  {
		    $minrank = $rank;
			$found = $car;
		  }
		}

		return $found;
	  }
	}
	
	function pickCar( $car )
	{
  	  $carlist = array();
	  $carlist []= new Car( "zippy", 90, 30, 10 );
	  $carlist []= new Car( "mom'n'pop", 45, 30, 55 );
	  $carlist []= new Car( "beauty", 40, 90, 10 );
	  $carlist []= new Car( "enviro", 40, 40, 90 );

	  $cw = new CarWeighter();
	  $cc = new CarChooser( $car, $cw );
	  $found = $cc->choose( $carlist );
	  echo( $found->name."\n" );
	}

	pickCar( new Car( "ideal", 80, 40, 10 ) );
	pickCar( new Car( "ideal", 40, 90, 10 ) );
	?>

Starting at the top of the file, I define the Car class, which holds the car name and the metrics for speed, looks, and mileage. Each is rated from 0 to 100 (largely to make the math easy). Then comes the CarWeighter, which compares two cars and returns a comparison metric. Finally, there's the CarChooser, which uses a CarWeighter to select the best car based on some input criteria. The pickCar( ) function creates a set of cars and then uses a CarChooser to choose the car from the list that best fits the criteria (passed in via another Car object).

The test code at the bottom of the file then asks for two carsone that is heavily weighted in the speed category, and another that is strongly weighted on looks.

7.6.2. Running the Hack

You run this hack on the command line with the PHP interpreter:

	% php strategy.php
	zippy
	beauty

The output shows that the car recommended to me if I want speed is the "zippy" car: a good approximation. The car recommended to me if I want something a little sexier is the "beauty" car. Excellent!

The code that deduces whether a car is a good match is totally abstracted away from the code that traverses the car list and picks one from that list. You can change the algorithm that weights a certain car independently of the code that picks which is the right car from the weighted list. For example, you can add the cars that have interested you recently or that you have owned in the past into the weighting algorithm. Or you can change the picker code to select the top three and provide a choice among them.

Previous Page
Next Page
Index: [SYMBOL][A][B][C][D][E][F][G][H][I][J][K][L][M][N][O][P][Q][R][S][T][U][V][W][X][Y][Z]


     Main Menu
PHP Hacks
Table of Contents
Copyright
Credits
Preface
Chapter 1.  Installation and Basics
Chapter 2.  Web Design
Chapter 3.  DHTML
Chapter 4.  Graphics
Chapter 5.  Databases and XML
Chapter 6.  Application Design
Chapter 7.  Patterns
Section 7.1.  Hacks 6778: Introduction
Hack 67. Observe Your Objects
Hack 68. Create Objects with Abstract Factories
Hack 69. Flexible Object Creation with Factory Methods
Hack 70. Abstract Construction Code with a Builder
Hack 71. Separate What from How with Strategies
Hack 72. Link Up Two Modules with an Adapter
Hack 73. Write Portable Code with Bridges
Hack 74. Build Extensible Processing with Chains
Hack 75. Break Up Big Classes with Composites
Hack 76. Simplify APIs Using a Façade
Hack 77. Create Constant Objects with Singletons
Hack 78. Ease Data Manipulation with Visitors
Chapter 8.  Testing
Chapter 9.  Alternative UIs
Chapter 10.  Fun Stuff
Colophon
Index


More Books
PHP Hacks
Processing Xml With Java - A Guide To Sax, Dom, Jdom, Jaxp, And Trax
The Koran (Holy Qur'an)
Macromedia Flash 8 Bible
Search Engine Optimization for Dummies
YouTube Traffic
PHP 5 for Dummies
Harry Potter and The Chamber of Secrets
Harry Potter and the Sorcerer's Stone
The Pilgrim's Progress
Wireless Hacks
Flash Hacks. 100 Industrial-Strength Tips & Tools
PayPal Hacks. 100 Industrial-Strength Tips and Tools
Amazon Hacks
Pdf Hacks
The Da Vinci Code
Google Hacks
The Holy Bible
Windows XP For Dummies
Harry Potter and the Half-Blood Prince
Seo Book
Upgrading and Repairing Networks
Macromedia Dreamweaver 8 UNLEASHED
Windows XP Annoyances
Windows XP Hacks
Microsoft Windows XP Power Toolkit
Teach Yourself MS Office In 24Hours
iPod & iTunes Missing Manual
PC Hacks 100 Industrial-Strength Tips and Tools
PC Overclocking, Optimization, and Tuning - 2th Edition
PC Hardware In A Nutshell 3rd Edition
PC Hardware in a Nutshell, 2nd Edition
Upgrading and Repairing PCs
Google for Dummies
MySQL Cookbook
Teach Yourself Macromedia Flash 8 In 24 Hours
PHP CookBook
Sams Teach Yourself JavaScript in 24 Hours
PHP5 Manual
Free Games Paper Airplanes
500 Juegos Gratis 500 Giochi Gratis 500 Jeux Gratuits 500 Jogos Gratis 500 Kostenlose Spiele