Delegates...?
I am trying to write an application using the "Presenter First"
methodology that I use when coding in C# but am a bit unsure how to get
round the fact that Java does not have delegates.
In C# I would create a Model class which has the actual application
functions in it, a view class which is a dumb GUI and then have a
presenter class which binds the two together. The presenters constructor
takes references to the model and view then calls methods in the model and
view classes to point a delegate to the method in the presenter. So
basically the presenter knows about the model and view but the model and
view simply fire events which call methods in the presenter.
If anybody could enlighten me here it would be great, its years since I've
done any Java and I'm just trying to get back into it. I've included some
example C# code which probably explains better what I'm trying to do.
thanks
Gav
public delegate void EventDelegate();
public class Model
{
private event EventDelegate Test;
public Model()
{
}
pivate void CallTest()
{
if ( this.Test != null )
this.Test();
}
public void SubscribeTestEvent(EventDelegate listener)
{
this.Test += listener;
}
}
public class View
{
private event EventDelegate Test2;
public View()
{
}
private void CallTest2()
{
if ( this.Test2 != null )
this.Test2();
}
public void SubscribeTest2Event(EventDelegate listener)
{
this.Test2 += listener;
}
}
public class Presenter
{
private Model myModel;
private View myView;
public Presenter(Model pModel, View pView)
{
this.myModel = pModel;
this.myView = pView;
this.myModel.SubscribeTestEvent(TestMethod);
this.myView.SubscribeTest2Event(TestMethod2);
}
private void TestMethod()
{
// Do Something
}
private void TestMethod2()
{
// Do Something
}
}