Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6042279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:46:21+00:00 2026-05-23T06:46:21+00:00

this my first question on here. I hope someone can help. It is to

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T06:46:21+00:00Added an answer on May 23, 2026 at 6:46 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.