+ All Categories
Home > Documents > 13. Delegates & events

13. Delegates & events

Date post: 02-Feb-2016
Category:
Upload: aulii
View: 34 times
Download: 5 times
Share this document with a friend
Description:
13. Delegates & events. Observer pattern Delegates Semantics Cil – kode Anvendelse Events. Observer pattern. Also called Observable in some litterature. Might also be a interface. Callback interfaces. Interfaces describes a common behavior that is shared by different classes - PowerPoint PPT Presentation
21
1 .NET Delegates & events NOEA / PQC 2009 13. Delegates & events Observer pattern Delegates – Semantics Cil – kode – Anvendelse Events
Transcript
Page 1: 13. Delegates & events

1 .NET Delegates & events NOEA / PQC 2009

13. Delegates & events

• Observer pattern• Delegates

– Semantics– Cil – kode– Anvendelse

• Events

Page 2: 13. Delegates & events

2 .NET Delegates & events NOEA / PQC 2009

Observer pattern Also called Observable in some litteratureMight also be a

interface

Page 3: 13. Delegates & events

3 .NET Delegates & events NOEA / PQC 2009

Callback interfaces

• Interfaces describes a common behavior that is shared by different classes

• Used in Observer pattern to construct a callback mecanism– The key is the Update() method in the Observer interface.– Note that it do not have to be named Update ;-)

• Callback can be used in languages that supports interfaces• Callback can be implemented with function pointers in C

(But then it is not by using observer pattern)

Page 4: 13. Delegates & events

4 .NET Delegates & events NOEA / PQC 2009

• Subject (or Observable) implements an add and a remove method to add and remove observers to/from an internal container (typical an arraylist)

• Has also a notify() method that calls update() on all observer object in the container

• Observer implements the update() method. • That will be the event handler in an event based system

Page 5: 13. Delegates & events

5 .NET Delegates & events NOEA / PQC 2009

Delegate

• Delegate is a new language construction in C#• Java programmers will compare it to observer pattern• C/C++, Pascal/Delphi… programmers will compare it to function

pointers

• A delegate gives the possibility to pass a method of a class as a parameter to objects of another class. The method can be a static or an instance method

• Delegates are object oriented, typesafe and undependent of method name.

• Delegates are objects itselves of classes that iniherit from System.Delegate via System.MulticastDelegate

Page 6: 13. Delegates & events

6 .NET Delegates & events NOEA / PQC 2009

St r i ng Ar r ay Val ueType Except i on Del egat e Cl ass1

Mul t i castDel egat e

Cl ass2

Cl ass3

Obj ect

Enum1

St r uct ur e1EnumPr i mi t i ve t ypes

Bool ean

Byt e

I nt 16

I nt 32

I nt 64

Char

Si ngl e

Doubl e

Deci mal

Dat eTi me

System-defined types

User-defined types

Del egat e1

Ti meSpan

Gui d

Page 7: 13. Delegates & events

7 .NET Delegates & events NOEA / PQC 2009

4 steps to create and use delegates

1. Declare a delegate with an appropiate signature

2. Define method(s) with the same signature

3. Instansiate a delegate object with a method as parameterThe method will be encapsulated in the delegate

4. Call the method by the delegate

Page 8: 13. Delegates & events

8 .NET Delegates & events NOEA / PQC 2009

Step 1:

1. Declare a delegate with an appropiate signature.That means with the same parameters and return values as the method to be encapsulated

delegate void Notifier(string sender);

Page 9: 13. Delegates & events

9 .NET Delegates & events NOEA / PQC 2009

Step 2:

• Define method with the same signature as the delegate

void SayHello (string sender){

Console.WriteLine("Hello from "+ sender);}

void SayGoodBye (string sender){

Console.WriteLine("Good Bye from "+ sender);}

Page 10: 13. Delegates & events

10 .NET Delegates & events NOEA / PQC 2009

Step 3:

• Instanstiate the delegate object and add the methods• Note: Static methods might be used as well.

HelloGoodbye MyObject=new HelloGoodbye();

//Instanstiate delegate and thereby a containerNotifier notify = new Notifier(MyObject.SayHello);

//Add a new Notifier to the container (multicast); notify += new Notifier(MyObject.SayGoodBye);

//For the example: remove Notifier:notify -= new Notifier(SayHello);

Page 11: 13. Delegates & events

11 .NET Delegates & events NOEA / PQC 2009

Trin 4:

• Call the methods via the delegate:

notify ("Pingo");

Page 12: 13. Delegates & events

12 .NET Delegates & events NOEA / PQC 2009

Demo

• Incl. ildasm

Page 13: 13. Delegates & events

13 .NET Delegates & events NOEA / PQC 2009

Events

• Delegates are often used for event based systems• The Event keyword in C# makes it little easier• The mecanism behind is the same. A event is a kind of delegate.• Events are often used in object oriented gui programs • Events can be used with advantage in other areas such as

simulation and realtime systems.

Page 14: 13. Delegates & events

14 .NET Delegates & events NOEA / PQC 2009

Gui demo

• To see the use of event handlers

Page 15: 13. Delegates & events

15 .NET Delegates & events NOEA / PQC 2009

Events (not gui)

1. Declare a delegate with an appropriate signature

2. Declare the events a class can send

3. Define event handlers (methods)

4. Associate event and eventhandler

5. Trigger event by calling event handler by the event name.

Page 16: 13. Delegates & events

16 .NET Delegates & events NOEA / PQC 2009

Step 1:

1. Declare a delegate with an appropriate signature

delegate void GreetingHandler(string sender);

Page 17: 13. Delegates & events

17 .NET Delegates & events NOEA / PQC 2009

Trin 3:

• Declare the events a class can send.• Events are members in the class they are generated from

public static event GreetingHander Hello;public static event GreetingHander GoodBye;

Page 18: 13. Delegates & events

18 .NET Delegates & events NOEA / PQC 2009

Step 3:

• Define methods with the same signature as the delegate

void OnHello (string sender){

Console.WriteLine("Hello from "+ sender);}

void OnGoodBye (string sender){

Console.WriteLine("Good Bye from "+ sender);}

Page 19: 13. Delegates & events

19 .NET Delegates & events NOEA / PQC 2009

Trin 4:

• Instanstiate the delegate object and add the methods that shall be called

MinKlasse.Hello+=new MinKlasse.GreetingHandler(OnHello);MinKlasse.GoodBye+=new MinKlasse.GreetingHandler(OnGoodBye);

Event name in the class Delegate

name in the class

Event handler (method to be

called)

Page 20: 13. Delegates & events

20 .NET Delegates & events NOEA / PQC 2009

Step 5:

• Call the methods by the event name:

// Somewhere in a class..

if(Hello != null) //Be sure there is an eventhandler{

Hello("Pingo");}

Page 21: 13. Delegates & events

21 .NET Delegates & events NOEA / PQC 2009

What is the difference between delegate and event?

• Fromhttp://en.csharp-online.net/CSharp_FAQ:_What_is_the_difference_between_delegate_and_event

• An event offers more restricted access than a delegate. When an event is public, other classes are only able to add or remove handlers for that event: such classes cannot—necessarily—fire the event, locate all the handlers for it, or remove handlers of which they are unaware..


Recommended