# Delegates and Method Pointers Cameron Watkins. History  Imperitive Programming  This is what everyone should be used to in this course  Java, C++,

## Presentation on theme: "Delegates and Method Pointers Cameron Watkins. History  Imperitive Programming  This is what everyone should be used to in this course  Java, C++,"— Presentation transcript:

Delegates and Method Pointers Cameron Watkins

History  Imperitive Programming  This is what everyone should be used to in this course  Java, C++, C#, etc  Based on Turing Machine http://www.asethome.org/mathfoundations/tmd/tm3.gif

History  Declarative Programming  Examples:  Database Query Languages  Regular Expressions  Logic Programming  Functional Programming  Based on Lambda Calculus  (λx.2 ∗ x + 1)3 = 2 ∗ 3 + 1 (= 7). http://en.wikipedia.org/wiki/Declarative_programming

Functional Programming

Lambda Calculus  (λx.2 ∗ x + 1)3 = 2 ∗ 3 + 1 (= 7).  x 7 → 2 ∗ x + 1 applied to the argument 3  (λx.x)a  Any Guesses?  F x = λy.f(x, y),  F = λx.F x.  Then  (F x )y = F x y = f(x, y). http://ftp.cs.ru.nl/CompMath.Found/lambda.pdf

Ocaml Examples  let square x = x * x;; val square : int -> int =  # square 3;; - : int = 9 #  let rec fact x = if x int =  # fact 5;; - : int = 120 http://caml.inria.fr/about/taste.en.html

Ocaml Functions can be passed as parameters!  # let compose f g = fun x -> f (g x);; val compose : ('a -> 'b) -> ('c -> 'a) -> 'c -> 'b =  # let squareofact = compose square fact;; val square_o_fact : int -> int = { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/13/3899646/slides/slide_7.jpg", "name": "Ocaml Functions can be passed as parameters.", "description": " # let compose f g = fun x -> f (g x);; val compose : ( a -> b) -> ( c -> a) -> c -> b =  # let squareofact = compose square fact;; val square_o_fact : int -> int =

Imperitive Languages took notice

C# Actions  Action is a method pointer for a function with no parameters or return type //Create the Action Action MyMethod; //The Method void DoThing() { Console.WriteLine("Does the thing"); } //Using the Action public static void main() { MyMethod += DoThing; MyMethod(); //Outputs "Does the Thing" }

C# Delegate  Delegates need to be defined with the parameters and return type //Define the delegate type public delegate int ReturnIntTake2Ints(int x, int y); //Create the delegate public ReturnIntTake2Ints myDelegate //The method int AddTwoNumbers(int x, int y) { return x + y; } //Using the delegate public static void main() { myDelegate += AddTwoNumbers; Console.WriteLine("1 + 1 = " + myDelegate(1, 1)); //Outputs "1 + 1 = 2" }

C++ Function Pointers //The Method void my_int_func(int x) { printf("%d\n", x); } int main() { //Declare the function pointer void (*foo)(int); foo = &my_int_func; //Using the function pointer foo( 2 ); (*foo) (2); return 0 } http://www.cprogramming.com/tutorial/function-pointers.html

Java? Nope, sorry...

Uses for design  The main use:  Observer  The Publisher can just have a public delegate  Any Subscriber can just subscribe by adding itself to the delegate  When the Publisher wants to post it just calls the delegate  Does not need any references to any of the objects it could be calling  Also helps with:  High Cohesion  Code makes more sense with less going on  Low Coupling  Classes don't need references to each other to be able to call methods  Indirection, Adapter, Protected Variations  If it's only a few methods delegates could be used.

Ending Notes  Just because you have this tool doesn't mean it should be used everywhere.  Delegates in code can be confusing if it's not clear why they are there.  Anything that matches the parameters and return type can subscribe so finding out what the purpose of the delegate can be confusing without comments and documentation.  Delegates are commonly used in UI frameworks  clicking a button calls a method  every UI render a collection of methods are called  etc.  Examples:  WPF  Silverlight  Metro

Download ppt "Delegates and Method Pointers Cameron Watkins. History  Imperitive Programming  This is what everyone should be used to in this course  Java, C++,"

Similar presentations