now this is interesting - interfaces and reflection?
White brendan
Greenhorn
Joined: Jan 10, 2003
Posts: 6
posted
0
hi there, this one is really confuculating me... let me start. I have developed a gateway for session-based mobile communication (using a control channel in GSM spec), I have a communication class to maintain the physical connection, which then passes on the packets received to a handler. The handler routes the packets to different applications according to what the users request. Because this is session based, I have to maintain state of where each person is, and what application they are using. I do this by using a standard interface that each application must implement. I then store each persons number and application in a hashtable and reference his application by his number. as follows: //////retrieving the class and inserting into hashtable, first session/////// Class cls = Class.forName(classname); Object obj = cls.newInstance(); CommandProcess cmdp = (CommandProcess)obj; //CommandProcess is interface
ht.put(address,cmdp); /////second session: retrieving instance of the application from the hash map according to the number (address) of the user///// Object classname = ht.get(address); //gets the application from the hashtable according to the address(number) of the user. CommandProcess cmdp = (CommandProcess)classname; //CommandProcess is the interface push(cmdp.processCommand(address, msg),"****"); //processCommand is the only method in the interface, push is method in handler My problem is that each application instantiates a monitoring class from its constructor. I can use the processCommand method in the application from the handler (because it is in the interface), but how do I access any other fields or methods in the application? I need to access fields in the monitoring class eg. cmdp.monitor.getPick() . If I put the monitoring class in the interface: Cmonitor monitor = new Cmonitor(); ,Every time I try and access any fields as above, a new instance of Cmonitor is created and all my fields are reset. Must I use reflection? how would I do it? Thanks Brendan White South Africa
Maulin Vasavada
Ranch Hand
Joined: Nov 04, 2001
Posts: 1863
posted
0
hi Brendan, so what i get from your post is, - u have an interface implemented by each application - u have a monitor class that is instantiated by each application (EACH APPLICATION WILL SURELY HAVE THIS MONITOR OBJECT FOR ITSELF) - u want to be able to access monitor object's some methods (e.g. getPick()) for ur purposes - the problem is that monitor class intance that is there in each application is not accessible as you are dealing with interface object when retrieved from the hashtable... right??? well, there r two types of delegations u can use here as per your need and design, 1. u delegate calls to monitor object's method to the application via the interface i.e. in interface u declare getPick() method implemented by each application which will invoke getPick() on monitor object in trun... 2. u declare a method getMonitor() in the interface and each application returns local object for that. then if u r having a interface object (i mean a variable declared to be of interface type to hold the application object), u can invoke, CommandProcess cmdp = (CommandProcess)obj; cmdp.getMonitor().getPick()... can' u? i hope i understood ur problem... regards maulin.