PHP5 - Object
am 14.10.2007 14:34:10 von iavian
interface Pizza {
public function getPrice();
}
class Margherita implements Pizza {
private $cost = 4.50;
public function getPrice(){
return $this->cost;
}
}
class withExtraTopping implements Pizza{
private $cost = 0.50;
private $pizza;
public function __construct(Pizza $pizza){
$this->pizza = $pizza;
}
public function getPrice(){
return $this->cost + $this->pizza->getPrice();
}
}
$pizza = new Margherita();
$topping1 = new withExtraTopping($pizza);
$total = $topping1->getPrice();
print $total;
?>
Need a couple of lines of code that would generate a margherita pizza
with two extra toppings from the classes above, and print out the
total cost of the pizza?
But how to create a pizza with 2 toppings ??
Re: PHP5 - Object
am 14.10.2007 15:42:25 von Thomas Mlynarczyk
> But how to create a pizza with 2 toppings ??
I would do something like this:
interface NotForFree { function getPrice(); }
class Pizza implements NotForFree { ... }
class Margherita extends Pizza { ... }
class Topping implements NotForFree { ... }
class CheeseTopping extends Topping { ... ]
class OnionTopping extends Topping { ... }
$pizza = new Margherita;
$pizza->addTopping( new CheeseTopping );
$pizza->addTopping( new OnionsTopping );
$pizza->getPrice();
/* or */
$pizza = new Margherita( new CheeseTopping, new OnionsTopping );
$pizza->getPrice();
Greetings,
Thomas
--
C'est pas parce qu'ils sont nombreux à avoir tort qu'ils ont raison!
(Coluche)
Re: PHP5 - Object
am 14.10.2007 16:12:52 von luiheidsgoeroe
On Sun, 14 Oct 2007 15:42:25 +0200, Thomas Mlynarczyk =
wrote:
>> But how to create a pizza with 2 toppings ??
>
> I would do something like this:
>
> interface NotForFree { function getPrice(); }
> class Pizza implements NotForFree { ... }
> class Margherita extends Pizza { ... }
> class Topping implements NotForFree { ... }
> class CheeseTopping extends Topping { ... ]
> class OnionTopping extends Topping { ... }
>
> $pizza =3D new Margherita;
> $pizza->addTopping( new CheeseTopping );
> $pizza->addTopping( new OnionsTopping );
> $pizza->getPrice();
>
> /* or */
>
> $pizza =3D new Margherita( new CheeseTopping, new OnionsTopping );
> $pizza->getPrice();
I'd definitely make Pizza a class and not an interface indeed. Then just=
a =
properly executed Decorator Pattern can set, and tell you exactly what =
type, toppings and total price this particular pizza would have. I would=
=
not recommend making 'Margherita','CheeseTopping' etc. hardcoded classes=
.. =
Unless it remains static after development, it would be a maintenance =
nightmare to add/remove/alter different kinds of pizza & toppings. I =
assume come kind of interface that gives options for Pizza & Toppings =
would be involved, which would mean that any alteration in those require=
s =
altering code & data in several places.
Rather, I'd have a database with a Pizzas table, including name & price,=
=
and a Toppings table, including name & price, and just a 'generic' Pizza=
=
object with the type/name/price in a variable, and either 'decorated' =
using the Decorator Pattern with generic Topping objects (which themselv=
es =
hold a toppingtype/toppingprice), or just an array of toppings in the =
Pizza class itself, so you could do a:
class Pizza{
...
public function getPrice(){
$price =3D $this->_price;
if(!empty($this->_toppings){
foreach($this->_toppings as &$topping){
$price +=3D $topping->getPrice();
}
}
return $price
}
...
}
... which hardly is any complicated pattern but gets the job done easily.=
=
As long as 'toppings' don't have any profound impact on the 'Pizza' othe=
r =
then price and actually being added as topping I'd choose this. If they =
=
have other impacts, like changing the output of Pizza::getRecipe(), =
Pizza::getBoxSize(), Pizza::getCalories() the Decorator Pattern would =
certainly pay out.
-- =
Rik Wasmus
Re: PHP5 - Object
am 15.10.2007 01:50:33 von oliver.graetz
iavian schrieb:
>
>
> interface Pizza {
> public function getPrice();
> }
>
> class Margherita implements Pizza {
>
> private $cost = 4.50;
>
> public function getPrice(){
> return $this->cost;
> }
> }
>
> class withExtraTopping implements Pizza{
>
> private $cost = 0.50;
> private $pizza;
>
> public function __construct(Pizza $pizza){
> $this->pizza = $pizza;
> }
>
> public function getPrice(){
> return $this->cost + $this->pizza->getPrice();
> }
> }
>
> $pizza = new Margherita();
> $topping1 = new withExtraTopping($pizza);
> $total = $topping1->getPrice();
> print $total;
>
> ?>
>
> Need a couple of lines of code that would generate a margherita pizza
> with two extra toppings from the classes above, and print out the
> total cost of the pizza?
>
> But how to create a pizza with 2 toppings ??
>
$topping2 = new withExtraTopping($topping1);
$total = $topping2->getPrice();
print $total;
Where's the problem, it's a simple decorator example.
OLLi
--
Piper: "See what I mean? We have bigger naked breasts to worry about."
Phoebe: "Paige has her naked breasts to worry about. I've got yours."
[Charmed 702]
Re: PHP5 - Object
am 15.10.2007 03:34:05 von Jerry Stuckle
Oliver Grätz wrote:
> iavian schrieb:
>>
>>
>> interface Pizza {
>> public function getPrice();
>> }
>>
>> class Margherita implements Pizza {
>>
>> private $cost = 4.50;
>>
>> public function getPrice(){
>> return $this->cost;
>> }
>> }
>>
>> class withExtraTopping implements Pizza{
>>
>> private $cost = 0.50;
>> private $pizza;
>>
>> public function __construct(Pizza $pizza){
>> $this->pizza = $pizza;
>> }
>>
>> public function getPrice(){
>> return $this->cost + $this->pizza->getPrice();
>> }
>> }
>>
>> $pizza = new Margherita();
>> $topping1 = new withExtraTopping($pizza);
>> $total = $topping1->getPrice();
>> print $total;
>>
>> ?>
>>
>> Need a couple of lines of code that would generate a margherita pizza
>> with two extra toppings from the classes above, and print out the
>> total cost of the pizza?
>>
>> But how to create a pizza with 2 toppings ??
>>
>
> $topping2 = new withExtraTopping($topping1);
> $total = $topping2->getPrice();
> print $total;
>
> Where's the problem, it's a simple decorator example.
>
> OLLi
>
Several things.
What's the difference between an object of class withExtraTopping and
$topping1?
What is the price of $topping2? Every pizza place I know charges more
for the same topping on a larger pizza.
I agree with Rik. Pizza is a real object, and should be a class, not an
interface.
Topping should also be a class. And keeping everything in a database
would be the best way to go.
And while you *could* create a class Margherita which extends Pizza, it
doesn't pay to get *too* specific. As Rik indicated, if you ever get
rid of this type of pizza or add others, you'll have to change a lot of
code.
Keep it simple. It makes life easier.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Re: PHP5 - Object
am 15.10.2007 23:07:30 von oliver.graetz
Jerry Stuckle schrieb:
> Oliver Grätz wrote:
>> $topping2 =3D new withExtraTopping($topping1);
>> $total =3D $topping2->getPrice();
>> print $total;
>>
>> Where's the problem, it's a simple decorator example.
>>
>> OLLi
>>
>=20
> Several things.
>=20
> What's the difference between an object of class withExtraTopping and=20
> $topping1?
>=20
> What is the price of $topping2? Every pizza place I know charges more =
> for the same topping on a larger pizza.
>=20
Changing the concept is out of scope here. I simply ANSWERED the
question asked by the original poster. Changing the price of extra
toppings and making the price dependent on the pizza size was NOT part
of the question.
OLLi
--=20
X:"I was working hard for that money."
S:"And I didn't?"
X:"You stole it."
S:"And you're making it very hard work"
[Buffy 514]
Re: PHP5 - Object
am 16.10.2007 01:25:45 von Jerry Stuckle
Oliver Grätz wrote:
> Jerry Stuckle schrieb:
>> Oliver Grätz wrote:
>>> $topping2 = new withExtraTopping($topping1);
>>> $total = $topping2->getPrice();
>>> print $total;
>>>
>>> Where's the problem, it's a simple decorator example.
>>>
>>> OLLi
>>>
>> Several things.
>>
>> What's the difference between an object of class withExtraTopping and
>> $topping1?
>>
>> What is the price of $topping2? Every pizza place I know charges more
>> for the same topping on a larger pizza.
>>
>
> Changing the concept is out of scope here. I simply ANSWERED the
> question asked by the original poster. Changing the price of extra
> toppings and making the price dependent on the pizza size was NOT part
> of the question.
>
> OLLi
>
It is if you're emulating the real world.
Initial requirements do not always reflect final needs.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Re: PHP5 - Object
am 16.10.2007 05:29:54 von oliver.graetz
Jerry Stuckle schrieb:
> Oliver Grätz wrote:
>> Jerry Stuckle schrieb:
>>> Oliver Grätz wrote:
>>>> $topping2 =3D new withExtraTopping($topping1);
>>>> $total =3D $topping2->getPrice();
>>>> print $total;
>>>>
>>>> Where's the problem, it's a simple decorator example.
>>>>
>>> Several things.
>>>
>>> What's the difference between an object of class withExtraTopping and=
=20
>>> $topping1?
>>>
>>> What is the price of $topping2? Every pizza place I know charges mor=
e=20
>>> for the same topping on a larger pizza.
>>>
>> Changing the concept is out of scope here. I simply ANSWERED the
>> question asked by the original poster. Changing the price of extra
>> toppings and making the price dependent on the pizza size was NOT part=
>> of the question.
>>
>=20
> It is if you're emulating the real world.
>=20
> Initial requirements do not always reflect final needs.
>=20
Yes, but you were answering to MY RESPONSE and not to the original
poster. I just DIRECTLY answered the question. Other evangelizing
answers had already been given (and I didn't need to elaborate on them)
but I personally dislike the fact that these people tend to bring up
more questions INSTEAD OF solving the problem at hand. You can bring up
your own concept, fine. But please ANSWER THE QUESTION first. What
speaks against the idea that iavian just didn't get the concept of a
decorator? Nothing, since the code he already had was well capable of
solving the questions he asked without change!
OLLi
--=20
Never change a running system.
Re: PHP5 - Object
am 16.10.2007 14:45:10 von Jerry Stuckle
Oliver Grätz wrote:
> Jerry Stuckle schrieb:
>> Oliver Grätz wrote:
>>> Jerry Stuckle schrieb:
>>>> Oliver Grätz wrote:
>>>>> $topping2 = new withExtraTopping($topping1);
>>>>> $total = $topping2->getPrice();
>>>>> print $total;
>>>>>
>>>>> Where's the problem, it's a simple decorator example.
>>>>>
>>>> Several things.
>>>>
>>>> What's the difference between an object of class withExtraTopping and
>>>> $topping1?
>>>>
>>>> What is the price of $topping2? Every pizza place I know charges more
>>>> for the same topping on a larger pizza.
>>>>
>>> Changing the concept is out of scope here. I simply ANSWERED the
>>> question asked by the original poster. Changing the price of extra
>>> toppings and making the price dependent on the pizza size was NOT part
>>> of the question.
>>>
>> It is if you're emulating the real world.
>>
>> Initial requirements do not always reflect final needs.
>>
> Yes, but you were answering to MY RESPONSE and not to the original
> poster. I just DIRECTLY answered the question. Other evangelizing
> answers had already been given (and I didn't need to elaborate on them)
> but I personally dislike the fact that these people tend to bring up
> more questions INSTEAD OF solving the problem at hand. You can bring up
> your own concept, fine. But please ANSWER THE QUESTION first. What
> speaks against the idea that iavian just didn't get the concept of a
> decorator? Nothing, since the code he already had was well capable of
> solving the questions he asked without change!
>
> OLLi
>
Yes, I responded to you because I thought your response was not a good
way to do it, for the reasons I gave.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================