番茄钟的设计与实现。design of tomato-clock
番茄钟的设计与实现。design of tomato-clock
domain entities
countdown-period
- duration
- default duration
- name
counter
- isRunning
- shouldLoop
counter has one or more countdown-periods
usecase
resume and pause
- this event is captured
- no business rules
isRunning = !isRunning- no output
set duration of a countdown-period
- this event is captured
- business rules
1 <= duration <= 60
- depends on
isRunning- if isRunning == true, do nothing
- set the specified countdown-period’s duration.
- no output
countdown
- execute every 1 second
- depends on isRunning
- if isRunning == true, increase internal counter by one
- otherwise, do nothing
- no output
reset
- this event is captured
- no business rules
- restore initial state
- isRunning = false
- for every countdown-period, duration = default duration
components/classes
countdown-period repository
It is a sequence of countdown-period.
This class provides a factory methodfromArray(periods: CountDownPeriod[]).
It provides whichDuration(time: number): CountDownPeriod which throws NoDurations if sequence is empty. If time is bigger than the sum of durations of all periods, it overflows. It throws InvalidTimeValue if time is negative.
countdown-period
This is a simple value entity representing basic information of periods. Instances are mutated by setDuration(duration: number) and reset. Constructor are fairly simple: ctor(name: string, defaultDuration: number). duration is exposed as public attribute.
clock
public state
readonly time: integerthe number of seconds elapsed with isRunning being true since this instance is created.
methods
reset(): callstop(), then set time to 0.start(): set isRunning to true,id = setTimeout(()=>{...}, 1000).stop(): set isRunning to false,clearTimeout(id).
controller
this entity handles aforementioned events.
本文由作者按照 CC BY 4.0 进行授权