tags:

views:

73

answers:

2

Given the following code:

Class User{
  Task m_Task;  
  public function getTask("Do work") { return m_Task; }
}

Class Project{
  Task m_Task;  
  public function getTask("Do work") { return m_Task; }
}

Class Task {
  private m_Name;
  public Task(name) { m_Name = name; }
}

Class Evil {
  new Task = Error
}

In a language that does not support multiple inheritance, nested classes, or private classes, (private constructor not an option), how can you design a requirement that Task is only ever instantiated through User or Project? Ideally using only language constructs instead of code. Is there a design pattern?

A: 

It seems somewhat quixotic, but I suppose you write Task a constructor that requires a User as an argument and does some handshaking back and forth to link the User to the Task, requiring that the User does not already have a Task.

chaos
A: 

Not quite an answer, you can construct a task from elsewhere but you need a valid task instance to do it..

public class Task {
   public Task (User u, String name)
   {
     if (u==null) throw new UnsupportedOperationException("that's cheating");
     this.name=name;
   }
}

Utlimately, if you're creating the requirement that Task be external to user then something else needs to be able to load task's constructor so I don't think it's possible.

What's the purpose of this question? Does this relate in any way to some real-world situation?

Steve B.