Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 469
  • Last Modified:

php patterns strategy and composite

Learning oop tutorial by larry ulman

what is example of php strategy pattern and php composite pattern

I already checked php.net
0
rgb192
Asked:
rgb192
3 Solutions
 
gr8gonzoConsultantCommented:
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
 
Slick812Commented:
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
 
Ray PaseurCommented:
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
 
gr8gonzoConsultantCommented:
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
 
rgb192Author Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now