.net的event事件模型和java的interface事件模型

最近学习了justin的观察者模式, 感觉似乎了解了.net的event模型和java的interface模型的区别。
个人理解的是,.net的event模型是针对方法的接口(定义了其他类的方法如果需要被该类的事件触发的话那些方法必须满足的规则),java的是针对类的接口(定义了其他类的方法如果需要该类的方法被触发的话那些类必须满足的规则)。下面的例子能显示这两种的区别:

java的:

 1package event; 
 2
 3/** 
 4 * Button class 
 5 * 
 6 * @author 
 7 * @version 1.0 
 8 */
 
 9public class Button 
10
11   /** 
12     * IButton interface implementer that will set up the button's click method 
13     */
 
14   private IButton iButton = null
15    
16   /** 
17     * Method that will set the IButton interface 
18     * 
19     * @param listener 
20     */
 
21   public void addClickListener(IButton listener) 
22   
23      this.iButton = listener; 
24   }
 
25    
26   /** 
27     * The class's click event that will be fired when the button is clicked 
28     */
 
29   public void raiseClick() 
30   
31      if (iButton != null
32      
33         iButton.click(); 
34      }
 
35   }
 
36}
 

 1package event; 
 2
 3/** 
 4 * Interface for button 
 5 * 
 6 * @author 
 7 * @version 1.0 
 8 */
 
 9public interface IButton 
10
11   /** 
12     * method that classes implement this interface must have 
13     */
 
14   void click(); 
15}

 1package event; 
 2
 3/** 
 4 * Adapter class that will hold a default click method with nothing inside 
 5 * 
 6 * @author 
 7 * @version 1.0 
 8 */
 
 9public class ListenerAdapter implements IButton 
10
11   /* 
12     * (non-Javadoc) 
13     * 
14     * @see event.IButton#click() 
15     */
 
16   public void click() 
17   
18   }
 
19}
 
20

 1package event; 
 2
 3/** 
 4 * Example class for the pane which holds a button class 
 5 * 
 6 * @author 
 7 * @version 1.0 
 8 */
 
 9public class Pane 
10
11   /** 
12     * Element of the pane 
13     */
 
14   public Button button; 
15    
16   /** 
17     * Method that will initialize the pane 
18     */
 
19   public void initialize() 
20   
21      button = new Button(); 
22      button.addClickListener(new ListenerAdapter() 
23      
24         /* (non-Javadoc) 
25          * @see event.ListenerAdapter#click() 
26          */
 
27         public void click() 
28         
29            System.out.println("clicked"); 
30         }
 
31      }
); 
32   }
 
33   /** 
34    * Constructor 
35    */
 
36   public Pane() 
37   
38      initialize(); 
39   }
 
40}
 
41

1Pane pane = new Pane(); 
2pane.button.raiseClick();


c#的:

 1using System; 
 2using System.Collections.Generic; 
 3using System.Text; 
 4
 5namespace EventCS 
 6
 7    public class Button 
 8    
 9        //Click event's delegate, which will find the methods to call when the event is raised 
10        public delegate void ClickHandler(); 
11        //Event click 
12        public event ClickHandler Click; 
13        //Raise the click event to the outside of the class 
14        public void RaiseClick() 
15        
16            if (Click != null
17            
18                Click(); 
19            }
 
20        }
 
21    }
 
22}
 

 1using System; 
 2using System.Collections.Generic; 
 3using System.Text; 
 4
 5namespace EventCS 
 6
 7    public partial class Pane 
 8    
 9        //Property button 
10        public Button button; 
11        //Method that will initialize the container class 
12        public void Initialize() 
13        
14            button = new Button(); 
15            button.Click += new Button.ClickHandler(Pane_Click); 
16        }
 
17        //Constructor 
18        public Pane() 
19        
20            Initialize(); 
21        }
 
22    }
 
23}
 

 1using System; 
 2using System.Collections.Generic; 
 3using System.Text; 
 4
 5namespace EventCS 
 6
 7    public partial class Pane 
 8    
 9        public void Pane_Click() 
10        
11            //Method that will be executed when the notice of button click event arrived 
12            Console.WriteLine("clicked"); 
13        }
 
14    }
 
15}

1Pane pane = new Pane(); 
2pane.button.RaiseClick();

vb.net的:

 1Imports System 
 2Imports System.Collections.Generic 
 3Imports System.Text 
 4
 5Namespace EventVB 
 6    Public Class Button 
 7        'Click event's delegate, which will find the methods to call when the event is raised 
 8        Public Delegate Sub ClickHandler() 
 9        'Event click 
10        Public Event Click As ClickHandler 
11        'Raise the click event to the outside of the class 
12        Public Sub RaiseClick() 
13            If Not ClickEvent Is Nothing Then 
14                ClickEvent.Invoke() 
15            End If 
16        End Sub
 
17    End Class
 
18End Namespace

 1Imports System 
 2Imports System.Collections.Generic 
 3Imports System.Text 
 4
 5Namespace EventVB 
 6    Partial Public Class Pane 
 7        'Property button 
 8        Public button As Button 
 9        'Method that will initialize the container class 
10        Public Sub Initialize() 
11            button = New Button 
12            AddHandler button.Click, New Button.ClickHandler(AddressOf Pane_Click) 
13        End Sub
 
14        'Constructor 
15        Public Sub New() 
16            Initialize() 
17        End Sub
 
18    End Class
 
19End Namespace
 
20

 1Imports System 
 2Imports System.Collections.Generic 
 3Imports System.Text 
 4
 5Namespace EventVB 
 6    Partial Public Class Pane 
 7        'Method that will be executed when the notice of button click event arrived 
 8        Public Sub Pane_Click() 
 9            Console.WriteLine("clicked"
10        End Sub
 
11    End Class
 
12End Namespace

1Dim pane As Pane = New Pane() 
2pane.button.RaiseClick()
posted @ 2007-07-01 01:41  N/A2011  阅读(588)  评论(0编辑  收藏  举报