I have a class Presenter
deciding which "class" is displayed to the user:
public class Presenter
{
public MyObject ActiveObject { get; set;}
}
All my classes which can be presented are derived from MyObject
:
public abstract class MyObject
{
protected Presenter Presenter;
}
One of the classes which can be presented is Child
:
public class Child : MyObject
{
public void Find()
{
Results results = ChildSearch.Find(this);
Child newChild = new Child(results);
Presenter.ActiveObject = newChild;
}
}
When the Find()
method is called a new child is created and sent to the Presenter so it shows the "new" child.
This works quite well.
Now I have a use case that there is also a Parent
class. It has a child and then the presenter shows "both" (a transport would be the parent and the driver the child, the presenter shows transport with its driver).
Inside the Parent a search for data of the child can be done (like the name of a driver):
public class Parent : MyObject
{
private Child child;
public void FindChild()
{
child.find();
}
}
But in this case the new Parent should be shown by the presenter. Instead, as you can see in the Find()
of Child
the child is shown by the presenter.
This shows that my design is bad but I really don't have a clue how this could be done.
If you know a better title for this problem please suggest one. Is there any pattern fitting to my problem?
Let your method only do one thing. Do the set active in the base class, and overload the find method in parent and child, this way every kind of MyObject can define which object has to be active.