|
| 1 | +interface RobotPlan { |
| 2 | + setRobotHead(head: string): void; |
| 3 | + setRobotTorso(torso: string): void; |
| 4 | + setRobotArms(arms: string): void; |
| 5 | + setRobotLegs(legs: string): void; |
| 6 | +} |
| 7 | + |
| 8 | +class Robot implements RobotPlan { |
| 9 | + private robotHead: string; |
| 10 | + private robotTorso: string; |
| 11 | + private robotArms: string; |
| 12 | + private robotLegs: string; |
| 13 | + |
| 14 | + public getRobotHead(): string { |
| 15 | + return this.robotHead; |
| 16 | + } |
| 17 | + public setRobotHead(head: string): void { |
| 18 | + this.robotHead = head; |
| 19 | + } |
| 20 | + |
| 21 | + public getRobotTorso(): string { |
| 22 | + return this.robotTorso; |
| 23 | + } |
| 24 | + public setRobotTorso(torso: string): void { |
| 25 | + this.robotTorso = torso; |
| 26 | + } |
| 27 | + |
| 28 | + public getRobotArms(): string { |
| 29 | + return this.robotArms; |
| 30 | + } |
| 31 | + public setRobotArms(arms: string): void { |
| 32 | + this.robotArms = arms; |
| 33 | + } |
| 34 | + |
| 35 | + public getRobotLegs(): string { |
| 36 | + return this.robotLegs; |
| 37 | + } |
| 38 | + public setRobotLegs(legs: string): void { |
| 39 | + this.robotLegs = legs; |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +interface RobotBuilder { |
| 44 | + buildRobotHead(): void; |
| 45 | + buildRobotTorso(): void; |
| 46 | + buildRobotArms(): void; |
| 47 | + buildRobotLegs(): void; |
| 48 | + getRobot(): Robot; |
| 49 | +} |
| 50 | + |
| 51 | +class OldRobotBuilder implements RobotBuilder { |
| 52 | + private robot: Robot; |
| 53 | + |
| 54 | + public constructor() { |
| 55 | + this.robot = new Robot(); |
| 56 | + } |
| 57 | + |
| 58 | + public getRobot(): Robot { |
| 59 | + return this.robot; |
| 60 | + } |
| 61 | + |
| 62 | + public buildRobotHead(): void { |
| 63 | + this.robot.setRobotHead("Tin Head"); |
| 64 | + } |
| 65 | + public buildRobotTorso(): void { |
| 66 | + this.robot.setRobotTorso("Tin Torso"); |
| 67 | + } |
| 68 | + public buildRobotArms(): void { |
| 69 | + this.robot.setRobotArms("Tin Arms"); |
| 70 | + } |
| 71 | + public buildRobotLegs(): void { |
| 72 | + this.robot.setRobotLegs("Tin Head"); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +class RobotEngineer { |
| 77 | + private robotBuilder: RobotBuilder; |
| 78 | + |
| 79 | + public constructor(robotBuilder: RobotBuilder) { |
| 80 | + this.robotBuilder = robotBuilder; |
| 81 | + } |
| 82 | + |
| 83 | + public getRobot(): Robot { |
| 84 | + return this.robotBuilder.getRobot(); |
| 85 | + } |
| 86 | + |
| 87 | + public makeRobot(): void { |
| 88 | + this.robotBuilder.buildRobotHead(); |
| 89 | + this.robotBuilder.buildRobotTorso(); |
| 90 | + this.robotBuilder.buildRobotArms(); |
| 91 | + this.robotBuilder.buildRobotLegs(); |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +// ------------------- |
| 96 | +const oldStyleRobot: OldRobotBuilder = new OldRobotBuilder(); |
| 97 | +const robotEngineer: RobotEngineer = new RobotEngineer(oldStyleRobot); |
| 98 | + |
| 99 | +robotEngineer.makeRobot(); |
| 100 | +const firstRobot: Robot = robotEngineer.getRobot(); |
| 101 | + |
| 102 | +console.log("robot head :", firstRobot.getRobotHead()); |
| 103 | +console.log("robot torso:", firstRobot.getRobotTorso()); |
| 104 | +console.log("robot arms :", firstRobot.getRobotArms()); |
| 105 | +console.log("robot legs :", firstRobot.getRobotLegs()); |
0 commit comments