I am writing a class that is very specific to it's application, and will never be used outside this application. It is instantiated from within another class (which is instantiated from yet another class/parent). I have always tried to keep my classes self-contained, all attributes set at instantiation or through methods -- period.
Now this class needs the value of a constant that is declared in the application's config file. I am trying to learn the correct way to do things, so my question is:
Given the situation, would it be better to pass this value through two parent classes just so they can pass it to this third (child) class, or should this class simply refer to the constant (making it no longer self-contained)?
Class attributes set by constants or passed vars?
Moderator: General Moderators
- n00b Saibot
- DevNet Resident
- Posts: 1452
- Joined: Fri Dec 24, 2004 2:59 am
- Location: Lucknow, UP, India
- Contact:
since you are after self-containment.. the variable can be passed through the parent classes (although I still think that defies self-containment.. somebody correct me if I am wrong).
I would also advise you to look into Registry pattern since you're into OOP
I would also advise you to look into Registry pattern since you're into OOP
Last edited by n00b Saibot on Wed Sep 20, 2006 10:45 am, edited 1 time in total.
Thanks
Thank you, this is the information that I was looking for. It seems every time I ask a question, I learn that the answer is irrelevant, because there's a better way to approach the situation all together. Thank again.n00b Saibot wrote: I would also advise you to look into Registry pattern since you're into OOP
- Maugrim_The_Reaper
- DevNet Master
- Posts: 2704
- Joined: Tue Nov 02, 2004 5:43 am
- Location: Ireland
Using the constant directly in the class creates coupling. Moving the class elsewhere, or changing the config file syntax for example, will require you to edit the class to compensate for the change. The solution which maintains decoupling (or self-containment) is to pass the value though the constructor or via a setter method.
If your class suffers from parameter crowding (too many variables getting passed into the constructor), a value object or registry might be worth looking into. One simple method is to create a Config or Settings object which is responsible for reading a config file and storing its values. This Config object can then be "registered" to a Registry. Pass the Registry into the class, and take it from there. There's a few Config/Settings classes for reference. I use one (http://svn.sourceforge.net/viewvc/quant ... /Settings/) and of course the Zend Framework has one bundled as Zend_Config.
Might seem overkill, but the config classes are reuseable for most projects in the future...
If your class suffers from parameter crowding (too many variables getting passed into the constructor), a value object or registry might be worth looking into. One simple method is to create a Config or Settings object which is responsible for reading a config file and storing its values. This Config object can then be "registered" to a Registry. Pass the Registry into the class, and take it from there. There's a few Config/Settings classes for reference. I use one (http://svn.sourceforge.net/viewvc/quant ... /Settings/) and of course the Zend Framework has one bundled as Zend_Config.
Might seem overkill, but the config classes are reuseable for most projects in the future...