To better demonstrate the difficulties of reading/updating objects in a multithreaded environment, I'm looking for examples for groups of objects with a bidirectional or unidirectional relationship between them such that if one is updated then the other(s) may update also.
Notes:
updated equals to - the former state of the object is no longer valid so a new object with different value(s) must be created (No setters; Functional style).
I'm not looking for any kind of synchronization solutions.
Examples I created/found so far:
The notation
B -> A
means thatB
state may derive fromA
state and ifA
is updated thenB
may/may not update due toA
update.
unidirectional relationship: A download contains the following characteristics:
is download started: boolean
- Can be set totrue
only once.is currently downloading: boolean
- Will betrue
whenis download started
istrue
. will befalse when
is download completedwill be
false. Also, it may change from
true` to false and vice-versa by the user.download speed: number
- Will change as soon asis currently downloading
istrue
is download completed: boolean
- Can betrue
iffis currently downloading
istrue
.
relationships:
is download completed -> is currently downloading -> is download started
and download speed -> is currently downloading
- bidirectional relationship: A
master
who's responsible for multipleslaves
(jobs). Amaster
and aslave
contains the following characteristics:
master
:
state: In progress/Canceled/Completed
- It starts withIn progress
state and will beCompleted
iff allslaves
haveCompleted
state.slaves: slave[]
slave
:
state: In progress/Canceled/Completed
- It starts withIn progress
state. Will beCanceled
iff themaster
's state isCanceled
and it states isIn progress
.
relationships:
master: state -> slaves: state
, master: slave[] -> slaves: state
and slave: state -> master: state
.
Bonus: Example for a group of objects with a bidirectional relationship which can be changed to a unidirectional relationship after realizing there was a design problem - But isn't easy to see immediately.