Hi all, I have a quick question that is perhaps very elementary. What is the best way to design my Morph subclasses so that they "react" to changes on their models? For example, if I have some data related model with text that updates and I want the "view" (the Morph) to update whenever this occurs? In my own quick code I've always just implemented a `step` method that checks for data on the model updates, but is that too brutal and inefficient? Thanks, -- Eric |
Hi Eric,
for general "reacting to changes", you could an observer pattern. I think this answer from StackOverflow explains it quite well: https://stackoverflow.com/a/17083241/13994294 For example in Squeak, you could take a look at the Pluggable morph implementations in the system categories Morphic-Pluggable Widgets as well as ToolBuilder-Morphic. For instance, in PluggableButtonMorph, you can see that the communication between morph and model is realized by defining selectors on the morph that will be performed to get necessary data and that can be signaled using the observer pattern to update the visual representation. See PluggableButtonMorph >> #update:.
Apart from that, you could also take a look at the "Object Events" pattern in Squeak, see Object >> #when:send:to: + overloads and senders. Personally, I prefer the "normal" observer pattern the most time because it feels less like coupling the morph and the model too strong to each other, and it gives you greater flexibility without necessarily defining one selector per event type on the morph class.
(Last but not least, it would be a possibility to store closures that were generated by the morph in the model and to evaluate them on every change. However, this is usually an anti-pattern because it creates a very strong coupling between the morph and the model, so I would not really recommend using this approach ...)
To learn more about the underlying design patterns, you could look up in the encyclopedia of your choice the terms "Observer pattern", "MVC", and "MVVM". I hope that helps, and if you have further questions, please don't hesitate to ask them. :-)
Best, Christoph Von: Squeak-dev <[hidden email]> im Auftrag von Eric Gade <[hidden email]>
Gesendet: Sonntag, 25. Oktober 2020 15:44:12 An: The general-purpose Squeak developers list Betreff: [squeak-dev] Morphs and Data Question Hi all,
I have a quick question that is perhaps very elementary. What is the best way to design my Morph subclasses so that they "react" to changes on their models? For example, if I have some data related model with text that updates and I want the "view" (the
Morph) to update whenever this occurs? In my own quick code I've always just implemented a `step` method that checks for data on the model updates, but is that too brutal and inefficient?
Thanks,
-- Eric
Carpe Squeak!
|
In reply to this post by darth-cheney
> On 2020-10-25, at 7:44 AM, Eric Gade <[hidden email]> wrote: > What is the best way to design my Morph subclasses so that they "react" to changes on their models? For example, if I have some data related model with text that updates and I want the "view" (the Morph) to update whenever this occurs? In my own quick code I've always just implemented a `step` method that checks for data on the model updates, but is that too brutal and inefficient? > A simple step method is certainly possible and certainly effective - but if you set the step time too short it will also eat your cpu alive. The basic strategy is to use some mechanism to signal to your morph that something has changed and thereby set a flag that the morph needs redraw/updating. One of the nice things about morphs (and I've certainly fulminated about a lot of the not-so-nice things over time) is that the system can work better than the old MVC approach to the #changed:/#update: protocol. The old approach often lead to massive storms (or worse, recursions) of a change leading to an update that lead to a number of changes that each lead to ... etc. Morphic quite neatly broke that (when used properly) by allowing for an update to flag that a redraw is needed. Multiple changes simply keep setting that flag and eventually the morphic cycle reaches the "everybody what wants to, update!" phase. I know there are plenty of people that know the details better than me, so I'll let them explain more... tim -- tim Rowledge; [hidden email]; http://www.rowledge.org/tim The downside of being better than everyone else is that people tend to assume you're pretentious |
In reply to this post by darth-cheney
Hi Eric,
I believe using a step method is unnecessarily obscure and inefficient. I nearly always use the old ST80 changed:/update: mechanism for MVC implementations:
Trygve On 2020-10-25 15:44, Eric Gade wrote:
--
The essence
of object orientation is that objects collaborate to achieve a
goal. |
In reply to this post by darth-cheney
On 25/10/20 8:14 pm, Eric Gade wrote:
> For example, if I have some data related model with text that updates > and I want the "view" (the Morph) to update whenever this occurs? In my > own quick code I've always just implemented a `step` method that checks > for data on the model updates, but is that too brutal and inefficient? It depends. Stepping is for periodic sampling. If you have fast changing variables but a view is required, say, only once every 100ms, then stepping is fine. But if the changes are slow and asynchronous (say from radio buttons) then an observer pattern explained by Trygve is a better choice. HTH .. Subbu |
Free forum by Nabble | Edit this page |