Value Object Pattern

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Value Object Pattern

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Value Object Pattern

Post 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);
}
(#10850)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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.
(#10850)
Post Reply