-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfactory.ts
65 lines (55 loc) · 1.66 KB
/
factory.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import {
FastestRouteAlgorithm,
ScenicRouteAlgorithm,
ShortestRouteAlgorithm,
} from "./route";
import type { LatLng, RouteAlgorithm } from "./route";
/**
* Defines the different types of route algorithms that can be created by the `RouteFactory`.
*/
enum RouteType {
Shortest,
Fastest,
Scenic,
}
/**
* Defines the parameters required to create a route algorithm.
*/
type Param = {
type: RouteType;
from: LatLng;
to: LatLng;
};
/**
* Defines the contract for a route factory that can create instances of different
* route algorithms based on the specified route type.
*/
interface RouteFactoryType {
create(params: Param): RouteAlgorithm;
}
/**
* The RouteFactory class is responsible for creating instances of
* different route algorithms based on the specified route type.
*
* @param {RouteType} params.type - The type of route algorithm to create.
* @param {LatLng} params.from - The starting location for the route.
* @param {LatLng} params.to - The destination location for the route.
* @returns {RouteAlgorithm} - The created route algorithm instance.
* @throws {Error} - Throws an error if the specified route type is invalid.
*/
class RouteFactory implements RouteFactoryType {
create({ type, from, to }: Param): RouteAlgorithm {
switch (type) {
case RouteType.Shortest:
return new ShortestRouteAlgorithm(from, to);
case RouteType.Fastest:
return new FastestRouteAlgorithm(from, to);
case RouteType.Scenic:
return new ScenicRouteAlgorithm(from, to);
default:
throw new Error("Invalid route type");
}
}
}
export { RouteFactory, RouteType };
export type { Param, RouteFactoryType };