Solved

where is the strategy in this strategy design pattern

Posted on 2014-02-16
8
231 Views
Last Modified: 2014-02-23
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28354154.html#a39857956


output

... code that builds a desk ...Okay, we're driving to 123 Main Street


where is the strategy
I thought strategy was 'if statement'
how is code different 'if something' happens
0
Comment
Question by:rgb192
  • 3
  • 3
  • 2
8 Comments
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 200 total points
ID: 39864466
Please read this description:
http://en.wikipedia.org/wiki/Strategy_pattern

The strategy pattern allows the programmer to have multiple "strategies" for handling an specific input, event or output.  Consider the example of similar data that requires some special handling.  Let's say you have clients with different levels of authorization.  Some are allowed to upload and store large image files, and others are allowed only small files.  A strategy pattern might handle all file uploads the same way, but then use a lookup to determine the authorization level of the user, and if she is a "small" user, the script would resize the image files before storing them.  The script knows which strategy to follow, based on some external signal.  In this case, it's the authorization level associated with the client identity.

In the case of the Workforce example, let's say your script got a signal that it needed to build three desks, but did not need anyone to drive a car.  Look at the totality of the PHP script and count the number of lines in this snippet (#lines = 66).  Now consider how many lines would have to change to respond to the request for 3 desks and no car (#lines = 3).  That's the power of object-oriented programming in action!
0
 
LVL 33

Assisted Solution

by:Slick812
Slick812 earned 300 total points
ID: 39865527
rgb192, It is my opinion that there is no clear strategy, of any kind, in your Class code for the WorkForce Class and other Class definitions there. It just looks like some example to show how to write a Class definitions in PHP. Please consider, The very word "strategy" means to "Plan and describe a Path to a Goal" or to describe a series of ways-methods needed to get a correct code result, , , most of the time in Classes, this planing is ALSO set to CHANGE the output result of the Class methods for Different Input to the properties-methods of that Class, - - - - SO if something "different" is input to the Class-Object, then the output changes,

In the entire code there, I can not see one single  "IF STATEMENT" or "SWITCH STATEMENT", as far as I can tell the only variance you can have (thought strategy was 'if statement'), , is to load a different Worker as Bob or Jerry Class as -
$WF->Workers[] = new Bob();
$WF->Workers[] = new Jerry();
$WF->Workers[] = new Jerry();
$WF->Workers[] = new Bob();  // I have added EXTRA workers for example

Bob class builds a DESK as Job, Jerry class drives a CAR as job, and this is supposed to be shown in the output as -

        code that builds a desk  // Bob job
        ...Okay, we're driving to 123 Main Street  // Jerry job

But this seems like a very POOR teaching example for Class code work, and does not help in understanding any Class "Structuring" to have GOOD interactive Class Definitions, , as in Work-Force and Workers. Also, you seem to be confused about most everything in this teaching example, I believe you were suppose to finish the code for the Bob Class in this -
  private function _buildDesk($Hammer,$Nails,$WoodPlanks)
  {
      echo'<br>... code that builds a desk ...';
  }

and place some code in the class Hammer,  class Nails, and  class WoodPlanks and then USE these objects in
  private function _buildDesk($Hammer,$Nails,$WoodPlanks)
  {
      echo'<br>'. $WoodPlanks->cutWood(). $Nails->GetNails('2-inch'). 
        $Hammer->poundNails() .  ' makes a Wood Desk';
  }

Open in new window


It is my opinion that empty Classes like -

class Hammer
{
}

Does not teach a beginner in Classes anything useful.

Can you tell us what you may mean by, your word "strategy", , and how you do not understand what this Code, in WorkForce Class, is suppose to be doing or teaching? ?
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39865595
@Slick: I could be mistaken, but I think the term strategy in this question is referring to the Gang-of-Four Strategy Design Pattern.  The implementation is abstracted from the requirement.  In PHP, this might be an Interface or an Abstract Class, but I think the little code sample is good enough.  We need work done, and the two kinds of work we can do are Build-A-Desk and Drive-A-Car.  The concrete class that provides each of those activities is defined in only one place and the Workforce class is configured to meet the client requirements.
0
 
LVL 33

Assisted Solution

by:Slick812
Slick812 earned 300 total points
ID: 39865789
@ ray paseur
I can not, in any way, see your point about -
"two kinds of work we can do are Build-A-Desk and Drive-A-Car"
and being applied to any "Planing" or "Strategy" in this very loose structure of the relationship of the worker and workforce Classes. There seems to be little, , if any Planing for the interaction between these two Classes, That I can see.
Ray, You seem to Guess, and read into this code, much more than I can attribute to it from reading that code.

