SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    Sep 2003
    Location
    Melbourne, Victoria, Australia
    Posts
    115
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question What's this Strategy Pattern?

    Hi,

    I've read a few posts lately that task about using the Strategy pattern. For my own curiosity and learning, would someone mind explaining what problem it sets out to solve and how it works?

    I've googled for it, but couldn't find any examples that were really relevant and explained it well.

    Thanks,
    Af.

  2. #2
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  3. #3
    SitePoint Zealot
    Join Date
    Sep 2003
    Location
    Melbourne, Victoria, Australia
    Posts
    115
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the quick response markl.

    Of course - the one site I didn't think to check!

    So when there in an action that be conducted a number of different way, the StrategyPattern uses inheritence to provide the same interface to each option. ??

    Any other uses aside from validation that people can think of?

    Af.

  4. #4
    SitePoint Guru
    Join Date
    Dec 2003
    Location
    oz
    Posts
    819
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    when calculating something based on different business rules, you would use it. For example alcohol has a higher charge of tax (unfortunantly =P), so to calculate cost, you might do something like this:

    PHP Code:
    $items[] = Items->GetByName('Smirnoff');
    $items[] = Items->GetByName('Chips');
    OrderManager->GetCost(items);
     
    function 
    GetCost($items)
    {
        
    $tota 0;
        foreach(
    $items as $item)
        {
            
    $category $item->Category;
            
    $tax $category->TaxAmount;
            
    $price $item->Cost $tax;
            
    $total += $price;
        }
        return 
    $total;

    In this case, you have a base class category, and inheriting classes for alcohol, food, etc... Each of these has a tax amount assigned to it, as each category is a different business rule.

  5. #5
    SitePoint Evangelist
    Join Date
    May 2004
    Location
    New Jersey, USA
    Posts
    567
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Afro Boy
    Any other uses aside from validation that people can think of?
    Any scenario in which the "rules" change for different parts of the system -- privilege levels, membership rankings, sales or value-added tax, sales compensation, gaming.

    =Austin

  6. #6
    SitePoint Zealot
    Join Date
    Sep 2003
    Location
    Melbourne, Victoria, Australia
    Posts
    115
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Excellent. It all makes sense to me now. Quite simple really!

    Thanks,
    Af.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •