Article Preview
Buy Now
FEATURE
Design Patterns Part 5
Using the Observer Pattern
Issue: 22.4 (July/August 2024)
Author: Marc Zeedar
Author Bio: Marc taught himself programming in high school when he bought his first computer but had no money for software. He's had fun learning ever since.
Article Description: No description available.
Article Length (in bytes): 16,596
Starting Page Number: 26
Article Number: 22404
Resource File(s):
project22404.zip Updated: 2024-07-01 10:15:30
Related Link(s): None
Excerpt of article text...
Today, I'm continuing my series on design patterns in Xojo, and I'm going to write about the Observer pattern. This was part of my talk at the MBS Xojo conference in Andernach, Germany in April.
The Observer is a fun pattern and one I've needed in decades past, so I know I'll be using it often in future projects.
A Watchlist for Variables
Have you ever had a situation where you wanted to know when a particular variable changed? This is a common need where one part of a program—possibly a user interface element—needs to know when a particular property's value changes so it can update its display, enable a control, or perform a relevant action.
You could, of course, check the value periodically with a
timer
, but that's not always the best use of resources, especially if the item doesn't change often. It also will only update as often as thetimer
runs. Wouldn't it be better if the variable itself could notify you when it changes?This is where the Observer pattern comes in. It's basically a notification system for variables. Even better, it's dynamic and can be changed at runtime. It also works with unlimited objects.
...End of Excerpt. Please purchase the magazine to read the full article.