1.NETDelegates & eventsNOEA / PQC 2009 13. Delegates & events Observer pattern Delegates –Semantics –Cil – kode –Anvendelse Events.

Slides:



Advertisements
Similar presentations
CS18000: Problem Solving and Object-Oriented Programming.
Advertisements

Copyright © 2012 Pearson Education, Inc. Chapter 9 Delegates and Events.
Delegates and lambda functions Jim Warren, COMPSCI 280 S Enterprise Software Development.
CS  C++ Function Pointers  C# Method References  Groundwork for Lambda Functions.
March Ron McFadyen1 Design Patterns In software engineering, a design pattern is a generally repeatable solution to a commonly-occurring problem.
Reza Gorgan Mohammadi AmirKabir University of Technology, Department of Computer Engineering & Information Technology Advanced design.
IEG 3080 Tutorial 5 Prepared by Wilson. Outline Lecture review Use-case driven approach Callback function by delegation Course project Smart container.
Objects and Classes First Programming Concepts. 14/10/2004Lecture 1a: Introduction 2 Fundamental Concepts object class method parameter data type.
Object-Oriented Programming with Java Lecture 2 The Java Event Model.
Delegates and Events Tom Roeder CS fa. Motivation – Function Pointers Treat functions as first-class objects eg. Scheme: (map myfunc somelist)
C# Event Processing Model Solving The Mystery. Agenda Introduction C# Event Processing Macro View Required Components Role of Each Component How To Create.
Object Oriented Software Development
Classes and Class Members Chapter 3. 3 Public Interface Contract between class and its clients to fulfill certain responsibilities The client is an object.
Observer Design Pattern Source: Design Patterns – Elements of Reusable Object- Oriented Software; Gamma, et. al.
Delegates and lambda functions Jim Warren, COMPSCI 280 S Enterprise Software Development.
Carnegie Mellon University MSCF1 C#/.NET Basics 2 Some code is from “C# in a Nutshell”
Delegates Programming in C# Delegates CSE 494R (proposed course for 459 Programming in C#) Prof. Roger Crawfis.
OOP and Dynamic Method Binding Chapter 9. Object Oriented Programming Skipping most of this chapter Focus on 9.4, Dynamic method binding – Polymorphism.
CS 350 – Software Design The Observer Pattern – Chapter 18 Let’s expand the case study to include new features: Sending a welcome letter to new customers.
1 Chapter Eleven Handling Events. 2 Objectives Learn about delegates How to create composed delegates How to handle events How to use the built-in EventHandler.
Java Dynamic Proxy Bibliography: reflection/proxy.html
Introduction to C#. Why C#? Develop on the Following Platforms ASP.NET Native Windows Windows 8 / 8.1 Windows Phone WPF Android (Xamarin) iOS (Xamarin)
July 28, 2015IAT 2651 Design Patterns. “Gang of Four” July 28, 2015IAT 2652.
Observer design pattern A closer look at INotifyPropertyChanged, INotifyPropertyChanging and ObservableCollection Observer design pattern1.
Object Oriented Programming.  Interface  Event Handling.
Module 8: Delegates and Events. Overview Delegates Multicast Delegates Events When to Use Delegates, Events, and Interfaces.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 27 JavaBeans and.
Classes Modeling the Object. Objects model the world Classes are programmer defined types that model the parts of a system Class serve as blueprints for.
Design Patterns Software Engineering CS 561. Last Time Introduced design patterns Abstraction-Occurrence General Hierarchy Player-Role.
Design Patterns SE464 Derek Rayside images from NetObjectives.com & Wikipedia.
FEN 2014UCN Teknologi/act2learn1 Higher order functions Observer Pattern Delegates Events Visitor Pattern Lambdas and closures Lambdas in libraries.
From C++ to C# Part 5. Enums Similar to C++ Similar to C++ Read up section 1.10 of Spec. Read up section 1.10 of Spec.
1 Design Patterns Delegates Visitor Pattern. 2 Observer Pattern Observer Pattern is an object-oriented design that simulates void-pointers in for instance.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 15 Event-Driven Programming and.
Special Features of C# : Delegates, Events and Attributes.
CMSC 345 Fall 2000 OO Design. Characteristics of OOD Objects are abstractions of real-world or system entities and manage themselves Objects are independent.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 32 JavaBeans and Bean.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 15 Event-Driven Programming and.
Java - hello world example public class HelloWorld { public static void main (String args[]) { System.out.println("Hello World"); }
Lesson 28: More on the GUI button, frame and actions.
1.NETDelegates & eventsNOEA / PQC 2007 Delegates & events Observer pattern Delegates –Semantics –Cil – kode –Anvendelse Events.
1.NETDelegates & eventsNOEA / PQC 2007 Delegates & events Observer pattern Delegates –Semantics –Cil – code –Usage Events Asynchronious delegates.
Arrays & Enum & Events. Arrays Arrays are data structures consisting of related data items of the same type. Arrays are fixed-length entities—they remain.
C# Fundamentals An Introduction. Before we begin How to get started writing C# – Quick tour of the dev. Environment – The current C# version is 5.0 –
CSC 243 – Java Programming, Spring, 2014 Week 4, Interfaces, Derived Classes, and Abstract Classes.
CIS NET Applications1 Chapter 6 – Events. CIS NET Applications2 Objectives Delegate-based Events.NET event support Practical guides for managing.
Java and C# - Some Commonalities Compile into machine-independent, language- independent code which runs in a managed execution environment Garbage Collection.
Chapter 32 JavaBeans and Bean Events
C# for C++ Programmers 1.
Advanced .NET Programming I 3nd Lecture
Delegates and Events Svetlin Nakov Telerik Corporation
Chapter 36 JavaBeans and Bean Events
2.7 Inheritance Types of inheritance
Delegates and Events 14: Delegates and Events
Singleton Pattern Command Pattern
Chapter Eleven Handling Events.
CS360 Windows Programming
C# Event Processing Model
Corresponds with Chapter 7
6 Delegate and Lambda Expressions
Abstract Class As per dictionary, abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of ,
Delegates & Events 1.
Lesson 7. Events, Delegates, Generics.
Java Lesson 36 Mr. Kalmes.
Programming in C# CHAPTER - 8
Advanced .NET Programming I 3rd Lecture
Creating and Using Classes
Events, Delegates, and Lambdas
Threads and concurrency / Safety
Presentation transcript:

1.NETDelegates & eventsNOEA / PQC Delegates & events Observer pattern Delegates –Semantics –Cil – kode –Anvendelse Events

2.NETDelegates & eventsNOEA / PQC 2009 Observer pattern Also called Observable in some litterature Might also be a interface

3.NETDelegates & eventsNOEA / 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)

