I'm wondering if using a Singleton for a storage of discounts is the right way to go. This is because I will be looping over all added discounts in another class.
client code
// Dynamically add new Discounts
$collection = DiscountCollection::getInstance();
$collection->add(new TenPercentDiscount());
$collection->add(new TwentyPercentDiscount());
DiscountCollection
<?php namespace Notflip\Discount;
use Notflip\Discount\Discounts\Discount;
class DiscountCollection {
private static $instance;
private static $discounts;
final static function getInstance()
{
if(static::$instance === null)
{
static::$instance = new static();
}
return static::$instance;
}
public function add(Discount $discount)
{
static::$discounts[] = $discount;
}
public function count()
{
return count(static::$discounts);
}
public function show()
{
return static::$discounts;
}
}