PDA

View Full Version : Classes




MacDonaldsd
Aug 25, 2007, 08:19 AM
How does one Class know about another class ?

Im doing a timetable application.

So every module is related to a single semester.

Therefore Semester is a class and Module is a class

Is this done through inheritance ?



Nutter
Aug 25, 2007, 08:26 AM
Just import the class definition of one class into the class implementation file of the other.

MacDonaldsd
Aug 25, 2007, 08:37 AM
Yeah but module inherits values from semester.

Such as start and end date.

Nutter
Aug 25, 2007, 08:55 AM
In that case, yes, Module could inherit from Semester. Or they could both inherit from a common abstract superclass.

Heath
Aug 25, 2007, 11:29 AM
I would shy away from inheritance in this case since a module is not a semester.
'Prefer composition over inheritance' is the guiding principle here.

I would instead say that a semester contains modules.
Have instances of modules within your semester instead of having module inherit from semester.

Unless I've not understood clearly, in which case please clarify.

toddburch
Aug 25, 2007, 05:30 PM
I would think a Module would be the static thing here, and it could be taught in any given Semester.

Therefore, any Semester could have a list of Modules that were taught, or, a Module could have a record of all the Semesters in which it was (or will be) taught.

One would then be an attribute of the other, and there would not be any inheritance.

start_date = module.semester().startdate() would fit the above scenario.

If any Module's start / end dates could override a Semester's start / end dates, you could do inheritance, where the Module inherited from the Semester, but now you are starting to get into some language specific implementation. In Java, Semester could be an abstract class that Module pulled in. This would get you to

start_date = module.startdate()

In C++ or Ruby though, it could be argued either way whether Module could be a subclass of Semester. I wouldn't necessarily go the other way around though.

Todd

MacDonaldsd
Aug 25, 2007, 05:42 PM
I would shy away from inheritance in this case since a module is not a semester.
'Prefer composition over inheritance' is the guiding principle here.

I would instead say that a semester contains modules.
Have instances of modules within your semester instead of having module inherit from semester.

Unless I've not understood clearly, in which case please clarify.

Yeah you understand correctly. And also like toddburch said the reason I want it is so it falls within the time frame of the semester, as well as being able to seach via semester etc.

Eraserhead
Aug 25, 2007, 06:34 PM
Are you using Core Data for this application? As long as you are prepared to make it Tiger/Leopard only that would probably be the best way of managing your different object types.

zimv20
Aug 25, 2007, 09:27 PM
since we're talking design now...

i don't see any direct relationship between a Module and a Semester. just off the top of my head, i'd start with something like:

- a ClassManager would aggregate Modules

- a SemesterManager would aggregate Semesters

- a SemesterScheduler would maintain a relationship between a Semester and the Modules offered during that semester. it would also schedule when those modules occur. note that there would be some kind of helper class (like ScheduledClass) to track the multiple offerings of that Module.

- a StudentManager would aggregate Students, and also keep track of which students are active for a given Semester. it would also map students to ScheduledClasses. helper classes would resolve conflicts w/in a student's schedule, and ensure classes don't overfill. to get fancy, you could implement waitlists for scheduled classes, w/ some cool student priority logic, and allow students in when either someone drops or another of that module is offered.

- likewise, there'd be a FacultyManager to, among other things, assign faculty to ScheduledClasses.

note that plural classes are actually implemented to be singular (i.e. Student, not Students, ScheduledClass instead of ScheduledClasses).

there are many "right" ways to design this, but even more "wrong" ones. again, i'm not imagining a good design that would maintain a direct relationship between Modules and Semesters.

MacDonaldsd
Aug 26, 2007, 04:22 AM
Are you using Core Data for this application? As long as you are prepared to make it Tiger/Leopard only that would probably be the best way of managing your different object types.

I was going to do this, but my Cocoa/Object Orientated programming isn't amazing at the moment. All my programming ive done before is procedurally in ANSI C and PHP etc so its a few concepts il be learning at the same time.

I think core data is the way to go on this though, but im a bit scared about adding custom methods to the Classes dealt with by Core Data, and also what core data is taking care of behind he scenes and more importantly what it isn't.

Eraserhead
Aug 26, 2007, 05:21 AM
I think core data is the way to go on this though, but im a bit scared about adding custom methods to the Classes dealt with by Core Data, and also what core data is taking care of behind he scenes and more importantly what it isn't.

OK, have you gone through the tutorial (http://developer.apple.com/cocoa/coredatatutorial/index.html) on apple.com?

Personally though while creating D&D Manager (http://www.erasersoft.com/) with Core Data I haven't needed to use Custom Classes at all. (though I have extended NSManagedObject to allow object duplication, but that is fairly advanced.)