I will leave this question, as It may not go anywhere I can follow using your logic.

I will leave with a Couple of design pattern, take out quotes from a "Richard Carr,", hopefully to give  rgb192  a light overview for "Planing" how to build-write an interactive Class.
"design patterns are generally aimed at solving the problems of object generation and interaction,"
PLEASE NOTE the word "interaction", to me this is the MOST important part of writing a Class, how elements as Properties, methods, Objects effect each other "interact"

And -
"It is more important to understand the concepts that design patterns describe, rather than memorizing their exact classes, methods and properties. It is also important to apply patterns appropriately. Using the incorrect pattern for a situation or applying a design pattern to a trivial solution can over complicate your code and lead to maintainability issues."
As he says, "understand the concepts", and this question code does not show any teaching for understanding the pattern used for "Planing"-"Strategy" in design, ,  for why the workers are in an array in the workforce class, yes I know that it works, but I can not see it as a plan, but I am likely just to dumb to get it, sorry.
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:rgb192
ID: 39872336
Can you tell us what you may mean by, your word "strategy", , and how you do not understand what this Code, in WorkForce Class, is suppose to be doing or teaching? ?

gang of four design pattern strategy.  I do not know all the design patterns, but this design pattern was mentioned in another oop book.

<?php
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)
  {
      echo'<br>... code that builds a desk ...';
      echo '<br>'. $WoodPlanks->cutWood(). $Nails->GetNails('2-inch').$Hammer->poundNails(). 'makes a Wood Desk';
  }
}

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

class Test
{
  public function doyourjob(){
    $s= new Hammer();
    $j= new Jerry();
    $j->DoYourJob();
  }
}

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





class Hammer
{
  public function poundNails(){
  }
}

class Nails
{
  public function GetNails($nails){
    $this->nails=$nails;
  }
}

class WoodPlanks
{
  public function cutWood(){
  }
}

class Car
{
  public function DriveTo($address)
  {
    echo "Okay, we're driving to {$address}";
  }
}

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

Open in new window


is that what you mean by putting data into empty classes so I can learn better?
0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 200 total points
ID: 39873392
I think the example was simpler and made more sense when the methods were empty.  As a practical matter, I often write empty methods when I start coding.  It helps me keep my thinking organized and lets me focus on overall structure instead of the details of the implementation.
0
 
LVL 33

Accepted Solution

by:
Slick812 earned 300 total points
ID: 39874252
OK, you ask - "is that what you mean by putting data into empty classes so I can learn better? "

Yes, add code to see it working,  I know that people can learn code application in development, ONLY if they write and USE code, so that they can see what happens when they change, add, remove, relocate written Code work.
As far as I can tell this is a MOST BASIC Class example, to show how to create different Classes (class bob and Jerry) as "workers" and add them to the "workforce class", this shows you that two methods with the same Name as "DoYourJob( )" has Different output, because they are in two different Classes.

I see that you have added the code for a "class Test", so I will comment about that,
In it you create two objects with -
$s= new Hammer();
$j= new Jerry();

but you do not use those in this method, so they are useless objects, and should not have been created, , , even thou you got this Idea from the "class Bob", please look to see that at least there, they use and send the new objects to a method as -
$this->_buildDesk($H, $N, $W);

the way this shows anything about web page output is to use strings as output, so to continue a more useful "code write" you may have placed output in the method such as -

class Hammer
{
  public function poundNails(){
  return " Hammering Nails ";
  }
}

I disagree whole-heatedly with ray about the first code write, although this tutorial may or may not be about the class code structuring, you can not learn about how and why "class code structuring" will help you in PHP work, unless you change code to see how the structure fits together and adds to your "ease and understanding" of the class code write.
AS the quote i gave says -
"It is more important to understand the concepts that design patterns describe, rather than memorizing their exact classes, methods and properties"

Ray says something about a pattern name, but these names do not help, I almost never think about any names, I think about something (pattern-class-structure) I have used-seen before, and then try-It-Out, to see if it helps, but I can only use these if I understand the relationships and how they work., but That's just my way, rgb192, You will need to find your own way by experimenting and changing code tutorials to see what can be done, and how it can deliver more efficient code classes for your php work, which can be very time consuming for some projects.
0
 

Author Closing Comment

by:rgb192
ID: 39881188
all these answers are helping me discover the strategy in strategy pattern
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
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…

762 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

16 Experts available now in Live!

Get 1:1 Help Now