Advertisement
sdfxs

Untitled

Jun 1st, 2021
1,186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Driver {
  2.     private command: Command;
  3.     constructor(command: Command) {
  4.         this.command = command;
  5.     }
  6.  
  7.     execute() {
  8.         this.command.execute();
  9.     }
  10. };
  11.  
  12. class Engine {
  13.     private state: boolean;
  14.     constructor() {
  15.         this.state = false;
  16.     }
  17.  
  18.     on() {
  19.         this.state = true;
  20.     }
  21.  
  22.     off() {
  23.         this.state = false;
  24.     }
  25. };
  26.  
  27. interface Command {
  28.     execute: () => void;
  29. }
  30.  
  31. class OnStartCommand implements Command {
  32.     private engine: Engine;
  33.     constructor(engine: Engine) {
  34.         this.engine = engine;
  35.     }
  36.  
  37.     execute() {
  38.         this.engine.on();
  39.     }
  40. };
  41.  
  42. class onSwitchOffCommand implements Command {
  43.     private engine: Engine;
  44.     constructor(engine: Engine) {
  45.         this.engine = engine;
  46.     }
  47.  
  48.     execute() {
  49.         this.engine.off();
  50.     }
  51. };
  52.  
  53. const engine = new Engine();
  54. console.log(engine)
  55.  
  56. const onStartCommand = new OnStartCommand(engine);
  57. const driver = new Driver(onStartCommand)
  58. driver.execute()
  59.  
  60. console.log(engine)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement