You are here: Re: PHP5 - Object « PHP Programming Language « IT news, forums, messages
Re: PHP5 - Object

Posted by Jerry Stuckle on 10/15/07 01:34

Oliver Grätz wrote:
> iavian schrieb:
>> <?php
>>
>> 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
==================

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация