Вопрос по кодонаписанию
От: Тычеблин Китай  
Дата: 08.01.06 04:13
Оценка: 4 (1)
Привет Всем

Сначала думал оформить в контексте Вопрос по правильному кодонаписанию
Автор: leska
Дата: 06.01.06
потом передумал. У меня вопрос тоже по стилю но о другом.

Предлагаю обсудить то, как можно было бы более правильно и красиво реализовать подписку на собылия объекта и отказ от них.
то как это сделано в AWT/SWING например у Component
    public synchronized void addComponentListener(ComponentListener l)
    public synchronized void removeComponentListener(ComponentListener l)
    public synchronized ComponentListener[] getComponentListeners()

    public synchronized void addFocusListener(FocusListener l)
    public synchronized void removeFocusListener(FocusListener l)
    public synchronized FocusListener[] getFocusListeners()

    public void addHierarchyListener(HierarchyListener l)
    public void removeHierarchyListener(HierarchyListener l)
    public synchronized HierarchyListener[] getHierarchyListeners()

    public void addHierarchyBoundsListener(HierarchyBoundsListener l)
    public void removeHierarchyBoundsListener(HierarchyBoundsListener l)
    public synchronized HierarchyBoundsListener[] getHierarchyBoundsListeners()

    public synchronized void addKeyListener(KeyListener l)
    public synchronized void removeKeyListener(KeyListener l)
    public synchronized KeyListener[] getKeyListeners()

    public synchronized void addMouseListener(MouseListener l)
    public synchronized void removeMouseListener(MouseListener l)
    public synchronized MouseListener[] getMouseListeners()

    public synchronized void addMouseMotionListener(MouseMotionListener l)
    public synchronized void removeMouseMotionListener(MouseMotionListener l)
    public synchronized MouseMotionListener[] getMouseMotionListeners()

    public synchronized void addMouseWheelListener(MouseWheelListener l)
    public synchronized void removeMouseWheelListener(MouseWheelListener l)
    public synchronized MouseWheelListener[] getMouseWheelListeners()

    public synchronized void addInputMethodListener(InputMethodListener l)
    public synchronized void removeInputMethodListener(InputMethodListener l)
    public synchronized InputMethodListener[] getInputMethodListeners()

лично я нахожу откровенно корявым и загаживающим API. (нет? )

более элегантным решением могло бы стать что-то вроди
    public ComponentListener.Hub onComponentEvent=....
    public FocusListener.Hub onFocusEvent=....
    public HierarchyListener.Hub onHierarchyEvent=....

........

        public interface ComponentListener extends EventListener
    {
        public void componentResized(ComponentEvent e);
        public void componentMoved(ComponentEvent e);
        public void componentShown(ComponentEvent e);
        public void componentHidden(ComponentEvent e);
        
        public class Hub
        {
            public void add(ComponentListener c){};
            public void del(ComponentListener c){};
            public ComponentListener get(){};
        }
        
    }
........

таким образом использование будет выглядить так
......
comp.onComponentEvent.add(componentListener);//добавление обработчика
comp.onComponentEvent.del(componentListener);//удаление обработчика
comp.onComponentEvent.get().componentShown(componentEvent);//вызов (invoke) у всех зарег. обработчиков componentShown(ComponentEvent e)
.....

Но (!) как это может быть устроено внутри? что думаете?
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.