Created
August 4, 2024 09:19
-
-
Save BiosBoy/445430f8b1096dc49f4f06a5e0204add to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Singleton { | |
| constructor() { | |
| if (Singleton.instance) { | |
| return Singleton.instance; | |
| } | |
| this.data = []; | |
| Singleton.instance = this; | |
| } | |
| getData() { | |
| return this.data; | |
| } | |
| setData(data) { | |
| this.data = data; | |
| } | |
| } | |
| const singleton1 = new Singleton(); | |
| const singleton2 = new Singleton(); | |
| singleton1.setData([1, 2, 3]); | |
| console.log(singleton2.getData()); // Output: [1, 2, 3] | |
| class Subject { | |
| constructor() { | |
| this.observers = []; | |
| } | |
| subscribe(observer) { | |
| this.observers.push(observer); | |
| } | |
| unsubscribe(observer) { | |
| this.observers = this.observers.filter(obs => obs !== observer); | |
| } | |
| notify(data) { | |
| this.observers.forEach(observer => observer.update(data)); | |
| } | |
| } | |
| class Observer { | |
| update(data) { | |
| console.log(`Observer received data: ${data}`); | |
| } | |
| } | |
| const subject = new Subject(); | |
| const observer1 = new Observer(); | |
| const observer2 = new Observer(); | |
| subject.subscribe(observer1); | |
| subject.subscribe(observer2); | |
| subject.notify("Hello, Observers!"); // Both observers will log the message | |
| class Car { | |
| constructor() { | |
| this.type = 'Car'; | |
| } | |
| } | |
| class Truck { | |
| constructor() { | |
| this.type = 'Truck'; | |
| } | |
| } | |
| class VehicleFactory { | |
| createVehicle(vehicleType) { | |
| switch (vehicleType) { | |
| case 'car': | |
| return new Car(); | |
| case 'truck': | |
| return new Truck(); | |
| default: | |
| throw new Error('Unknown vehicle type'); | |
| } | |
| } | |
| } | |
| const factory = new VehicleFactory(); | |
| const myCar = factory.createVehicle('car'); | |
| const myTruck = factory.createVehicle('truck'); | |
| console.log(myCar.type); // Output: Car | |
| console.log(myTruck.type); // Output: Truck | |
| const Module = (function () { | |
| let privateVar = 'I am private'; | |
| function privateMethod() { | |
| console.log(privateVar); | |
| } | |
| return { | |
| publicMethod: function () { | |
| privateMethod(); | |
| } | |
| }; | |
| })(); | |
| Module.publicMethod(); // Output: I am private | |
| class Car { | |
| constructor() { | |
| this.cost = 20000; | |
| } | |
| getCost() { | |
| return this.cost; | |
| } | |
| } | |
| class Sunroof { | |
| constructor(car) { | |
| this.car = car; | |
| } | |
| getCost() { | |
| return this.car.getCost() + 1500; | |
| } | |
| } | |
| class LeatherSeats { | |
| constructor(car) { | |
| this.car = car; | |
| } | |
| getCost() { | |
| return this.car.getCost() + 2000; | |
| } | |
| } | |
| let myCar = new Car(); | |
| myCar = new Sunroof(myCar); | |
| myCar = new LeatherSeats(myCar); | |
| console.log(myCar.getCost()); // Output: 23500 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment