Omri Bashari
Omri Bashari

Reputation: 41

Observer Design Pattern Issues

I am working on a large project in C++ that will have a graphical user interface. The user interface will use some design pattern (MVVM/MVC) that will rely on the observer pattern.

My problem is that I currently have no way of predicting which parts of the Model should be observable. And there are many, many parts.

I find myself being pulled in several directions due to this issue:

  1. If I develop back-end classes that do not support notification I will find myself violating the Open-Closed principle.
  2. If I do provide support for notification to all Model classes and all of their data members it will have a massive performance cost that is unjustified since only a fraction of this support will actually be needed (even though this fraction is unknown).
  3. The same is true if I only provide support for extension by making all non-const methods virtual and accessing these methods through base-pointers. This will also have a cost in readability.

I feel that out of these 3, (1.) is probably the lesser evil.

However, I feel like an ideal solution should actually exists in some language (definitely not C++), but I don't know if it's supported anywhere.

The unicorn solution I was thinking of is something like this: Given a class Data, shouldn't it be possible for clients that seek to make Data observable do something like

@MakeObservable(Data)

as a compile time construct. This in turn would make it possible to call addObserver on Data objects and modify all assignments to data members with notifiers. it would also make you pay in performance only for what you get.

So my question is two-fold:

  1. Am I right to assume that out of the 3 options I stated, (1.) is the lesser but necessary evil?
  2. Does my unicorn solution exist anywhere? being worked on? or would be impossible to implement for some reason?

Upvotes: 2

Views: 779

Answers (2)

Vanilla Face
Vanilla Face

Reputation: 916

Templatious library can help you with completely decoupling GUI and domain logic and making flexible/extendable/easy to maintain message systems and notifiers.

However, there's one downside - you need C++11 support for this.

Check out this article taming qt And the example in github: DecoupledGuiExamples

So, you probably don't need notifiers on every class, you can just shoot messages from the inside functions and on specific classes where you can make any class send any message you want to GUI.

Upvotes: 1

user4842163
user4842163

Reputation:

If I understand correctly, you're concerned with the cost of providing a signal/notification for potentially every observable property of every object.

Fortunately you're in luck, since storing a general thread-safe notifier with every single property of every object would generally be extremely expensive in any language or system.

Instead of getting all clever and trying to solve this problem at compile-time, which I recommend would shut out some very potentially useful options to a large-scale project (ex: plugins and scripting), I'd suggest thinking about how to make this cheaper at runtime. You want your signals to be stored at a coarser level than the individual properties of an object.

If you store just one with the object that passes along the appropriate data about which property was modified during a property change event to filter which clients to notify, then now we're getting a lot cheaper. We're exchanging some additional branching and larger aggregates for the connected slots, but you get a significantly smaller object in exchange with potentially faster read access, and I'd suggest this is a very valuable exchange in practice.

You can still design your public interface and even the event notification mechanism so that clients work with the system in a way that feels like they're connecting to properties rather than the whole object, perhaps even calling methods in a property (if it's an object/proxy) to connect slots if you need or can afford a back pointer to the object from a property.

