Start learning free

LESSON 323 OF 343 · DESIGN PATTERNS

Observer Pattern in Rust

Practice Behavioral

Some objects need to know when another object changes. A price screen must show each new price of a stock. Asking the stock again and again wastes work.

The Observer pattern turns this around. The stock keeps a list of its observers, and it tells each one when its price changes. A new kind of observer can join without any change to the stock.

Here you write a stock and two kinds of observers.

The trait Observer has one method, update(price). The stock calls it to tell an observer the new price. A trait lists the methods a type promises to have.

Write three structs:

  • PriceDisplay::new() implements Observer. It remembers the last price it was told. Its shown() returns that price, or -1 if it was never told one.
  • PriceAlert::new(threshold) implements Observer too. It counts the prices it was told that are at least threshold. Its count() returns that number.
  • Stock::new() keeps its observers, in the order they subscribed.
    • subscribe(observer) adds one. If that observer is already there, nothing changes.
    • unsubscribe(observer) removes one. If that observer isn't there, nothing changes.
    • set_price(price) calls update(price) on each of its observers.

Two observers are the same only when they are the very same object. Two displays that show the same price are still two observers. In Rust, Rc::ptr_eq tells whether two Rcs share one observer.

Rust has no classes. Each observer and the stock is a struct, and new makes one. The tests keep each observer, and the stock keeps it too. So they share it as an Rc<RefCell<…>>, as the graph lessons share their nodes. The stock holds Rc<RefCell<dyn Observer>> values.

The tests make the observers themselves and keep them. They subscribe and unsubscribe them on the stock. Later they call shown() and count() on their own objects. Only set_price tells the observers a price.

Each example runs commands on a new stock, one per line:

  • display name makes a display called name.
  • alert name threshold makes an alert called name with that threshold.
  • sub name subscribes that observer to the stock.
  • unsub name unsubscribes it.
  • price p calls set_price(p).
  • show name writes shown() for a display, or count() for an alert.

Only show writes a line.

 

Example 1:

Input:
1. display screen
2. alert high 100
3. show screen
4. sub screen
5. sub high
6. price 90
7. show screen
8. show high
9. price 100
10. show high
11. price 120
12. show screen
13. show high
Output:
-1
90
0
1
120
2
Explanation:
The display was never told a price, so command 3 writes -1.
Both observers are told 90. That is below 100, so the alert counts nothing.
The price 100 is at least 100, so the alert counts it.
The price 120 counts too. The display shows the last price, 120.

Example 2:

Input:
1. alert big 50
2. display board
3. display spare
4. sub big
5. sub big
6. sub board
7. price 60
8. show big
9. unsub board
10. unsub spare
11. price 70
12. show board
13. show big
14. sub board
15. price 40
16. show board
17. show big
Output:
1
60
2
40
2
Explanation:
The second subscribe of big changes nothing.
So big is told 60 once, and it counts 1.
Command 9 unsubscribes board. It misses 70 and still shows 60.
Command 10 unsubscribes spare, which was never subscribed. Nothing changes.
Then board subscribes again and is told 40.
That is below 50, so big still counts 2.

 

Constraints:

  • A name is 1 to 10 lowercase letters.
  • No name is made twice in one example.
  • Each sub, unsub and show uses a name made before it.
  • A stock has at most 20 observers.
  • p is a whole number from 1 to 1,000,000.
  • threshold is a whole number from 1 to 1,000,000.
  • There are at most 100,000 commands in all.

Note:

Each call should run in O(1) time. A stock has at most 20 observers, so set_price() and unsubscribe() are O(1) too.

STEP 1 OF 20
    Ctrl + Enter to check

    Observer Pattern in Rust

    Keep going with Rust1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free