Advertisement
Idanref

Facade

Aug 11th, 2023
1,182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class CPU {
  2.   start(): void {
  3.     console.log("CPU started");
  4.   }
  5. }
  6.  
  7. class Memory {
  8.   load(position: number, data: string): void {
  9.     console.log(`Loading data ${data} at position ${position}`);
  10.   }
  11. }
  12.  
  13. class HardDrive {
  14.   read(position: number, size: number): string {
  15.     console.log(`Reading ${size} data from position ${position}`);
  16.     return "data";
  17.   }
  18. }
  19.  
  20. // Facade: This is the class that simplifies the interface to the subsystems.
  21. class Computer {
  22.   private cpu: CPU;
  23.   private memory: Memory;
  24.   private hardDrive: HardDrive;
  25.  
  26.   constructor() {
  27.     this.cpu = new CPU();
  28.     this.memory = new Memory();
  29.     this.hardDrive = new HardDrive();
  30.   }
  31.  
  32.   start(): void {
  33.     this.cpu.start();
  34.     const data = this.hardDrive.read(1000, 200);
  35.     this.memory.load(0, data);
  36.     console.log("Computer started");
  37.   }
  38. }
  39.  
  40. // Client Code: The client interacts with the subsystems through the simplified interface provided by the facade.
  41. const computer = new Computer();
  42. computer.start();
  43. // Output:
  44. // CPU started
  45. // Reading 200 data from position 1000
  46. // Loading data data at position 0
  47. // Computer started
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement