Page 1 of 1

Value Object Pattern

Posted: Thu Jun 29, 2006 4:55 am
by Chris Corbyn
I probably don't need this but I'm just looking to use it in a practical scenario since I haven't used it as yet :P

Basically, I'm writing a stock control system for a school shop.

It only needs to be *very* basic. (I'm not really using an interface -- I'm just representing it this way).

Code: Select all

interface iStock
{
    public function addItem($name, $price, $quantity=1, $description='');
    public function removeItem($id);
    public function increaseQuantity($id, $amount); //When more stock comes in
    public function decreaseQuantity($id, $amount=1); //When a sale occurs
    public function disableItem($id); //Disable it in the system so people can't buy it even if it's in stock
    public function enableItem($id);
    public function changePrice($id, $price);
}
Does using Value Object make sense here? I've yet to see where Value Object is actually of any use :( Perhaps it's more suited to the side of things for adding and removing funds from the student.

Re: Value Object Pattern

Posted: Thu Jun 29, 2006 5:28 am
by Christopher
All the $id's make gave me this idea:

Code: Select all

interface iStock
{
    public function addItem($item);
    public function removeItem($id);
    public function getItem($id);
}

interface iItem
{
    public function __construct($id, $amount, $price);
    public function increaseQuantity($amount); //When more stock comes in
    public function decreaseQuantity($amount=1); //When a sale occurs
    public function disable(); //Disable it in the system so people can't buy it even if it's in stock
    public function enable();
    public function setPrice($price);
}

Posted: Thu Jun 29, 2006 6:01 am
by Chris Corbyn
That makes a bit more sense actually yes. So now we have stock control, items, and funds (not mentioned here).

Using Value Object is going to actually be more work and needlessly complex I think here too.... it's all just number juggling in the database by running queries. Value object does not appear to be persistent in that way from the examples I've seen.

Posted: Thu Jun 29, 2006 10:20 am
by Christopher
It actually looks like an OR Mapper / ActiveRecord combo which is the usual solution that shakes out for problems like these. Each Item object contains the business logic and the Stock object tracks everything and provides support for persistence.