Florian wrestles with data structures in his exercise application:
An excercise can contain multiple stages and
each stage can be one of two types.
One type is a count down timer, the other a tap counter,
... snip ... would [you] create a separate entity for each type of stage, or just have one entity for the stages?
It seems you defined a one-to-many relationship. One Exercise
object might contain many Stages
.
From your description, a Stage
object might contain different properties. That is, one type of Stage
might have a Counter
property collecting repetitions. (Perform 15 Push-Ups.) Another Stage
object instead will have a Timed
property containing the value indicating how long a Stage
should be performed. (Hold a plank position for 90 seconds.)
Protocols
I think this is where the power of Protocols
may help. Map your two Stage
objects and determine what is similar to both. This might form the basis of your Stage
protocol.
Your Exercise
object will contain an array of objects that define the particular goals of that exercise. Each object in the array should conform to your Stage
protocol.
This will allow you to build views using ForEach
and list all the Stages
for an exercise regardless of some of the design differences. Additionally, you should be able to add more Stage
types to your appliction without much change, as long as the new objects conform to your Stage
protocol.
Using protocols, you can enforce the similarities between Exercise
goals, and eliminate the need to define properties that would be unused because of its Type
.