Primus2\Falcraft: New Design Pattern (publisher/observer)

IMPORTANT!

This code is obsolete and has been moved to a new project, asherwunk/phabstractic.  For more current information please see this blog post.

I’m proud to announce the implementation of Publisher/Observer design pattern interface(s) in primus/falcraft

The Publisher/Observer (Events) Design Pattern

To quote Wikipedia:

The observer pattern is a software design pattern in which an object, called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods. It is mainly used to implement distributed event handling systems. The Observer pattern is also a key part in the familiar model–view–controller (MVC) architectural pattern.[1] The observer pattern is implemented in numerous programming libraries and systems, including almost all GUI toolkits.

There are some additional places that explain in detail the Publisher/Observer design pattern, such as OODesign.com, or SourceMaking.com.

One interesting note about these interfaces is that both the publisher and the observer include add and detach functions for the other.  Usually, observers only have a notify function.  Using both of these sets of functions is optional: the only set that must be implemented is on the publisher (to call the observer’s notify).

Concrete Examples

You can find a concrete implementation of an Observer object (github) whose notify function simply calls a method on the object. This object is meant to be extended thus.

The meat of the object is the notify function:

This function gets called when a publisher announces something.

So when does something get announced? You can find a concrete example of a publisher (github). In this object we are changing states and sending out notifications to observers. To attach an observer, we use the attach method on the publisher (not the other way around) and when the state changes all the observers are notified:

This uses the mapInternal static function found in RestrictedSet (github). It is like a map function but handles set information better.

Conclusion

It’s anticipated that there will be many different de-coupled connections between various systems in the central processing framework of an artificial intelligence server.  This is the first step towards implementing a universal event system, identifying which objects are standard publishers and observers.  One object stores a state, such as a concept, or recognized pattern, or piece of sensory data, while the listeners act on its changes.  This can lead to complex multi-state neural networks or knowledge representation frames.

photo credit: Statesman handset via photopin (license)

Liked it? Take a second to support kadar on Patreon!

kadar

I'm just a wunk, trying to enjoy life. I am a cofounder of http//originalpursuitssoc.com/ and I like computers, code, creativity, and friends.

You may also like...

Leave a Reply

%d bloggers like this: