What design pattern is this?

Can anyone tell me what design pattern this is?
class Vehicle {
 
    var $price;
    
    function & _instance($vehicle)
    {
        $class = 'Vehicle_' . $vehicle;
        $file = 'Vehicle_'.$vehicle.'.php';
        include($file);
        $instance = & new $class;
        return $instance;
    }
}
 
class Vehicle_Car extends Vehicle {
    var $price = 1000;
}
 
class Vehicle_Bike extends Vehicle {
    var $price = 2000;
}

Open in new window

LVL 11
SlimshaneeyAsked:
Who is Participating?
 
Jonah11Connect With a Mentor Commented:
Slim,

Okay, I think the confusing thing here is that basket isn't a client of the factory.  Basket is using the products themselves, not the factory.  Take a look at the simple example here:

http://en.wikipedia.org/wiki/Factory_method_pattern#Example

PIzzaLover (the class at the bottom) is the client in this example.
0
 
BrianMMCommented:
Looks like singleton to me....
0
 
Jonah11Commented:
if this were singleton, don't we have to be tracking if the object has been created already, and returning the already created version if it has?  it seems in this case we are *always* creating a new instance..... am i missing something?
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
DarrenDCommented:
Hi,

It could be you are trying to create a singleton but I don't see how you are stopping it creating multiple instances...


if ($instance == null)
{
  $instance = & new $class;
}
return $instance;

Could be a version of Factory pattern....

Haven't used PHP in a while...

Hope this helps,

Darren
0
 
SlimshaneeyAuthor Commented:
Factory sounds about right. Im trying to get my head around it.
As an example of the challenge I have in front of me, what if I had another class that was to say, purchase thesse items.

What code do I need to add to addtoBasket so that a new Vehicle is created and added to the basketitems array?


class basket()
{
var $basketitems = array();
var $sumtotal = 0;
 
function addtoBasket($vehicle){
 
}
 
function getTotalCost(){
 
}
}

Open in new window

0
 
Jonah11Commented:
hey slim,

you may be getting confused because it's not clear what the interacting parts are in your example.  in the factory method, eg, you have a client that needs objects, a factory (used by the client) to create the objects, and the objects themselves.  in this way, the client is simply using a generic "product", and doesn't need to know any specific details about the product (ie, doesnt need to know what particular concrete subclass its using).  you probably won't be able to wrap your head around it until you approach it from this high-level conceptual perspective, and figure out, in your particular example:

1. who/what the client is?
2.  what the abstract "product" is (presumably Vehicle)
3.  what the concrete classes are (presumably Car and Bike)
4. why it's important for the client not to know what concrete subclass its working with.

HTH,
Jonah
0
 
Jonah11Commented:
Also, one other note: If the only thing that was different in your products was the price, it would probably be bad design to use factory method.  Typically you will have 2 or more methods on your class (Vehicle), and the subclasses define different behavior for them.
0
 
SlimshaneeyAuthor Commented:
The Context of the system is that we want to be easily able to add new vehicle types, derived from the class Vehicle. The basket class is then a way for me to be able to access any type of vehicle, add it to the basket and get a total cost.
Its a very simple example to demonstrate this pattern and Im struggling with the addtoBasket bit and how its actually useful.

Shane
0
 
SlimshaneeyAuthor Commented:
Price is only one exposure, thats correct, there coulde be many more. Im just trying to understand how it actually all fits together, how I can use the basket class to add items to an imaginary basket and total the cost.
0
 
SlimshaneeyAuthor Commented:
Awesome example, exactly what Im looking for. Think I understand now.

0
 
Jonah11Commented:
glad i could help.  also, if you want to learn more about patterns, i highly recommend this book:

http://www.amazon.com/First-Design-Patterns-Elisabeth-Freeman/dp/0596007124/ref=sr_1_2?ie=UTF8&s=books&qid=1242219321&sr=1-2

very simple, easy to understand examples with detailed explanation.  user friendly and not overly technical.
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.

All Courses

From novice to tech pro — start learning today.