Pragmatic Objects
  • Pragmatic Objects
  • What's an Object ?
  • Thinking Object
  • Practices
    • The Pragmatic Practices
    • No Getters & Setters
    • Inherit Wisely
    • Wrap Null
    • Wrap Primitives
    • Wrap Collections
    • Expose Snapshots
    • Abandon Composed Names
    • Don't Check Types
    • Minimize Knowledge
    • Immutability
    • Separate Commands & Queries
    • Abandon Statics
    • Invert Dependencies
  • Patterns
    • Domain Model
    • Always Valid
    • Wrapper
    • Command
    • Procedural Object
    • Compute Object
    • Snapshot
    • Value Object
    • Observability
  • Examples
    • Celsiuses / Fahrenheits
Powered by GitBook
On this page
  1. Patterns

Observability

Allow objects to work collaboratively

PreviousValue ObjectNextCelsiuses / Fahrenheits

Last updated 1 year ago

As said previously, observability is a feature, a capability of an object.

Because objects want to work in a choreographed manner more than in an orchestrated manner, they don't want anyone above telling them what to do.

In order to work collaboratively, they need to exchange messages with one another, in real time.

Think microservices : it's about APIs communicating together using asynchronous messages.

That's the pinacle of object-orientation. When in doubt, look at microservices : they're doing objects right !

There's many ways to implement observability, but the most flexible solution is to use an event dispatcher and to implement a .

There's a rough sketch.

class NavigationBar {
    constructor(eventDispatcher) {
        eventDispatcher.on("newMessages", this.incrementUnreadMessages.bind(this));
        eventDispatcher.on("messagesViewed", this.decrementUnreadMessages.bind(this));
    }
    
    incrementUnreadMessages() {
        // code
    }
    
    decrementUnreadMessages() {
        // code
    }
}

class ChatBox {
    receiveMessages(messages: Message[]) {
        this.eventDispatcher.emit("newMessages", messages);
    }
    
    focus() {
        this.eventDispatcher.emit("messagesViewed", ...);
    }
}

Imagine a website like LinkedIn or Facebook where you can open multiple message boxes and maintain a count of unread messages on the navigation bar.

This structure is collaborative : each chatbox can emit events if they know there might be some interested parties.

Alternatively, NavigationBar can listen to these events.

And either know about each other.

And not a single master class to coordinate them.

Observability allows them to work in tandem and to evolve independently as their respective needs grow.

publish/subscribe mechanism