If you're not sure, I would err on the side of attaching event slots to properties as well as modifying them as part of the object interface rather than property interface, as you'll have a lot more breathing room to optimize in exchange for a slightly different client aesthetic (one that I really don't think is less convenient so much as just 'different', or at least potentially worth the cost of eliminating a back pointer per property).

That's in the realm of convenience and wrapper-type things. But you don't need to violate the open-closed principle to achieve MVP designs in C++. Don't get crammed into a corner by data representation. You have a lot of flexibility at the public interface level.

Memory Compaction -- Paying for What We Use

On discovering that efficiency plays an important role here, I'd suggest some basic ways of thinking to help with that.

First, just because an object has some accessor like something() does not mean that the associated data has to be stored in that object. It doesn't even have to be stored anywhere until that method is called. If memory is your concern, it can be stored at some level outside.

Most software breaks down into hierarchies of aggregates owning resources. For example, in a 3D software, a vertex is owned by a mesh which is owned by the scene graph which is owned by the application root.

If you want designs where you pay almost no memory cost whatsoever for things that are not being used, then you want to associate the data to the object at a coarser level. If you store it directly in the object, then every object pays for what something() returns regardless of whether it is needed. If you store it indirectly in the object with a pointer, then you pay for the pointer to something() but not for the full cost of it unless it is used. If you associate it to the owner of the object, then retrieving it has a lookup cost, but one that is not as expensive as associating it to the owner of the owner of the object.

So there's always ways to get something very close to free for things you don't use if you associate at a coarse enough level. At granular levels you mitigate lookup and indirection overhead, at coarse levels you mitigate costs for things you don't use.

Massive Scale Events

Given massive scalability concerns with millions to billions of elements being processed, and still the desire for potentially some of them to generate events, if you can use an asynchronous design, I'd really recommend it here. You can have a lock-free per-thread event queue to which objects with a single bit flag set generate events. If the bit flag is not set, they don't.

This kind of deferred, async design is useful with such scale since it gives you periodic intervals (or possibly just other threads, though you'd need write locks -- as well as read locks, though writing is what needs to cheap -- in that case) in which to poll and devote full resources to bulk processing the queue while the more time-critical processing can continue without synchronizing with the event/notifier system.

Basic Example

// Interned strings are very useful here for fast lookups
// and reduced redundancy in memory.
// They're basically just indices or pointers to an 
// associative string container (ex: hash or trie).

// Some contextual class for the thread storing things like a handle
// to its event queue, thread-local lock-free memory allocator, 
// possible error codes triggered by functions called in the thread,
// etc. This is optional and can be replaced by thread-local storage 
// or even just globals with an appropriate lock. However, while
// inconvenient, passing this down a thread's callstack is usually 
// the most efficient and reliable, lock-free way.
// There may be times when passing around this contextual parameter
// is too impractical. There TLS helps in those exceptional cases.
class Context;

// Variant is some generic store/get/set anything type. 
// A basic implementation is a void pointer combined with 
// a type code to at least allow runtime checking prior to 
// casting along with deep copying capabilities (functionality
// mapped to the type code). A more sophisticated one is
// abstract and overriden by subtypes like VariantInt
// or VariantT<int>
typedef void EventFunc(Context& ctx, int argc, Variant** argv);

// Your universal object interface. This is purely abstract:
// I recommend a two-tier design here: 
// -- ObjectInterface->Object->YourSubType
// It'll give you room to use a different rep for 
// certain subtypes without affecting ABI.
class ObjectInterface
{
public:
     virtual ~Object() {}

     // Leave it up to the subtype to choose the most
     // efficient rep.
     virtual bool has_events(Context& ctx) const = 0;

     // Connect a slot to the object's signal (or its property 
     // if the event_id matches the property ID, e.g.).
     // Returns a connection handle for th eslot. Note: RAII 
     // is useful here as failing to disconnect can have 
     // grave consequences if the slot is invalidated prior to 
     // the signal.
     virtual int connect(Context& ctx, InternedString event_id, EventFunc func, const Variant& slot_data) = 0;

     // Disconnect the slot from the signal.
     virtual int disconnect(Context& ctx, int slot) = 0;

     // Fetches a property with the specified ID O(n) integral cmps.
     // Recommended: make properties stateless proxies pointing
     // back to the object (more room for backend optimization).
     // Properties can have set<T>/get<T> methods (can build this
     // on top of your Variant if desired, but a bit more overhead
     // if so).
     // If even interned string compares are not fast enough for
     // desired needs, then an alternative, less convenient interface
     // to memoize property indices from an ID might be appropriate in
     // addition to these.
     virtual Property operator[](InternedString prop_id) = 0;

     // Returns the nth property through an index.
     virtual Property operator[](int n) = 0;

     // Returns the number of properties for introspection/reflection.
     virtual int num_properties() const = 0;

     // Set the value of the specified property. This can generate
     // an event with the matching property name to indicate that it
     // changed.
     virtual void set_value(Context& ctx, InternedString prop_id, const Variant& new_value) = 0;

     // Returns the value of the specified property.
     virtual const Variant& value(Context& ctx, InternedString prop_id) = 0;

     // Poor man's RTTI. This can be ignored in favor of dynamic_cast
     // for a COM-like design to retrieve additional interfaces the
     // object supports if RTTI can be allowed for all builds/modes.
     // I use this anyway for higher ABI compatibility with third
     // parties.
     virtual Interface* fetch_interface(Context& ctx, InternedString interface_id) = 0;
};

I'll avoid going into the nitty gritty details of the data representation -- the whole point is that it's flexible. What's important is to buy yourself room to change it as needed. Keeping the object abstract, keeping the property as a stateless proxy (with the exception of the backpointer to the object), etc. gives a lot of breathing room to profile and optimize away.

For async event handling, each thread should have a queue associated which can be passed down the call stack through this Context handle. When events occur, such as a property change, objects can push events to this queue through it if has_events() == true. Likewise, connect doesn't necessarily add any state to the object. It can create an associative structure, again through Context, which maps the object/event_id to the client. disconnect also removes it from that central thread source. Even the act of connecting/disconnecting a slot to/from a signal can be pushed to the event queue for a central, global place to process and make the appropriate associations (again preventing objects without observers from paying any memory cost).

When using this type of design, each thread should have at its entry point an exit handler for the thread which transfers the events pushed to the thread event queue from the thread-local queue to some global queue. This requires a lock but can be done not-too-frequently to avoid heavy contention and to allow each thread to not be slowed down by the event processing during performance-critical areas. Some kind of thread_yield kind of function should likewise be provided with such a design which also transfers from the thread-local queue to the global queue for long-lived threads/tasks.

The global queue is processed in a different thread, triggering the appropriate signals to the connected slots. There it can focus on bulk processing the queue when it isn't empty, sleeping/yielding when it is. The whole point of all of this is to aid performance: pushing to a queue is extremely cheap compared to the potential of sending synchronous events every time an object property is modified, and when working with massive scale inputs, this can be a very costly overhead. So simply pushing to a queue allows that thread to avoid spending its time on the event handling, deferring it to another thread.

Upvotes: 1

Related Questions