Design problems (beginner)

M

Michael Judge

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?
 
G

Gregory Brown

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

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 d=
o.

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.
 
G

Gregory Brown

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top