-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ts][step_shotgun_surgery-01_base] Add premature abstractions
- Loading branch information
1 parent
38215b3
commit 8488517
Showing
12 changed files
with
115 additions
and
23 deletions.
There are no files selected for viewing
6 changes: 2 additions & 4 deletions
6
examples/typescript/ts-step_shotgun_surgery-01_base/src/Application/GetStepDuration.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
examples/typescript/ts-step_shotgun_surgery-01_base/src/Domain/DurationMultiplier.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import Step from "./Step"; | ||
import * as StepEnums from "./StepEnums"; | ||
|
||
export function multiplierFor(step: Step): number { | ||
if (step.type() === StepEnums.STEP_TYPE_VIDEO) { | ||
return StepEnums.STEP_DURATION_MULTIPLIER_VIDEO; | ||
} | ||
|
||
if (step.type() === StepEnums.STEP_TYPE_QUIZ) { | ||
return StepEnums.STEP_DURATION_MULTIPLIER_QUIZ; | ||
} | ||
|
||
return 1.0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 1 addition & 3 deletions
4
examples/typescript/ts-step_shotgun_surgery-01_base/src/Domain/Step.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
examples/typescript/ts-step_shotgun_surgery-01_base/src/Domain/StepDurationCalculator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import Step from "./Step"; | ||
|
||
interface StepDurationCalculator { | ||
supports(step: Step): boolean; | ||
calculate(step: Step): number; | ||
} | ||
|
||
export default StepDurationCalculator |
28 changes: 28 additions & 0 deletions
28
...ples/typescript/ts-step_shotgun_surgery-01_base/src/Domain/StepDurationCalculatorChain.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import StepDurationCalculator from "./StepDurationCalculator"; | ||
import Step from "./Step"; | ||
import {STEP_TYPES} from "./StepEnums"; | ||
|
||
class StepDurationCalculatorChain implements StepDurationCalculator { | ||
constructor(private calculators: Array<StepDurationCalculator>) { | ||
} | ||
|
||
supports(step: Step): boolean { | ||
return STEP_TYPES.some(step.type); | ||
} | ||
|
||
calculate(step: Step): number { | ||
if (!this.supports(step)) { | ||
throw new Error(`Missing calculator for step type ${step.type()}`); | ||
} | ||
|
||
for (let calculator of this.calculators) { | ||
if (calculator.supports(step)) { | ||
return calculator.calculate(step); | ||
} | ||
} | ||
|
||
throw new Error(`Missing calculator for step type ${step.type()}`); | ||
} | ||
} | ||
|
||
export default StepDurationCalculatorChain |
17 changes: 17 additions & 0 deletions
17
...es/typescript/ts-step_shotgun_surgery-01_base/src/Domain/StepDurationCalculatorFactory.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import StepDurationCalculator from "./StepDurationCalculator"; | ||
import StepDurationCalculatorVideo from "./StepDurationCalculatorVideo"; | ||
import StepDurationCalculatorQuiz from "./StepDurationCalculatorQuiz"; | ||
import StepDurationCalculatorChain from "./StepDurationCalculatorChain"; | ||
|
||
class StepDurationCalculatorFactory { | ||
static build(): StepDurationCalculator | ||
{ | ||
// Remember to add the calculator!! | ||
return new StepDurationCalculatorChain([ | ||
new StepDurationCalculatorVideo(), | ||
new StepDurationCalculatorQuiz(), | ||
]); | ||
} | ||
} | ||
|
||
export default StepDurationCalculatorFactory |
17 changes: 17 additions & 0 deletions
17
examples/typescript/ts-step_shotgun_surgery-01_base/src/Domain/StepDurationCalculatorQuiz.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import StepDurationCalculator from "./StepDurationCalculator"; | ||
import QuizStep from "./QuizStep"; | ||
import Step from "./Step"; | ||
import {multiplierFor} from "./DurationMultiplier"; | ||
import {QUIZ_QUESTION_DURATION} from "./StepEnums"; | ||
|
||
class StepDurationCalculatorQuiz implements StepDurationCalculator { | ||
supports(step: Step): boolean { | ||
return step instanceof QuizStep; | ||
} | ||
|
||
calculate(step: QuizStep): number { | ||
return step.totalQuestions * QUIZ_QUESTION_DURATION * multiplierFor(step); | ||
} | ||
} | ||
|
||
export default StepDurationCalculatorQuiz |
16 changes: 16 additions & 0 deletions
16
...ples/typescript/ts-step_shotgun_surgery-01_base/src/Domain/StepDurationCalculatorVideo.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import StepDurationCalculator from "./StepDurationCalculator"; | ||
import VideoStep from "./VideoStep"; | ||
import Step from "./Step"; | ||
import {multiplierFor} from "./DurationMultiplier"; | ||
|
||
class StepDurationCalculatorVideo implements StepDurationCalculator { | ||
supports(step: Step): boolean { | ||
return step instanceof VideoStep; | ||
} | ||
|
||
calculate(step: VideoStep): number { | ||
return step.videoDuration * multiplierFor(step); | ||
} | ||
} | ||
|
||
export default StepDurationCalculatorVideo |
12 changes: 6 additions & 6 deletions
12
examples/typescript/ts-step_shotgun_surgery-01_base/src/Domain/StepEnums.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
export const STEP_TYPE_VIDEO = 'video'; | ||
export const STEP_TYPE_QUIZ = 'quiz'; | ||
export const STEP_TYPE_VIDEO: string = 'video'; | ||
export const STEP_TYPE_QUIZ: string = 'quiz'; | ||
|
||
export const STEP_DURATION_MULTIPLIER_VIDEO = 1.1; | ||
export const STEP_DURATION_MULTIPLIER_QUIZ = 1.5; | ||
export const STEP_DURATION_MULTIPLIER_VIDEO: number = 1.1; | ||
export const STEP_DURATION_MULTIPLIER_QUIZ: number = 1.5; | ||
|
||
export const QUIZ_QUESTION_DURATION = 5; | ||
export const QUIZ_QUESTION_DURATION: number = 5; | ||
|
||
// Important: don't forget to add here the type!! | ||
export const STEP_TYPES = [ | ||
export const STEP_TYPES: Array<string> = [ | ||
STEP_TYPE_VIDEO, | ||
STEP_TYPE_QUIZ | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters