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?
Not quite an answer, you can construct a task from elsewhere but you need a valid task instance to do it..
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?