java - Multiple event listener and adapter -



java - Multiple event listener and adapter -

i creating custom event listener tree. have created listener interface here

public interface treeactionlistener extends eventlistener { public void onaddnode(treeeventobject eventobject); public void onremovenode(treeeventobject eventobject); public void onrenamenode(treeeventobject eventobject); public void oncreatenode(treeeventobject eventobject); }

also i've created custom event object named treeeventobject here code

public class treeeventobject extends eventobject { private object datatopass =null; /** * constructs prototypical event. * * @param source object on event occurred. * @throws illegalargumentexception if source null. */ public treeeventobject(object source, object datatopass) { super(source); this.datatopass = datatopass; } public object getpasseddata() { homecoming this.datatopass; } }

and mentioned in jdk pattern adapter used. used when have lot of methods in listener interface, need 1 method. have created adapter

public class treeactionadapter implements treeactionlistener { @override public void onaddnode(treeeventobject eventobject) { } @override public void onremovenode(treeeventobject eventobject) { } @override public void onrenamenode(treeeventobject eventobject) { } @override public void oncreatenode(treeeventobject eventobject) { } }

but little confused 1 thing. want write code clean possible , utilize patterns. wonder elegant way next. have class has listener field. have 4 different events can occurred. right way fire events. have ideas, don't know better.

create 1 fireevent method should check type of event has been performed. in case of else if. , if way should create illustration enum , create field type in event object , in constructor set it, , check later in fireevent method. , depending on type phone call different methods listeners list. instance :

if(event.type==node_add) { (treeactionlistener listener: listeners) { listener.onaddnode(event); } }else if(event.type==node_delete) { (treeactionlistener listener: listeners) { listener.ondeletenode(event); } }

use different types of fireevent methods : fireaddevent,firedelete event....... , depending on phone call different listener methods.

create separate arrays each type of listener. , utilize special fireevent method too.

i don't know improve , practice. please suggest other implementation or best listed before. in advance

i consider having 4 different fire methods. changing behavior off of method parameter, isn't clean me.

just have class have add/remove listener methods (add/remove listeners sort of collection). 4 fire methods (one each type of event, loop though listeners calling right method) , think life should pretty good.

java android events listener

Comments

Popular posts from this blog

java Multi query from Mysql using netbeans -

c# - DotNetZip fails with "stream does not support seek operations" -

c++ - StartServiceCtrlDispatcher don't can access 1063 error -