November 4th, 2009, 05:19 PM
OO Design Question
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.
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.
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.
November 5th, 2009, 02:24 AM
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.
Comments on this post
November 5th, 2009, 03:04 AM
Thanks for the reply, that makes a lot of sense. I will look into implementing it now.
January 8th, 2010, 08:05 PM
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
January 10th, 2010, 11:41 AM
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.