Code covered by the BSD License  

Highlights from
MagicListener

5.0

5.0 | 1 rating Rate this file 2 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' );

Acknowledgements

This file inspired Graphical Wrappers.

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

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

Contact us