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

  • SEARCH
  • Home
  • 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 4248308
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T04:14:11+00:00 2026-05-21T04:14:11+00:00

I have three simple classes for illustration. People have many Projects which have many

  • 0

I have three simple classes for illustration. People have many Projects which have many Tasks.

public class Person {
  private ArrayList<Project> projects;

  public Person() {
    projects = new ArrayList<Project>();
  }

  public printTasks() {
    //TODO
  }
}

public class Project {
  private String name;
  private ArrayList<Task> tasks;

  public Project(String name) {
    this.name = name;
    tasks = new ArrayList<Task>();
  }

  public getName() {
    return name;
  }
}

public class Task {
  private String name;
  private String description;

  public Task() {
    tasks = new ArrayList<Task>();
  }

  public getName() {
    return name;
  }

  public getDescription() {
    return description;
  }
}

My problem relates to what would be the best way to implement Person.printTasks()..I want to display on stdout a list of the project names a person has and after each project, a list of the task names + descriptions that project has.

  1. Create Task.printTask() and have that print out out the name and description for that task. Create Project.printTasks() and have that print out the project name and then call printTask on each task. Have Person.printTasks call printTasks on each project.
  2. Create Task.getTaskString() and have that concatenate the name and description for that task and return it. Create Project.getTasksString() and have create a string containing the project name and then call getTaskString on each task, concatenating each result. Have Person.printTasks() call getTasksString on each project and print out each string.
  3. Create Project.getTasks() and have it return the ArrayList of tasks. Have Person.printTasks() call Project.getName and Project.getTasks() for each project, print the project name and then iterate over the returned tasks array, calling getName() and getDescription() on each and printing them out.

Is there a best way to handle this or does it really not matter? Personally, I am leaning towards 2) because 1) ties the idea of how an object should be displayed to that object but it feels like this should be decided in a single place i.e. Person. That way I can easily change from stdout to a file for example. 3) gives Person direct access to Task, a class which it previously was not coupled to (at least it was only coupled indirectly through Project).

Is there a better solution than these 3?

Note: This could be a language-agnostic question but I happen to be writing in Java and I imagine that there very well could be different best solutions for different languages so I would like answers to at least address this problem with regards to Java.

  • 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-21T04:14:12+00:00Added an answer on May 21, 2026 at 4:14 am

    Visitor pattern: nobody knows how to print and what to print. You just give project/person/task in whatever relationsship they are a visitor object. project/person/task iterate over objects they own/know about and pass the visitor to them.

    The visitor does the right thing – it knows how to access information from objects it visits and if the information should be printed and where: console/file/database or whatever, it also know how to format: xml/html/plain text/csv come to mind as examples.

    This was you are free to design your project hierarchy whatever you will, change printing target and formats etc.

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

Sidebar

Related Questions

Let's assume I have three classes that are subclasses of a base class: public
I have three coffeescript classes, set up like this: class A class C extends
I have three classes: Class A { string name IList<AB> list } Class B
I have built a simple rails app with three classes that inherit from ActiveRecord
Hi I'm using ms2005 for a simple calendaring system. We have three 'legacy' tables:
I have a simple JavaScript file that has three jQuery $document.ready functions. All three
I have a simple object that allows you to assign three properties (x,y,z) (lets
I have a very simple Setup project that copies three dlls into the GAC.
I have a simple tree which takes the shape below ROOT /\ A B
I have a problem with a makefile. I have three classes. They do pretty

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.