trainings/CreatingReliableSoftwareCpp/Presentation/exercises/observer/README.md

143 lines
4.2 KiB
Markdown

## Linux compilation
> mkdir build
> cd build
> cmake -DCMAKE_BUILD_TYPE=Debug ..
> make
## Exercise 1
* Go into directory *Core* and implement *Time* and *TimeObserver* class
* Class *Time* should have 4 methods:
* void attach(TimeObserver* observer); -> attach observer
* void detach(TimeObserver* observer); -> detach observer
* Time& operator++(); -> increment day and notify observers
* size_t day() const; -> return current day
* Go into directory *Ship* and make *Ship* class inherit from *TimeObserver*
* Each time the observer will be triggered, you should give the crew food and drink
* Each crew member should consume **1** rum and **1** banana (I know they usually eat biscuits)
* If you don't have enough cargo crew should rebel
* Each day of a rebel you should subtract **5** members of the crew
* If the number of crew drops below **0**, throw an exception "Game Over"
Example (We have 40 members of crew):
Day: 0
crew: 40
|----------------|----------------|
| NAME | AMOUNT |
|----------------|----------------|
|Rum | 300 |
|Banana | 200 |
|Banana | 150 |
|----------------|----------------|
DAY: 1
crew: 40
|----------------|----------------|
| NAME | AMOUNT |
|----------------|----------------|
|Rum | 260 |
|Banana | 160 |
|Banana | 150 |
|----------------|----------------|
DAY: 2
crew: 40
|----------------|----------------|
| NAME | AMOUNT |
|----------------|----------------|
|Rum | 220 |
|Banana | 120 |
|Banana | 150 |
|----------------|----------------|
DAY: 3
crew: 40
|----------------|----------------|
| NAME | AMOUNT |
|----------------|----------------|
|Rum | 180 |
|Banana | 80 |
|Banana | 150 |
|----------------|----------------|
DAY: 4
crew: 40
|----------------|----------------|
| NAME | AMOUNT |
|----------------|----------------|
|Rum | 140 |
|Banana | 40 |
|Banana | 150 |
|----------------|----------------|
DAY: 5
crew: 40
|----------------|----------------|
| NAME | AMOUNT |
|----------------|----------------|
|Rum | 100 |
|Banana | 150 |
|----------------|----------------|
DAY: 6
crew: 40
|----------------|----------------|
| NAME | AMOUNT |
|----------------|----------------|
|Rum | 60 |
|Banana | 110 |
|----------------|----------------|
DAY: 7
crew: 40
|----------------|----------------|
| NAME | AMOUNT |
|----------------|----------------|
|Rum | 20 |
|Banana | 70 |
|----------------|----------------|
********** Crew rebel **********
DAY: 8
crew: 35
|----------------|----------------|
| NAME | AMOUNT |
|----------------|----------------|
|Banana | 30 |
|----------------|----------------|
********** Crew rebel **********
DAY: 9
crew: 30
|----------------|----------------|
| NAME | AMOUNT |
|----------------|----------------|
|----------------|----------------|
********** Crew rebel **********
DAY: 10
crew: 25
|----------------|----------------|
| NAME | AMOUNT |
|----------------|----------------|
|----------------|----------------|
## Exercise 2
* Go into directory *Store* and make *Store* class inherit from *TimeObserver*
* Class *Store* should don't know anything about class *Time*
* We should attach and detach observer outside of class. Think about how you can make it exception-safe (RAII)
* Each time the observer will be triggered, you should change the number of available cargo in the store
* Each time draw a number between -50 and 50 and add it to the amount of cargo
* Print everyday cargo from the Shop and verify if the amount changes
## Exercise 3
* Rewrite Observer to use modern approach -> by value semantic.
* Rewrite class Ship
* Rewrite class Store
* Question: Which class was easier to rewrite?