Simple custom event and Observer/Observable

V

VisionSet

I need to generate some simple events from my Model.
I'm using Observer/Observable interface/class, since I find the
PropertyChanged event framework over bulky for my needs. So the public
void update(Observable obj, Object evt) method will be used to deliver
events in the 2nd argument. The event class I have at present is as
follows:

public class ModelEvent {

public enum Type {PLACEMENT, PLUS_OTHER_EVENTS};

private final Object source;
private final Type type;

public ModelEvent(Object source, Type type) {
this.source = source;
this.type = type;
}

public Object getSource() {
return source;
}

public Type getEvent() {
return type;
}
}

I create a new event as so: new ModelEvent(this, ModelEvent.Type.PLACEMENT);

My GUI has interface references to the model to query state after receiving
an event.

I really don't want to build an elaborate event framework.
So I thought I'd rip the Observer/Observable code and bring it upto date
whilst giving type safety to the update/notifyObserver pair, replacing
Object with my ModelEvent

hence update(Object source, ModelEvent evt)

Now remains the source object, how to improve the type safety of that? In
this simple framework, I know it will be one of my model interfaces...

I suppose I can have my interfaces extend a tagging interface, but whats the
point I still could cast to the wrong Interface.

Can it be improved further? with generics?
Any other comments on my musings above?

TIA
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top