Download presentation
Presentation is loading. Please wait.
Published byJaylen Passe Modified over 9 years ago
1
Delegates and Method Pointers Cameron Watkins
2
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
3
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
4
Functional Programming
5
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
6
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
7
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 = <fun # squareofact 5;; - : int = 14400 http://caml.inria.fr/about/taste.en.html
8
Imperitive Languages took notice
9
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" }
10
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" }
11
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
12
Java? Nope, sorry...
13
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.
14
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
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.