Archive

Posts Tagged ‘Listeners’

Java Events im Vergleich zu C# Events

18. Februar 2011 4 Kommentare

Ich mache hier mal ein Vergleich um zu zeigen, was ich mit unnötige Komplexität und Unübersichtlichkeit meine.

 


Erstellen von Custom Events

Java

Java
1
23
45
    public interface EventCustom extends EventListener
    {        public void fireEvent(EventObject e);
    } 
GeSHi 1.0.8.8
Java
1
23
45
67
89
1011
1213
1415
1617
18
    protected Vector<EventCustom> listenerList = new Vector<EventCustom>();
     public void addEventListener(EventCustom eventListener)
    {        listenerList.add(eventListener);
    }      public void removeEventListener(EventCustom eventListener)    {
        listenerList.remove(eventListener);    }
        private void fireEndOfGame(EventObject e)
    {        for(EventCustom listener: listenerList)
            listener.fireEvent(e);    }
 
GeSHi 1.0.8.8

C#

C#
1
    public delegate void EventCustomHandler(object sender);
GeSHi 1.0.8.8
C#
1
private EventCustomHandler CustomEvent;
GeSHi 1.0.8.8

 


Event aufrufen

Java

Java
1
fireEndOfGame(new EventObject(this));
GeSHi 1.0.8.8

C#

C#
1
if(CustomEvent!=null) CustomEvent(this);
GeSHi 1.0.8.8

 


Listener hinzufügen

Java

Java
1
23
45
67
89
anyObject.addEventListener(new EventCustom()
{    @Override
    public void fireEvent(EventObject e)    {
         // Bla bla bla    }
}); 
GeSHi 1.0.8.8

oder

Java
1
23
45
67
89
    private EventCustom eventCustom = new EventCustom()
    {        @Override
        public void fireEvent(EventObject e)        {
            // bla bla bla        }
    }; 
GeSHi 1.0.8.8
Java
1
anyObject.addEventListener(eventCustom);
GeSHi 1.0.8.8

oder

Java
1
23
45
67
89
1011
1213
1415
public class CustomEvent implements EventCustom
{    private Object sender;
            public CustomEvent(Object sender)
    {        this.sender = sender;
    }            @Override    public void fireEvent(EventObject e)
    {        // bla bla bla
    }}
GeSHi 1.0.8.8
Java
1
private CustomEvent customEvent = new CustomEvent(this);
GeSHi 1.0.8.8
Java
1
anyObject.addEventListener(eventCustom);
GeSHi 1.0.8.8

 

C#

C#
1
anyObject.CustomEvent += AnyObject_CustomEvent;
GeSHi 1.0.8.8
C#
1
23
4
private void AnyObject_CustomEvent(object sender) 
{     // bla bla bla
}
GeSHi 1.0.8.8

oder

C#
1
23
4
this.CustomEvent += delegate(object sender) 
{    // bla bla bla
};
GeSHi 1.0.8.8

 

So… Genug für heute….

Kategorien:.NET, C#, Java Schlagwörter: , , , ,