4.NETDelegates & eventsNOEA / 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

5.NETDelegates & eventsNOEA / 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

6.NETDelegates & eventsNOEA / PQC 2009

7 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 parameter The method will be encapsulated in the delegate 4.Call the method by the delegate

8.NETDelegates & eventsNOEA / 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);

9.NETDelegates & eventsNOEA / 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); }

10.NETDelegates & eventsNOEA / 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 container Notifier 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);

11.NETDelegates & eventsNOEA / PQC 2009 Trin 4: Call the methods via the delegate: notify ("Pingo");

12.NETDelegates & eventsNOEA / PQC 2009 Demo Incl. ildasm

13.NETDelegates & eventsNOEA / 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.

14.NETDelegates & eventsNOEA / PQC 2009 Gui demo To see the use of event handlers

15.NETDelegates & eventsNOEA / 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.

16.NETDelegates & eventsNOEA / PQC 2009 Step 1: 1.Declare a delegate with an appropriate signature delegate void GreetingHandler(string sender);

17.NETDelegates & eventsNOEA / 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;

18.NETDelegates & eventsNOEA / 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); }

19.NETDelegates & eventsNOEA / 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)

20.NETDelegates & eventsNOEA / 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"); }

21.NETDelegates & eventsNOEA / PQC 2009 What is the difference between delegate and event? From online.net/CSharp_FAQ:_What_is_the_difference_between_delegate_and _event 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..