File tree Expand file tree Collapse file tree 12 files changed +115
-23
lines changed
examples/typescript/ts-step_shotgun_surgery-01_base Expand file tree Collapse file tree 12 files changed +115
-23
lines changed Original file line number Diff line number Diff line change 1
1
import StepId from '../Domain/StepId'
2
2
import StepRepository from '../Domain/StepRepository'
3
- import VideoStep from "../Domain/VideoStep " ;
3
+ import StepDurationCalculatorFactory from "../Domain/StepDurationCalculatorFactory " ;
4
4
5
5
class GetStepDuration {
6
6
constructor ( private repository : StepRepository ) {
7
7
}
8
8
9
9
execute ( stepId : string ) : number {
10
10
const step = this . repository . find ( new StepId ( stepId ) )
11
- return step instanceof VideoStep ?
12
- step . getVideoDuration ( ) :
13
- step . totalQuestions ;
11
+ return StepDurationCalculatorFactory . build ( ) . calculate ( step )
14
12
}
15
13
}
16
14
Original file line number Diff line number Diff line change
1
+ import Step from "./Step" ;
2
+ import * as StepEnums from "./StepEnums" ;
3
+
4
+ export function multiplierFor ( step : Step ) : number {
5
+ if ( step . type ( ) === StepEnums . STEP_TYPE_VIDEO ) {
6
+ return StepEnums . STEP_DURATION_MULTIPLIER_VIDEO ;
7
+ }
8
+
9
+ if ( step . type ( ) === StepEnums . STEP_TYPE_QUIZ ) {
10
+ return StepEnums . STEP_DURATION_MULTIPLIER_QUIZ ;
11
+ }
12
+
13
+ return 1.0 ;
14
+ }
Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ import {STEP_TYPE_QUIZ} from "./StepEnums";
5
5
class QuizStep extends Step {
6
6
constructor (
7
7
stepId : StepId ,
8
- public readonly totalQuestions : Number
8
+ public readonly totalQuestions : number
9
9
) {
10
10
super ( stepId ) ;
11
11
}
Original file line number Diff line number Diff line change 1
1
import StepId from "./StepId" ;
2
2
3
3
abstract class Step {
4
-
5
- protected constructor ( private readonly _id : StepId ) {
6
- }
4
+ protected constructor ( private readonly _id : StepId ) { }
7
5
8
6
abstract type ( ) ;
9
7
}
Original file line number Diff line number Diff line change
1
+ import Step from "./Step" ;
2
+
3
+ interface StepDurationCalculator {
4
+ supports ( step : Step ) : boolean ;
5
+ calculate ( step : Step ) : number ;
6
+ }
7
+
8
+ export default StepDurationCalculator
Original file line number Diff line number Diff line change
1
+ import StepDurationCalculator from "./StepDurationCalculator" ;
2
+ import Step from "./Step" ;
3
+ import { STEP_TYPES } from "./StepEnums" ;
4
+
5
+ class StepDurationCalculatorChain implements StepDurationCalculator {
6
+ constructor ( private calculators : Array < StepDurationCalculator > ) {
7
+ }
8
+
9
+ supports ( step : Step ) : boolean {
10
+ return STEP_TYPES . some ( step . type ) ;
11
+ }
12
+
13
+ calculate ( step : Step ) : number {
14
+ if ( ! this . supports ( step ) ) {
15
+ throw new Error ( `Missing calculator for step type ${ step . type ( ) } ` ) ;
16
+ }
17
+
18
+ for ( let calculator of this . calculators ) {
19
+ if ( calculator . supports ( step ) ) {
20
+ return calculator . calculate ( step ) ;
21
+ }
22
+ }
23
+
24
+ throw new Error ( `Missing calculator for step type ${ step . type ( ) } ` ) ;
25
+ }
26
+ }
27
+
28
+ export default StepDurationCalculatorChain
Original file line number Diff line number Diff line change
1
+ import StepDurationCalculator from "./StepDurationCalculator" ;
2
+ import StepDurationCalculatorVideo from "./StepDurationCalculatorVideo" ;
3
+ import StepDurationCalculatorQuiz from "./StepDurationCalculatorQuiz" ;
4
+ import StepDurationCalculatorChain from "./StepDurationCalculatorChain" ;
5
+
6
+ class StepDurationCalculatorFactory {
7
+ static build ( ) : StepDurationCalculator
8
+ {
9
+ // Remember to add the calculator!!
10
+ return new StepDurationCalculatorChain ( [
11
+ new StepDurationCalculatorVideo ( ) ,
12
+ new StepDurationCalculatorQuiz ( ) ,
13
+ ] ) ;
14
+ }
15
+ }
16
+
17
+ export default StepDurationCalculatorFactory
Original file line number Diff line number Diff line change
1
+ import StepDurationCalculator from "./StepDurationCalculator" ;
2
+ import QuizStep from "./QuizStep" ;
3
+ import Step from "./Step" ;
4
+ import { multiplierFor } from "./DurationMultiplier" ;
5
+ import { QUIZ_QUESTION_DURATION } from "./StepEnums" ;
6
+
7
+ class StepDurationCalculatorQuiz implements StepDurationCalculator {
8
+ supports ( step : Step ) : boolean {
9
+ return step instanceof QuizStep ;
10
+ }
11
+
12
+ calculate ( step : QuizStep ) : number {
13
+ return step . totalQuestions * QUIZ_QUESTION_DURATION * multiplierFor ( step ) ;
14
+ }
15
+ }
16
+
17
+ export default StepDurationCalculatorQuiz
Original file line number Diff line number Diff line change
1
+ import StepDurationCalculator from "./StepDurationCalculator" ;
2
+ import VideoStep from "./VideoStep" ;
3
+ import Step from "./Step" ;
4
+ import { multiplierFor } from "./DurationMultiplier" ;
5
+
6
+ class StepDurationCalculatorVideo implements StepDurationCalculator {
7
+ supports ( step : Step ) : boolean {
8
+ return step instanceof VideoStep ;
9
+ }
10
+
11
+ calculate ( step : VideoStep ) : number {
12
+ return step . videoDuration * multiplierFor ( step ) ;
13
+ }
14
+ }
15
+
16
+ export default StepDurationCalculatorVideo
Original file line number Diff line number Diff line change 1
- export const STEP_TYPE_VIDEO = 'video' ;
2
- export const STEP_TYPE_QUIZ = 'quiz' ;
1
+ export const STEP_TYPE_VIDEO : string = 'video' ;
2
+ export const STEP_TYPE_QUIZ : string = 'quiz' ;
3
3
4
- export const STEP_DURATION_MULTIPLIER_VIDEO = 1.1 ;
5
- export const STEP_DURATION_MULTIPLIER_QUIZ = 1.5 ;
4
+ export const STEP_DURATION_MULTIPLIER_VIDEO : number = 1.1 ;
5
+ export const STEP_DURATION_MULTIPLIER_QUIZ : number = 1.5 ;
6
6
7
- export const QUIZ_QUESTION_DURATION = 5 ;
7
+ export const QUIZ_QUESTION_DURATION : number = 5 ;
8
8
9
9
// Important: don't forget to add here the type!!
10
- export const STEP_TYPES = [
10
+ export const STEP_TYPES : Array < string > = [
11
11
STEP_TYPE_VIDEO ,
12
12
STEP_TYPE_QUIZ
13
13
] ;
You can’t perform that action at this time.
0 commit comments