Order of instantiation & Events
Show older comments
Say I have a class A which has an Event "event1" which class B needs to listen for. I want class B to also be a property of class A, ie. I can instantiate obj = A(B). Ideally, in this process B would be set to listen for "event1" from A, but since Matlab builds B first, it does not know anything about A, and thus it has to be done effectively during the instantiation of A. The more complicated version is A(B(C)), where once again C needs to listen for "event1" in A, but is a property of B, which is a property of A.
In a nutshell I have an object A which is supplying the base data to a process which can involve anywhere from one to multiple filters, which is acted on and ultimately passed back to A... so the initial data needs to go to the bottom filter... the filters are defined by classes, in order to be portable and used in different situations..
The question, is there an efficient way to either define the classes or to instantiate the classes that would allow me to have the inner most nested class listen to the Event "event1" in the outer class without having to build a bunch of conditionals in the outer class instantiation function.
9 Comments
An object having to listen to an event triggered from the object that contains it sounds like slightly suspicious design though. If A owns B anyway why doesn't it just directly trigger what would otherwise be triggered by the event? Then B doesn't need to have knowledge of A. I don't mean necessarily right in the function that would trigger the event as that would also be bad design, but you can inject a callback (or as many callbacks as you want) into that function that will get called when you would otherwise fire off the event. That callback could be either a direct function on B or a function on A that calls the function on B.
Guillaume
on 4 Jan 2017
I'm with Adam, something does not sound right here. I don't see why B needs to listen to A, when A could just inform B straight away.
Maybe you should explain your use case in more details. What is the relevant interface of B and A? Are all the filters derived from an abstract class?
Jeff Waldron
on 4 Jan 2017
It would probably help if you could give a simple code example of the structure. I would say that the object that supplies data should not be owning objects that want to act upon that data in specific ways, as a general rule.
I have many areas in my code where I have classes that need to act on some changeable data and within these classes I inject a 'data provider' object which is responsible for supplying the data to the e.g. calculator class.
In terms of how the messaging is designed I have done this in numerous different ways, depending on where I want to drive the actions from and what it is that triggers the data to change. If the 'data provider' is responsible for choosing what data to provide and when then it can send out notifications when this data changes and the calculator/filter classes into which it has been injected can then respond to this in whatever way they choose and update their calculations, possibly then sending out a notification themselves for any further part of the program which wishes to respond to this - e.g. your multiple layers of transforms.
In other places my data provider plays no part in the communications and some other object is responsible for letting components know that something has changed so that those components (calculators/filters/transforms) must re-query the data provider object for the latest data to use.
The basic idea though is of reversing the chain of messaging from what you seem to have. An object should never really be wanting to know about its parent. Instead that would-be 'parent' object should be injected into the other object for it to use and then that object is listening to one of the properties it owns (or has a reference to) rather than needing to know what owns it and listen to that.
Jeff Waldron
on 5 Jan 2017
Jeff Waldron
on 6 Jan 2017
Edited: Jeff Waldron
on 6 Jan 2017
Jeff Waldron
on 6 Jan 2017
Edited: Jeff Waldron
on 6 Jan 2017
Adam
on 6 Jan 2017
I'm not sure I get what is happening with the recursive children here. Each one has the same copy of updated data and each one will multiply it by 0.5.
But what is the expected result of all this if, for example, there are 3 nested children. As it stands you will just get the result of the top-most child, but the result of all child objects will be the same anyway as they are all acting on the same data.
I assumed the intention was for them to act upon each other's results back up the chain, but this doesn't seem to be the setup you have because the 'data' field is just the same data being passed to each child.
If the child objects are to act upon the result of the next one down the chain then the 'data' of children higher up the chain needs to be the result of obj.child.output, which will then get multiplied by 0.5.
If this is the case then the final result you want will be in the child immediately below the parent object and would be the result of effectively, e.g 0.5 * 0.5 * 0.5 * data rather than just 0.5 * data.
Jeff Waldron
on 6 Jan 2017
Accepted Answer
More Answers (0)
Categories
Find more on Create System Objects in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!