Graphic designer friend Ryan recently asked me, “what is my favourite function?” That was a tough question to think about. Pondering it for a while, I decided that AddObserver
is my favourite function. AddObserver
is used in various programming contexts including the model-view-controller paradigm and asynchronous computing.
Model-View-Controller
A model-view-controller (MVC) paradigm separates three core pieces of functionality in a data-centric application. The model represents the content that would be stored in a database or other application state. Application state remembers what you were typing, what is on your screen, what tabs you have opened in your web browser. The view is the presentation of your data, which can appear simultaneously in different places, e.g., two windows editing the same Word document. Controller mediates interaction between the view and model, handling push notifications from the model and UI interaction from the view.
In order for a model-view-controller paradigm to work, AddObserver
is used to register listeners from UI input and model changes, to show them in the view.
Asynchronous Computing
Asynchronous computing refers to the concept of placing a request into a computer’s queue and waiting for it to finish. The current thread of sequential execution doesn’t have to wait until the computation is finished. Instructions are given to the computer after the current instruction has finished. Imagine a stack of punchcards being read by a mainframe. It synchronous programming, each instruction card is read one at a time, and each card can take a couple of seconds to finish. In asynchronous programming, an instruction card is read but it isn’t run until the previous card’s computation has finished. (I know, I’m oversimplifying the scenario.)
Asynchronous computing allows a computer to process long-running tasks. In a web browser context, some tasks are sent to a queue for processing. When the asynchronous task has completed, AddObserver
listens to the task’s completion so we can do something else with the computed result.