When developing a program in the above language, I was faced with the following question: where is it better to declare listeners for visual elements?

The essence of my task is as follows: developing an engineering calculator. I estimated approximately, on the frame not less than 50 buttons will be placed

The ClInitVisualElements class declares and initializes the form with 50 buttons on it in its procedures.

I am stumped when creating listeners for these window elements. I understand that you can declare listeners:

  • In the constructor (but I immediately reject this option), since more than one person explained to me that in the constructor there should be only a call to a single procedure

  • In a separate procedure of the ClInitVisualElements class

  • In a separate method of which other class, for example, CInitFrameListeners and call this method when creating visual elements

If anyone has experience in developing JAVA programs, then tell me, please, what would be the right thing to do from the professional point of view of code organization?

Thanks in advance.

  • The classic example of the Observer pattern. It is not clear why to invent your bicycle. - test81278

1 answer 1

everything is very simple:

Suppose you have the following classes:

 class CalcEvent {...} // базовый класс для ивентов калькулятора (будете от него наследоватся для разных типов ивентов, напр ввод команды/ввод значения) class CalcFrame // предполагается что это класс вашего окна калькулятора { private CalcEngine engine = null; public abstract void generateDummyEvent () { // generate event engine.handleEvent ( new CalcEvent() ) ; } } class CalcEngine implements CalcEventListener // класс который обрабатывает и распределяет ивенты { private List<CalcEventListener> listeners = new LinkedList<CalcEventListener>(); // ... другие поля/методы... public void handleEvent () { for ( CalcEventListener l : listeners ) { l.handleEvent(e); } } } interface CalcEventListener { public void handleEvent(CalcEvent event); } 

where the engine will be the only listener of your "calculator". This will be the first use of the Observer pattern.

and then, inside the engine itself, do the processing and distribution of events depending on how convenient: if / switch / observer pattern

UPDATE: made the example more detailed

  • I decided to organize a separate handler for each element, this will help me to avoid from 1 to 50 (and possibly more) verification operations in the default case. This will allow me to save machine time (although a few milliseconds, but for me this is a priority), but surely you will need to allocate more RAM for program execution. My problem is. what I don’t know where to perform declarations of listeners for these 50 elements: execute in the class method that is responsible for building graphics, or execute in the method of a separate class that will be responsible only for declaring listeners - pj-infest
  • you probably do not understand the essence of the idea. I suggest you hang up on each button the generation of the event after which it will be processed by the engine class. and already in the engine class you will distribute the next event to someone, - 50 ifs and switches - jmu
  • I understood your idea, but it does not solve the problem of performing a large number of checks, so for each element a separate listener will be created (or as many as needed) as advised in the previous topic hashcode.ru/questions/63724/… - pj-infest
  • The problem was that I knew where it would be better to describe these 50 listeners. on another resource, I was advised to describe the element listener in the same place where it is initialized and its parameter is set. so probably I will. Thanks for your help. - pj-infest
  • I don’t think that it makes sense to create a lot of different handlers, I think that you need only 2 (for a start): 1) processes data input 2) processes command input you either enter data or enter some command to perform an action on data - jmu