|
Posted by Steve on 08/24/07 19:01
"Toby A Inkster" <usenet200707@tobyinkster.co.uk> wrote in message
news:18g2q4-6po.ln1@ophelia.g5n.co.uk...
| burgermeister01@gmail.com wrote:
|
| > I needed to add an array class member to an object. It was exactly the
| > same as another class member, except that one array stored regular
| > products, and the other stored free promotional products. As such, I
| > needed a way to add products to the new, free array. Since all the logic
| > was the same between the two arrays aside from the price, I had a few
| > different options to do this.
|
| Frankly I think all the options you outlined expose too much of the inner
| workings of your class. What happens when you add a third category of
| products (products that cost money, free products & products we have to
| pay you to take away!)
|
| A better solution would be something like this:
|
| public function add_product (Product $p)
| {
| if ($p->price==0)
| $this->free_products[] = $p;
| else
| $this->products[] = $p;
| }
well, add_product shouldn't care about anything specific to $p outside of
it's price, availability, quantity, etc. since both 'free' and 'not-free'
product have them, add_product should look more like:
public function add_product(IProduct $p)
{
$this->productions[] = $p;
}
where IProduct is an interface having all of those shared attributes. if
add_product is the interface of an 'order' object, it should look more like:
public function add_product(IItem $item)
{
$this->items[] = $item;
}
where IItem is an interface implemented in a base 'product' class and a base
'service' class...a free product or service simply extends their base class.
the WHOLE time, the 'order' object couldn't care less. it just cares about
price, availability, quantity, etc.. ain't loose-coupling great!
;^)
[Back to original message]
|