Code covered by the BSD License  

Highlights from
MagicListener

5.0

5.0 | 1 rating Rate this file 4 Downloads (last 30 days) File Size: 2.71 KB File ID: #34606

MagicListener

by Benoit Charles

 

16 Jan 2012

MagicListener object

| Watch this File

File Information
Description

MagicListener constructor creates a listener just like the addlistener function. The difference is the constructor accepts one more input, one object or a cell of objects, and the life cycle of the returned object will be linked also on these objects.

Exemple:

Without MagicListener:

[Class1.m]
classdef Class1 < handle
events
myEvent
end
end

[Class2.m]
classdef Class2 < handle
methods
function this = Class2( obj1 )
addlistener( obj1, 'myEvent', @(H,E) this.callback() );
end
function callback( this )
disp( 'CALLBACK!' );
end
end
end

>> obj1 = Class1();
>> obj2 = Class2( obj1 );
>> notify( obj1, 'myEvent' );
CALLBACK!
>> delete( obj2 );
>> notify( obj1, 'myEvent' );
Warning: Error occurred while executing callback:
Invalid or deleted object.

With MagicListener:

[Class1.m]
classdef Class1 < handle
events
myEvent
end
end

[Class2.m]
classdef Class2 < handle
methods
function this = Class2( obj1 )
MagicListener( obj1, 'myEvent', @(H,E) this.callback(), this );
end
function callback( this )
disp( 'CALLBACK!' );
end
end
end

>> obj1 = Class1();
>> obj2 = Class2( obj1 );
>> notify( obj1, 'myEvent' );
CALLBACK!
>> delete( obj2 );
>> notify( obj1, 'myEvent' );

MATLAB release MATLAB 7.7 (R2008b)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (1)
18 Jan 2012 Julien

Works like a charm ! Simple, useful and efficient, thank you...

Please login to add a comment or rating.
Tag Activity for this File
Tag Applied By Date/Time
oop Benoit Charles 16 Jan 2012 08:38:18
listener Benoit Charles 16 Jan 2012 08:38:18

Contact us at files@mathworks.com