Presentation is loading. Please wait.

Presentation is loading. Please wait.

IEG 3080 Tutorial 5 Prepared by Wilson. Outline Lecture review Use-case driven approach Callback function by delegation Course project Smart container.

Similar presentations


Presentation on theme: "IEG 3080 Tutorial 5 Prepared by Wilson. Outline Lecture review Use-case driven approach Callback function by delegation Course project Smart container."— Presentation transcript:

1 IEG 3080 Tutorial 5 Prepared by Wilson

2 Outline Lecture review Use-case driven approach Callback function by delegation Course project Smart container

3 Lecture review: Use-case driven approach Problem for traditional approach Describing system from developer prospective “System should do this and that …..” Use-case driven approach Describing system from user prospective “User should do this and that…..” Derived use cases will be used in the later phases of System Development Life Cycle

4 Lecture review: Use-case driven approach Use-case ? Describing user-system interaction process to achieve a certain goal Written as step-by-step description (Scenarios) System functionality is fully described by a complete set of use cases. Actors – representing the users involved Use-case model Use-case diagram Use-case description

5 Lecture review: Use-case driven approach Use-case diagram Shows pictorially the “Actor – use-case” relationship Use-case description Describes the actual steps for a use-case (use-case diagram) 1.A Borrower presents a book. 2.The system checks.……. 3.….. ( Corresponding use-case Description )

6 Lecture review: Use-case driven approach Structuring use-cases > / > A sequence common to several use cases > Describing an alternative scenario Check for reservation > Refuse load > Use case: Borrow copy of book ……….. Extensions: 3a: The check failed. Refuses the load Use case: Borrow copy of book ……….. 5. Check for reservation ……….. Use case: Extend load ……….. 8. Check for reservation ………..

7 Callback function by delegation What is callback? The mechanism that allows an object to send message to another object Two ways to do callback By inheritance – override the default handler for an event By delegation

8 Callback function by delegation Recall that : Delegate – encapsulates a reference to a method (function pointer) Event – a container for delegates, can be used to notify all previously registered delegates. Delegates can register to an Event (with matched type as in the event’s declaration) ** Event can be “triggered” to call back all previous registered delegates *** Fun2 Fun3 Event1 Fun1 Event1 Fun2 Fun3 Fun1 **Registration into Event1 ***Triggering Event1 to execute all registered delegates register calls External trigger

9 Callback function by delegation Example registration of delegates to an event fun2Del event1 fun1Del public class A { // declaration of delegate type suit for the // follow three functions public delegate void FunDelegate(); // event that accepts only pass-in of FunDelegate delegates public event FunDelegate event1; public void Fun1() {…..} public void Fun2() {…..} public static void Main() { A a = new A(); FunDelegate fun1Del = new a.FunDelegate(a.Fun1); FunDelegate fun2Del = new a.FunDelegate(a.Fun2); // registration of delegates a.event1 += fun1Del; a.event1 += fun2Del; } register

10 Callback function by delegation Example triggering of an event event1 fun2Del fun1Del calls User input argument “print!” public class A { // declaration of delegate type suit for the // follow three functions public delegate void FunDelegate(); // event that accepts only pass-in of FunDelegate delegates public event FunDelegate event1; public void Fun1() { Console.writeLine(“Hello 1”); } public void Fun2() { Console.writeLine(“Hello 2”); } public static void Main(string[] args) { A a = new A(); // delegate creation and registration into event in previous slide ……………….. // trigger the event if user type the word “print!” if (String.compare(args[0],”print!”) == 0) a.event1(); } Fun2 Fun1 Console output: Hello 1 Hello 2

11 Callback function by delegation Examples of system callback functions System.Windows.Forms.Form.KeyUp (similar to event1 before) An event trigger by a keyboard up event from users System.Windows.Forms.KeyEventHandler (similar to FunDelegate before ) An EventHandler (a delegate) to response the above event Registration (assume a function fooDel has been defined): System.Windows.Forms.Form.KeyUp += new System.Windows.Forms.KeyEventHandler(fooDel);** This event is triggered when user release a key *** KeyUp fooDel register **Registration into KeyUp Windows user releases a key KeyUp triggers the event fooDel calls Actual call ***Triggering KeyUp to execute all registered delegates

12 Course project You should prepare a container to store all objects in the game scene By a Smart Container Smart container is under System.Collections Automatic resizing, no buffer overflow Example ArrayList Hashtable Queue Stack

13 Course project ArrayList has benefits of Direct access to any elements by index Automatically resizing, no overflow Insertion, deletion for elements in any position of an ArrayList It can contains any objects, but it may potentially add a wrong type of objects. Better solution: Type-safe container

14 Course project The easiest way is to inherit from IEnumerable interface Add your own Add() method public MyArrayList : IEnumerable { private ArrayList gameObject; public int Add(ABall a) { return list.Add(a); }

15 Course project Reading data: Direct access by index - Myarray[5] Use foreach foreach (myobject i in Myarray){ // do anything you want } In your game, you may need to prepare an ArrayList to store all the game objects. Plan ahead on the class hierarchy before writing the code. Best to have single container for everythings A single for-loop to redraw / move all the game objects

16 Course project Best thing is one container for everythings Flying Object BalloonStoneBullet From tutorial 2 An ArrayList container for Flying Object

17 Course project Reminder Naming format of submitted project : “[your sid]_project_phase1.zip”


Download ppt "IEG 3080 Tutorial 5 Prepared by Wilson. Outline Lecture review Use-case driven approach Callback function by delegation Course project Smart container."

Similar presentations


Ads by Google