Solved

What design pattern is this?

Posted on 2009-05-13
11
323 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
 
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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 500 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

The CRUD Functions CRUD, meaning "Create, Read, Update, Delete (http://en.wikipedia.org/wiki/Create,_read,_update_and_delete)" is a common term to data base developers.  It describes the essential functions of data base table maintenance.  This art…
Introduction A frequently used term in Object-Oriented design is "SOLID" which is a mnemonic acronym that covers five principles of OO design.  These principles do not stand alone; there is interplay among them.  And they are not laws, merely princ…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

744 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