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

Slides:



Advertisements
Similar presentations
Copyright © 2012 Pearson Education, Inc. Chapter 9 Delegates and Events.
Advertisements

Delegates and lambda functions Jim Warren, COMPSCI 280 S Enterprise Software Development.
CS  C++ Function Pointers  C# Method References  Groundwork for Lambda Functions.
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.
Aalborg Media Lab 26-Jun-15 Software Design Lecture 5 “ Writing Classes”
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.
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.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
SIGCSE’11 - Workshop #3Pedagogical Progressions for Teaching OOD Computer Science & Engineering Carl Alphonce A Progression of Events.
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.
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.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
Java Dynamic Proxy Bibliography: reflection/proxy.html
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
July 28, 2015IAT 2651 Design Patterns. “Gang of Four” July 28, 2015IAT 2652.
Anonymous Classes An anonymous class is a local class that does not have a name. An anonymous class allows an object to be created using an expression.
Object Oriented Programming.  Interface  Event Handling.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Event-Driven Programming 1.
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.
(1) Introduction to Java GUIs Philip Johnson Collaborative Software Development Laboratory Information and Computer Sciences University of Hawaii Honolulu.
1.NETDelegates & eventsNOEA / PQC Delegates & events Observer pattern Delegates –Semantics –Cil – kode –Anvendelse Events.
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.
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 – 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.
CSC 243 – Java Programming, Spring, 2014 Week 4, Interfaces, Derived Classes, and Abstract Classes.
Event Driven (Asynchronous) Programming. Event handling in Unity Subclass a class that contains event handling methods, and then override those methods.
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 5 Introduction to Defining Classes Fundamentals of Java.
Chapter 32 JavaBeans and Bean Events
Lecture 15.1 Event Delegation.
Observer Pattern Context:
C# for C++ Programmers 1.
Advanced .NET Programming I 3nd Lecture
Delegates and Events Svetlin Nakov Telerik Corporation
Chapter 36 JavaBeans and Bean Events
Objects as a programming concept
2.7 Inheritance Types of inheritance
Delegates and Events 14: Delegates and Events
Chapter Eleven Handling Events.
C# Event Processing Model
Corresponds with Chapter 7
6 Delegate and Lambda Expressions
Introduction to Event Handling
Delegates & Events 1.
Lesson 7. Events, Delegates, Generics.
Creating and Using Classes
Events, Delegates, and Lambdas
Threads and concurrency / Safety
Presentation transcript:

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

2.NETDelegates & eventsNOEA / PQC 2007 An example (from java!) public class FrameMedKnap extends javax.swing.JFrame { private javax.swing.JButton jButton1; public FrameMedKnap() { jButton1 = new javax.swing.JButton(); jButton1.setText("jButton1"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); this.add(jButton1, java.awt.BorderLayout.CENTER); } private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { jButton1.setText("Der er trykket på knap"); }

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

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

5.NETDelegates & eventsNOEA / PQC 2007 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 What is what in the java example?

6.NETDelegates & eventsNOEA / PQC 2007 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

7.NETDelegates & eventsNOEA / PQC 2007

8 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

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

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

11.NETDelegates & eventsNOEA / PQC 2007 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);

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

13.NETDelegates & eventsNOEA / PQC 2007 Demo Incl. ildasm

14.NETDelegates & eventsNOEA / PQC 2007 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.

15.NETDelegates & eventsNOEA / PQC 2007 Gui demo To see the use of event handlers

16.NETDelegates & eventsNOEA / PQC 2007 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.

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

18.NETDelegates & eventsNOEA / PQC 2007 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;

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

20.NETDelegates & eventsNOEA / PQC 2007 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)

21.NETDelegates & eventsNOEA / PQC 2007 Step 5: Call the methods by the event name: // Somewhere in a class.. if(Hello != null) //Be sure there is an eventhandler { Hello("Pingo"); }

22.NETDelegates & eventsNOEA / PQC 2007 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..