Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

What design pattern is this?

Posted on 2009-05-13
11
Medium Priority
?
374 Views
Last Modified: 2013-11-12
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

0
Comment
Question by:Slimshaneey
11 Comments
 
LVL 11

Expert Comment

by:BrianMM
ID: 24372807
Looks like singleton to me....
0
 
LVL 7

Expert Comment

by:Jonah11
ID: 24372886
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
 
LVL 18

Expert Comment

by:DarrenD
ID: 24372951
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 11

Author Comment

by:Slimshaneey
ID: 24373544
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
 
LVL 7

Expert Comment

by:Jonah11
ID: 24373625
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
 
LVL 7

Expert Comment

by:Jonah11
ID: 24373635
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
 
LVL 11

Author Comment

by:Slimshaneey
ID: 24373642
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
 
LVL 11

Author Comment

by:Slimshaneey
ID: 24373656
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
 
LVL 7

Accepted Solution

by:
Jonah11 earned 2000 total points
ID: 24373880
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
 
LVL 11

Author Closing Comment

by:Slimshaneey
ID: 31580879
Awesome example, exactly what Im looking for. Think I understand now.

0
 
LVL 7

Expert Comment

by:Jonah11
ID: 24374220
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
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.
Suggested Courses

578 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