2

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.

2 Answers 2

2

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

If it's for displaying task names only and you don't want Person depend on Task, then I suggest adding a method

 List<String> getTaskNames();

to the Project class. That's what a project should be able to tell: the names of its tasks.

Or create an extra TaskMetaData class, that holds all properties, a person needs to know about a task, and return a collection of TaskMetaData objects. I'd prefer that over concatenating Strings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.