Solved

php patterns strategy and composite

Posted on 2014-01-31
5
441 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 35

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 34

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 110

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 35

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

728 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