Talk About Network



Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Eiffel > Re: Newbie ques...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 4 of 4 Topic 821 of 913
Post > Topic >>

Re: Newbie question -> Observer pattern

by Peter Horan <peter@[EMAIL PROTECTED] > Sep 21, 2006 at 01:22 AM

iso8859-1@[EMAIL PROTECTED]
 wrote:
> In EiffelVision 2 I use the automatic generated event handling with the
> code supplied by EiffelBuild. But this is my Usercode. I want my class
> to be observable. This way the UI is notified whenever a value changes.
> 
> Example: (from an rpg - gurps)
> 
> Damage depends on ST. Whenever ST changes, Damage needs to be updated.
> If both values are displayed on screen, both Views of them (the st-view
> and the damage view) have to be updated. ST and Damage are of type
> valueholder which is the class I want to be observable.

Provide your class with an action list:

feature
	notify_st_change_actions: NOTIFY_ST_CHANGE_SEQUENCE is
			-- Actions to be performed when a color is changed.
		once
			create Result
		ensure
			not_void: Result /= Void
		end

=====================================
This class is probably adapted from somewhere in EiffelVision or the
events 
classes, but I forget. I chose to use EV_ACTION_SEQUENCE but there are
others to 
use. The root is ACTION_SEQUENCE in the Event cluster:

class
	NOTIFY_ST_CHANGE_SEQUENCE

inherit
	EV_ACTION_SEQUENCE[TUPLE[ARG_1, ARG_2]]
	
create
	default_create, make_filled
	
feature -- Access
	force_extend (action: PROCEDURE [ANY, TUPLE]) is
			-- Extend without type checking.
		do
			extend (~wrapper (?, ?, action))
		end

	wrapper (arg_1: ARG_1; arg_2: ARG_2; action: PROCEDURE [ANY, TUPLE]) is
			-- Use this to circumvent tuple type checking. (at your
                         -- own risk!)
			-- Calls `action' passing all other arguments.
		do
			action.call ([fgc, bgc])
		end

feature {NONE} -- Implementation

	new_filled_list (n: INTEGER): like Current is
			-- New list with `n' elements.
		do
			create Result.make_filled (n)
		end
		
end -- class NOTIFY_ST_CHANGE_SEQUENCE
==============================================================
Your various observers can then add one or more agent routines to the 
notify_st_change_actions list.

Then, when st updates, the list is executed by the call:
     notify_st_change_actions.call([arg_1, arg_2])

==============================================================
The beauty of the approach is that st does not need to know 'who' is
observing, 
just 'what' to do when it changes.

The same mechanism is used in C# .NET, except that the names are wrong.
What in 
Eiffel is called an "action list", is known as an "event" and the agent 
mechanism for wrapping a routine in a type-safe way is a "delegate".

So, we write:
     divide.Click += new System.EventHandler(handle_divide);

to add the routine handle_divide to the "Click event (action list)". Its 
signature is:
     void handle_divide(object sender, EventArgs e)

I have not made my own event action lists in C# though.
-- 
Peter Horan               School of Engineering and Information Technology
peter@[EMAIL PROTECTED]
       Deakin University
+61-3-5227 1234 (Voice)   Geelong, Victoria 3217, AUSTRALIA
+61-3-5227 2028 (FAX)     http://www.eit.deakin.edu.au/~peter

-- The Eiffel guarantee: From specification to implementation
-- (http://www.cetus-links.org/oo_eiffel.html)




 4 Posts in Topic:
Newbie question -> Observer pattern
iso8859-1@[EMAIL PROTECTE  2006-09-19 14:19:00 
Re: Newbie question -> Observer pattern
Peter Horan <peter@[EM  2006-09-20 03:19:01 
Re: Newbie question -> Observer pattern
iso8859-1@[EMAIL PROTECTE  2006-09-19 22:06:02 
Re: Newbie question -> Observer pattern
Peter Horan <peter@[EM  2006-09-21 01:22:12 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat May 17 3:04:29 CDT 2008.