Is there a design pattern for chained observers?

Posted by sharakan on Programmers See other posts from Programmers or by sharakan
Published on 2012-12-06T15:28:24Z Indexed on 2012/12/06 17:20 UTC
Read the original article Hit count: 328

Several times, I've found myself in a situation where I want to add functionality to an existing Observer-Observable relationship.

For example, let's say I have an Observable class called PriceFeed, instances of which are created by a variety of PriceSources. Observers on this are notified whenever the underlying PriceSource updates the PriceFeed with a new price.

Now I want to add a feature that allows a (temporary) override to be set on the PriceFeed. The PriceSource should still update prices on the PriceFeed, but for as long as the override is set, whenever a consumer asks PriceFeed for it's current value, it should get the override.

The way I did this was to introduce a new OverrideablePriceFeed that is itself both an Observer and an Observable, and that decorates the actual PriceFeed. It's implementation of .getPrice() is straight from Chain of Responsibility, but how about the handling of Observable events? When an override is set or cleared, it should issue it's own event to Observers, as well as forwarding events from the underlying PriceFeed.

I think of this as some kind of a chained observer, and was curious if there's a more definitive description of a similar pattern.

© Programmers or respective owner

Related posts about design-patterns

Related posts about observer-pattern