Interfaces, Callbacks, Delegates and Events
Interfaces, Callbacks, Delegates and Events
Interfaces, Callbacks
Delegates and Events
Contents
Introduction to interfaces
Example An IMeasureable interface
Callback functions
Delegates
Events and event handlers
Summary
Introduction to interfaces
Introduction to interfaces
For example, we may want to sort objects
of class Square on the basis of the length of
their side
We could easily do this through
polymorphism by implementing an abstract
base class Sortable and using similar
generic programming techniques we looked
at in the last lecture
But, C# (along with Java) doesnt
support multiple inheritance
Introduction to interfaces
Sortable
Shape
Square
Introduction to interfaces
Introduction to interfaces
Introduction to interfaces
compareTo(ISortable b) returns 1,0 or 1
depending on whether some chosen instance
field f of a ISortable object is such that :
this.f<b.f
this.f==b.f
this.f>b.f
We can implement this method in class
Square to sort on the basis of length of side
Introduction to interfaces
public class Square : Shape, ISortable
{
private int side;
public Square(int s) { side = s; }
public override void draw() { }
public override double area() { return side * side; }
public int compareTo(ISortable s)
{
Square sq=(Square)s;
if (side<sq.side) return -(1);
if (side > sq.side) return 1;
return 0;
}
}
Introduction to interfaces
Introduction to interfaces
public class ArrayAlg
{
public static void shellSort(ISortable[] a)
{
int n = a.Length;
int incr = n / 2;
while (incr >= 1)
{
for (int i = incr; i < n; i++)
{
ISortable temp = a[i];
int j = i;
while (j >= incr && temp.compareTo(a[j - incr]) < 0)
{
a[j] = a[j - incr];
j -= incr;
}
a[j] = temp;
}
incr /= 2;
}
}
}
Introduction to interfaces
Introduction to interfaces
public class SortablesTest
{
static void Main(string[] args)
{
Square[] s = new Square[3];
s[0] = new Square(3);
s[0] = new Square(2);
s[0] = new Square(1);
ArrayAlg.shellSort(s);// Sorts on length of side
}
}
An IMeasureable interface
Input stream
DataSet
average
maximum
An IMeasureable interface
An IMeasureable interface
public interface IMeasureable
{
double getMeasure();
}
An IMeasureable interface
The IMeasurable interface expresses the
commonality amongst objects
The fact that each measurable objects can
return a value relating to its size
DataSet objects can then be used to analyse
collections of objects of any class
implementing this interface with minor
modifications to the code
An IMeasureable interface
class MeasureablesTest
{
static void Main(string[] args)
{
DataSet d=new DataSet();
Coin c1=new Coin(10);
Coin c2=new Coin(20);
d.add(c1);
d.add(c2);
double maxCoin=d.getMaximum();
System.Console.WriteLine("coin max= " + maxCoin);
}
}
Callback functions
Callback functions
Callback functions
public class DataSet
{
public DataSet(IMeasurer m)
{
measurer=m;
}
public void add(Object x)
{
sum=sum+measurer.measure(x);
if (count==0 || maximum<measurer.measure(x))
maximum=measurer.measure(x);
count++;
}
public double getMaximum()
{
return maximum;
}
private
private
private
private
IMeasurer measurer;
double sum;
int count;
double maximum;
Callback functions
Callback functions
public class Square : Shape
{
private int side;
public Square(int s) { side = s; }
public override void draw() { }
public override double area() { return side * side; }
}
public class SquareMeasurer : IMeasurer
{
public double measure(Object anObject)
{
Square sq=(Square) anObject;
return sq.area();
}
}
Callback functions
class MeasurersTest
{
static void Main(string[] args)
{
IMeasurer m = new SquareMeasurer();
DataSet data = new DataSet(m);
// Add squares to the data set
data.add(new Square(5));
data.add(new Square (30));
// Get maximum
double max = data.getMaximum();
}
}
Callback functions
Delegates
Delegates
Delegates
Delegates
myDelegate(int,double)
call
aMethod()
anotherMethod()
Invoked by
call(1,2.0)
aMethod(1,2.0)
anotherMethod(1,2.0)
Delegates
public delegate void myDelegate(int arg1, double arg2);
public class App
{
public static void Main()
{
myDelegate call = new myDelegate(aMethod);
call += new myDelegate(anotherMethod);
call(1, 2.0);
}
static void aMethod(int k, double x)
{
System.Console.WriteLine("aMethod " + k + " " + x);
}
static void anotherMethod(int k, double x)
{
System.Console.WriteLine("anotherMethod " + k + " " + x);
}
}
Delegates
// Fire an event
Summary