Solved

php patterns strategy and composite

Posted on 2014-01-31
5
422 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
Comment Utility
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
Comment Utility
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 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 50 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

772 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now