Where should I locate an Event Listener? Basics of OOP

77 views Asked by At

After a few years I'm trying to get back to programming and therefore i am trying to programm a relatively simple app in android studio.

Im still at the part of thinking how the programm should be lazed out, where to place which attributes and methods. Since i would like to programm this app neatly. Its supposed to be an App which can log parameters of a specific event. For this event (sudden change of the devices orientation), I will create a class in which the data (e.g. duration, angle...) is stored.

I obviously need some sort of Listener that is checking if the event is happening. Where do I locate this method/listener?

Here are my thoughts about this:

Should it be a method of the class of this event? This seems to make sense until I have to implement it. To call this method I would have to create an instance of this object an than call the Listener. Once the event has happened I would save this instance to the database. This seems kind of odd to me.

On the other hand i could create the function/method somewhere else and only create an an instance of the event once the event is happening. Would the listener be a class for its own, of which I create an Instance once the tracking is activated?

If there is a (short) document regarding these basics im happy to read that. But since i#m still doing this for fun, i#m noot looking for a 400 page book about OOP. I appreciate any helpful comment regarding this topic!

1

There are 1 answers

0
Peter Seliger On

I tried to sketch the most needed basic structure of the JavaScript implementation which did answer following SO question ... "How to implement an event dispatching system for ES/JS object types?"


+- EventTarget or Observable (module) -----------------------------+
|                                                                  |
|    (*) optional                                                  |
|                                                                  |
|    +- Event (class) ------+                                      |
|    |                      |                                      |
|    |  uuid:string         |                                      |
|    |  type:string         |                                      |
|    |  target:EventTarget  |                                      |
|    |  data*:Object        |                                      |
|    |                      |                                      |
|    +----------------------+                                      |
|                                                                  |
|    +- EventListener (class) ----+                                |
|    |                            |                                |
|    |  target:EventTarget        |                                |
|    |  type:string               |                                |
|    |  handler:function          |                                |
|    |                            |                                |
|    |  handleEvent( evt:Event )  |                                |
|    |                            |                                |
|    +----------------------------+                                |
|                                                                  |
|    +- EventTarget (class) or Observable (mixin) ------------+    |
|    |                                                        |    |
|    |  addEventListener( type:string, handler:function )     |    |
|    |  removeEventListener( type:string, handler:function )  |    |
|    |  dispatchEvent( evt:Event|Object )                     |    |
|    |                                                        |    |
|    +--------------------------------------------------------+    |
|                                                                  |
+------------------------------------------------------------------+