Observer Design Pattern - Array Headache!

Hi

I have a canvas control which holds, as a property, an array of a class. This array of classes forms the backbone of how the canvas is drawn/put together. What I’m trying to do is set up an observer design pattern on the class so the canvas can be notified of any changes to any of the classes properties in the array.

Is there any way I can automatically add in the register code when I append/insert the class into the array.

t.RegisterObserver(CustomCanvas1)

I’m trying to make life as easy for anyone who uses my canvas control, they instantiate the class wherever they like, populate it and simply append/insert it into the canvas array. All the observer design pattern stuff is done behind the scenes so it’s one less thing for the developer to worry about - they don’t have to remember to register the class.

This all works if I create a property of the class on the canvas - works really well. I can set the property to be a computed property and put the register code in the setter. The issue comes when I try and work with an array. Obviously I can’t make the array a computed property!

Any ideas? Is this even possible!

Been banging my head against a wall on this one - any help greatly appreciated. Thanks

btw - I do have an example project I can put on Dropbox if anyone wants to have a play.

Yes, the Dropbox idea sounds good for tomorrow.

Why not?

Hey Tim - hope your doing well.

If I right-click on the array of classes I don’t get the “Convert to Computed Property” option. If I do it manually I get a syntax error. Hey if you know how I can do it then, job done! :slight_smile:

(ignore the item does not exist error - that’s something else I was testing and not related to the syntax error!)

Make the array private and add your own Append, Insert and Remove methods to the Canvas. That way you can remove the class when it gets removed as well.

Fwiw, a computed property only helps you if you are setting the whole array, not manipulating it.

Thanks Greg,

I want the developers to be able to play/manipulate the array directly so not really wanting to make it private. Quite happy for the whole array to get set as an observer in this implementation as all classes in the array register to the same subject.

Ah, but it wouldn’t be the whole array that is the observer, it would be the individual elements.

Yes - that’s fine. If I have 10 elements in the array, I want them all to be observers.

The original description would lead me to think the canvas is an observer of a bunch of things that influence how it draws
It’s the observer of many other items
No ?

In either case I’d just add method to the canvas that let me manipulate it as if it were an array since you can’t have a computed property that is an array.
You can easily make it handle insert remove append etc and then even provide methods that allow you to append an array of observable object to the exist ing list, replace the existing list if you want.
I don’t think you could do redim but thats about it

This post from some time ago from Paul suggests to use interfaces.

http://www.realsoftwareblog.com/2013/03/observer-pattern.html

[quote=105905:@Eduardo Gutierrez de Oliveira]This post from some time ago from Paul suggests to use interfaces.

http://www.realsoftwareblog.com/2013/03/observer-pattern.html[/quote]
Yep - that’s pretty much how I’ve done it.

[quote=105904:@Norman Palardy]The original description would lead me to think the canvas is an observer of a bunch of things that influence how it draws
It’s the observer of many other items
No ?[/quote]
That’s not how I wrote it :wink:

The individual elements in the array need to be observers rather than the array itself. Making the array private and having separate Append/Insert/Remove methods - great idea. Only thing going through my mind is that I would like the developers to be able to play with the array contents directly.

That’s pretty much what i had in mind. A set of methods/computed properties that mimic the array. There’s no reason you can’t allow the users to play with the contents directly, or at least believe they are playing with the contents directly. Use lots of overloaded methods. Or even a class that acts like an array, with the actual array as a property.

Thanks for all your input people it’s very much appreciated.

I’ll have a play in the morning with the mimicked array methods. Cheers…

[quote=105910:@Patrick Delaney]
The individual elements in the array need to be observers rather than the array itself. Making the array private and having separate Append/Insert/Remove methods - great idea. Only thing going through my mind is that I would like the developers to be able to play with the array contents directly.[/quote]
This doesn’t make sense - an array cannot be an observer since it cannot implement an interface.
Only the members in the array can be.
But what is being observed ?
Is the canvas observing the items that are in the array ?
Or is the canvas bing observed by those items ?
Either way the canvas has an array.
And the thing you want is “when the array is assigned do some code” but leave the array publicly exposed ?
Short of having a special method to assign the array I can’t think of how to do that.
But then if the array IS public a person could just assign directly to it and bypass your assigning code that does the extra work.
I think having the array public means “I won’t know if they do this” as there’s no way to hook the assignment with something like a computed property / method pair.

I obviously haven’t explained this very well.

[quote=105923:@Norman Palardy]This doesn’t make sense - an array cannot be an observer since it cannot implement an interface.
Only the members in the array can be.[/quote]
Absolutely!

The Canvas is observing items in the array

This is what I was wondering, is there a way to automatically assign the observer when the class is assigned to the array whilst leaving the array itself public. Seems not.

I’ll play with this idea in the morning, grateful for the thought.

I was asking if someone had a wizard wheeze in lieu of no computed property/method.

Yeah I don’t think there’s any way to leave it public AND have some magic occur when you do add new members or just swap the whole array.

With a public array a person could literally do

      canvas1.ListOfItemsToObserve = Array( new observableItem )

and you have no clue they’ve done that.
Its probably important you do as you’ll want the canvas to register as an observer of those items so when they do change the canvas redraws right away with whatever new data etc.
I just don’t recall what the Observer & Observable example includes if thats what you’ve based your usage one.
Would seem a good thing to have a way to tell an object to start observing a list of items (which is you case) as part of the interface.
Should be an easy one to add.

Your saying, do something within the interface itself to observe a list/array? That would be cool - do you want a Feedback for that? In the meantime I’ll get unto the mimicked methods tomorrow.

Cheers & Night Night from the UK!

No
I’m saying add methods to the Observer and Observable interfaces in your code that permit your implementors of those interfaces to accept an array

ahhh - ok! Thanks Norman, I’ll be having a dig into this later today…