I'm working on a medium-sized project and feel like I either: A. Still don't understand object-oriented programming -or- B. Am duplicating Ruby syntax which I'm not familiar with First, a little background, (kept mercifully short,) I'm working on a survey interviewing program. I'm having having trouble with hammering classes into a structure: Interview Questions Question Answers Contents RHTML Control Answers I've been told that ideally, classes like "Answers" shouldn't know anything about other classes like "Control"; but in practice, it's really convenient for Answer's methods to talk to Control's public methods and vice versa. Here's a sexy graph: Interview.control <=> Interview.questions.question.answers When implementing this, to give these classes a "channel" to talk to each other, I've ended up passing each "parent" object to the "child" object at the child's creation. Let me show you what I'm talking about: class Question def initialize(interview, questions) self.interview = interview self.questions = question self.answers = Answers.new(self.interview, self.questions, self) self.contents = Contents.new(self.interview, self.questions, self) self.rhtml = RHTML.new(self.interview, self.questions, self) end end In english, to be created, Question demands to be passed its Questions "parent" and Interview "grandparent", then passes these "ancestors" and itself on to its children. Thankfully the children don't have any child objects of their own so we're not creating any four parameter objects, but still... I feel like this must be the programming equivalent of eating at a fancy restaurant with your feet. I fear I'm going to be thrown out of the programmer's union. Please help! Does ruby have a better way to emphasize structure between classes?
on 2006-01-26 08:38
on 2006-01-26 09:12
On 1/26/06, Michael Judge <email@example.com> wrote: > survey interviewing program. I'm having having trouble with hammering > classes into a structure: > > Interview > Questions > Question > Answers > Contents > RHTML > Control > Answers Can you give a bit more information as to what your program actually does? I think that you may be over classing this system I would say you have something like: Interview: contains collections of questions and answers Question: constitutes a single question and associative rules such as validation Answer: constitutes a single response to a question, and rules such as what could be considered valid. Control and Formatting tools seperate.... Consider a Question#to_rhtml method or something like that maybe? This still might be overkill. If your questions always map to the questions one to one, you probably don't need a class for Answers. I guess what I'd need to know to help is what you'd want these classes to do. There is almost certainly no need for Answers and Questions (the collection classes) as an array or hash will most likely hold them perfectly well in interview.
on 2006-01-26 09:15
On 1/26/06, Gregory Brown <firstname.lastname@example.org> wrote: > This still might be overkill. If your questions always map to the > questions one to one, you probably don't need a class for Answers. s/Answers/Answer
on 2006-01-27 01:49
Thanks for your input, Gregory. I've been doing more reading on object-oriented design today and think you're right, I should revamp my project structure into: Interview Question Answer And any cross-talk that currently happens between questions and answers needs to be handled in Interview. So I'd have Interview.compile_question, but not Interview.question(name).compile. And I'd have Interview.answer[key].validate, but not Interview.question(name).validate. As a bonus, it's less typing too. * * * The project is a web-framework for online surveys. Already existing open-source projects (like PHPSurveyor) lack basic functionality required for survey research. The alternative, commercial packages, with basic ones starting at $5,000/year for a lease -- make it really tough for people with limited means like small business consultants and graduate students to do survey research. So I'm trying to give back to the research community by releasing the next version of my company's in-house interviewing system open-source. I'm excited about the project and the work I've done on it, but want the source code to be reasonably polished too since I expect someday others will be looking at it. Here's an example of my current test quesitonnaire script: Q-INTRODUCTION Q <b>Thank you</b> for your interest in this study. Once you have completed the study, please allow up to 7-10 business days for your [panel] credit to appear in your [panel] account. Thank you for your time and opinions! Q-SA1 Q Which of the following best describes your employment status? (MARK ONE ONLY.) X-1 Employed full-time outside of the home X-2 Employed full-time, working from my home X-3 Employed part-time outside of the home X-4 Employed part-time, working from my home X-5 Homemaker X-6 Retired X-7 Student X-8 Not currently employed post: self.validate :required skip 'Q-S1' if self.questions.current.answers.has_value? 8 Q-SA Q Which, if any, of the following items have you purchased or ordered in the past year for work purposes? (MARK ALL THAT APPLY.) M-1 Business Cards M-2 3-Ring Binders M-3 Mailing Labels M-4 File Folders M-5 None of these M-6 Not currently employed post: self.validate :required self.validate :multiple, :params => [1..4, 5..6, "none of these/not currently employed"] skip 'Q-SB' if self.questions.current.answers.has_value? 2 skip 'Q-S1' ...and so on... Supports ERB codes too, which is kind of neat.