this my first question on here. I hope someone can help.
It is to do with good object oriented design practices.
I am writing an android app, but the question is a general one and would apply equally to (e.g.) a swing user interface.
For the sake of argument, say I have a class Student.
public class Student {
public int StudentID;
public String firstName;
public String lastName;
}
There is a principle that you should rarely ask an object for information about itself, rather you should tell it what you want it to do, and let the object do the work itself.
To this end, I have the following methods
public class Student {
public int StudentID;
public String firstName;
public String lastName;
// Constructors
public Student () {}
public Student (int StudentID){
populateFromDataBase (StudentID);
}
private void populateFromDataBase (int StudentID){
// Get the data from the database and set the
// values of all the properties of this
}
public void save (){
// Save the values of the properties to db
}
}
This is so that other classes may use this class without caring how it persists it’s information.
Disclaimers: I know I am not using accessors, just public properties. I’m just trying to keep this example simple.
Don’t ask how an external class would know a StudentID, That’s irrelavent to the question I want to ask, which is this:
(Say) I want to draw a table of students and their details to the screen. From the UI class (Say a ListActivity in android) I could get an array of students, then loop through them, setting the properties of my ListView as I go. The problem I have with that is that I seem to be thinking far too procedurally, and not in the true spirit of object oriented design. It also requires asking each student object about itself, violating encapsulation.
Apparently (from what I read) the student should draw itself.
Here’s where I get confused. How can a student draw itself when it knows nothing of the UI? Do I pass a reference to the UI to the student object? Does this break the separation of presentation and business layers or not? What is considered good practice? Are there any articles or design patterns out there, preferably with example code, because I could not find any? Am I worrying about something not that important and should I just go with my first messy idea?
I really would appreciate any input, as clearly this is an issue that will reoccur with anything that I code.
Another possibility I considered was accessing the database directly from the UI and binding to a cursor, but that just seems wrong. or is it?
Opinions may vary, but IMHO, objects should not draw themselves or for that matter, save themselves to database.
For drawing, I would generally implement some form of double dispatch, such as the Visitor Pattern.
For separation from the UI, you should also consider Model-View-Controller, Model-View-Presenter or Model-View-ViewModel.
Persistence of objects can be rather more complex, and might involve assorted patterns, as described in Martin Fowler’s Patterns of Enterprise Application Architecture and summarized at Fowler’s website catalog.
And of course, the UI should not bypass the model and go straight to the database.