Solved

php patterns strategy and composite

Posted on 2014-01-31
5
428 Views
Last Modified: 2014-02-01
Learning oop tutorial by larry ulman

what is example of php strategy pattern and php composite pattern

I already checked php.net
0
Comment
Question by:rgb192
5 Comments
 
LVL 34

Accepted Solution

by:
gr8gonzo earned 300 total points
ID: 39824159
Composite pattern is fairly simple - you've had questions about this before. Composite pattern simply means that instead of having two classes that have 90% of the same code, you use a single class and then either extend that class or bring the differences all into the same class:
<?php
class Jerry
{
  public function SayHelloWorld()
  {
    echo "Hello world!";
  }

  public function SayMyNameIsJerry()
  {
     echo "My name is Jerry!";
  }
}


class Bob
{
  public function SayHelloWorld()
  {
    echo "Hello world!";
  }

  public function SayMyNameIsBob()
  {
     echo "My name is Bob!";
  }
}
?>

Open in new window


Now the composite would be:
<?php
class PersonWhoCanSayHelloWorld
{
  public function SayHelloWorld()
  {
    echo "Hello world!";
  }
}

class Jerry extends PersonWhoCanSayHelloWorld
{
  public function SayMyNameIsJerry()
  {
     echo "My name is Jerry!";
  }
}

class Bob extends PersonWhoCanSayHelloWorld
{
  public function SayMyNameIsBob()
  {
     echo "My name is Bob!";
  }
}

?>

Open in new window


So with composite, you're centralizing / sharing code. You could also combine all the methods into one class that does all three methods.

Strategy pattern simply means that you're providing some kind of parameters or data to the code and you're letting the code figure out what to do. The simplest example of strategy pattern is a simple function that changes its behavior based on the parameter:

function SayMyNameIs($name)
{
  if($name == "Bob")
  {
    echo "My name is Bob and I'm a carpenter!";
  }
  elseif($name == "Jerry")
  {
    echo "My name is Jerry and I drive cars!";
    echo "Vroom! Vroom!";
  }
  else
  {
    echo "I'm not Bob nor Jerry. I wish I were....";
  }
}

SayMyNameIs("Bob");
SayMyNameIs("Jerry");
SayMyNameIs("Alice");

Open in new window


Inside the context of OOP, you can use objects to do more drastic changes, because objects can have their own methods and all the means to do vastly-different things, all in a very modular and organized manner:

class Bob
{
   public function DoYourJob()
   {
     $H = new Hammer();
     $N = new Nails();
     $W = new WoodPlanks();
     $this->_buildDesk($H, $N, $W);     
   }

  private function _buildDesk($Hammer,$Nails,$WoodPlanks)
  {
      ... code that builds a desk ...
  }
}

class Jerry
{
   public function DoYourJob()
   {
     $C = new Car();
     $C->DriveTo("123 Main Street");
   }
}

class WorkForce
{
   public $Workers = array();
   public function GoToWork()
   {
      foreach($Workers as $Worker)
      {
         $Worker->DoYourJob();
      }
   }
}

$WF = new WorkForce();
$WF->Workers[] = new Bob();
$WF->Workers[] = new Jerry();
$WF->GoToWork();
?>

Open in new window


In that example, Bob and Jerry both do very different things, but the parent class doesn't care. It's telling all of the workers to do the same method ("DoYourJob"), and it leaves it up to them to do the correct thing.
0
 
LVL 33

Assisted Solution

by:Slick812
Slick812 earned 150 total points
ID: 39824627
greetings rgb192, the two things in this question "strategy pattern and composite pattern" are ways to deal with expanding code additions (as Classes) as you develop a web site or web app, The way I approach it is like this - I have a Goal to build a web site that will do a specific display (job) with many pages, according to a design we have agreed upon, or have been given. Then start the development with 2 or 3 Classes, for the home page (first page). Then the next page or pages are started to develop, and 2 more Classes are added to do the work for the new pages, then other pages are developed and 3 more Classes are added for those pages that do different PHP work required as needed (while using some of the previous Classes also). And at this point I can see that 3 of the Classes have some (maybe many) of the same kinds of functions, that do the same or similar work, So at that point I decide to group together code by Making a Parent class with all of the common functions, and then making Class extensions to add the Non-Common methods and properties, (this might be the "composite pattern" to make extensions), But I never, never, ever think about the term "composite pattern", what I do think about is reducing my work load and time spent, by grouping together ALL CODE POSSIBLE to make my development faster, with less code to write, , as the Parent class can do one code write, and then used that in ALL of the extended classes, one write many uses, save time writing code.
I can not start a development project and know before hand what php work I will need to do in the Classes, so the "strategy pattern and composite pattern" are things that I usually do with code during development NOT before development! But I do not use those names, I just know some methods (like Factory Class method) and others to make the project go faster, with less code to write. But there are many different "Views" on how to do project Class development for better code production.
0
 
LVL 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 50 total points
ID: 39826217
Nobody could add much to what @gr8gonzo has already written here, since it is an exceptionally good response and example, but since you have asked about class abstraction before, consider the role of an abstract class in the composite design pattern.  There would never be any need to instantiate class PersonWhoCanSayHelloWorld because it takes no input arguments.  You would just extend it into a more specific person object.
0
 
LVL 34

Expert Comment

by:gr8gonzo
ID: 39826709
Or, for that matter, interfaces with the strategy pattern (since you are dealing with different classes but you want them all to be able to have the DoYourJob method).
0
 

Author Closing Comment

by:rgb192
ID: 39826908
I like gr8gonzo code samples and explanations the best.
followup
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28354154.html

I agree with slick, when bosses give me work, I just use procedural but for learning I think of design patterns.

Thanks
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question