#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2009
    Posts
    34
    Rep Power
    6

    OO Design Question


    The Application
    I have built a web application that allows a company to manage its clients and the projects they are doing for those clients. The app is built using PHP5 + Zend Framework.

    I currently have a project class that is populated from the database on creation and has a number of different methods.

    The Problem
    I must now change the system so that clients can pay for projects. Each project is either a fixed price project or a pay as you go project. The different types of projects will have different functions for example pay as you go would have a function to fetch the number of remaining hours whereas fixed price would have a function to find out how much is left to pay.

    The Question
    Whats the best way to implement this. My first thought was to make 2 new child classes of project one for fixed price the other for pay as you go. However I am not an OO expert and I am not sure if this is correct.

    Also if I was to do this I am not sure how I would fetch a project because I would have to know the type of project before knowing what subclass to initiate.

    Any help would be greatly appriecated.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    IMHO inheritance is often overused by people new to OO design. In this sort of situation delegation is often better.

    I would have a separate PaymentPolicy class that the Project class can have as an attribute. This would then be subclassed into FixedPricePolicy and PayAsYouGoPolicy (and maybe others in the future). This class heirarchy would handle anything to do with payment.

    This has several advantages:

    1) it solves your creation problem

    2) It fulfills the Single Responsibility Principle (every class should be responsible for exactly one thing).

    3) A project can change its payment policy at will - contracts sometimes get re-negotiated.

    4) The policy classes could be reused with things other than Project the classes, such as maintenance contracts.

    Dave

    Comments on this post

    • jwdonahue agrees : It's better to have a thing than to be a thing, unless of course, a thing is what you are! ;)
    • Yawmark agrees
    • cubic98 agrees
    • NXInteractif agrees
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2009
    Posts
    34
    Rep Power
    6

    Thank You


    Thanks for the reply, that makes a lot of sense. I will look into implementing it now.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2008
    Posts
    62
    Rep Power
    14
    I think DevCoach is right, there are many mismatches between relational database and object oriented design paradigms. One of them is how to map the relational entities to objects and wether to choose inheritance over delegation. There is no rule to give us the right response, only experience can show us what to do.

    Here is a very technical lecture you might have, maybe too technical: http://en.wikipedia.org/wiki/Object-Relational_impedance_mismatch
    To find out everything about design patterns check the Design Patterns Reference on oodesign.com.

    Find and share the best tips, tutorials and howto's for php web developers on the newly started php-html.net community.
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2009
    Location
    Hobart, Tasmania
    Posts
    475
    Rep Power
    286
    All projects are payable ... but if the price is zero ... skip that bit.

    There is no reason for inheritance or decoration or any other fancy pattern here ... either the project has an amount due, or it doesn't. This is a two state issue. There have been basic control structures capable of catering for this since Margret wore fur panties and Chong recoiled in disgust.
    Last edited by l8rm8e; January 10th, 2010 at 11:44 AM.
    Latery Matey

IMN logo majestic logo threadwatch logo seochat tools logo