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 abstract class Observer has one method, update(price). The stock calls it to tell an observer the new price. An abstract class lists methods without a body. Each class built on it must write them.
Write three classes:
PriceDisplay()is built onObserver. It remembers the last price it was told. Itsshown()returns that price, or -1 if it was never told one.PriceAlert(threshold)is built onObservertoo. It counts the prices it was told that are at leastthreshold. Itscount()returns that number.Stock()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.setPrice(price)callsupdate(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.
The tests make each observer with new and pass a pointer. So the stock keeps Observer* pointers.
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 setPrice tells the observers a price.
Each example runs commands on a new stock, one per line:
display namemakes a display calledname.alert name thresholdmakes an alert callednamewith that threshold.sub namesubscribes that observer to the stock.unsub nameunsubscribes it.price pcallssetPrice(p).show namewritesshown()for a display, orcount()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
nameis 1 to 10 lowercase letters. - No
nameis made twice in one example. - Each
sub,unsubandshowuses anamemade before it. - A stock has at most 20 observers.
pis a whole number from 1 to 1,000,000.thresholdis 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 setPrice() and unsubscribe() are O(1) too.
Free plan: 30 tutor messages. PRO is unlimited.
