Created
July 3, 2022 19:12
-
-
Save exvion/14b7e19da22300be2b7ee9fce60e0c0f to your computer and use it in GitHub Desktop.
Factory
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 Base { | |
isConnected = false; | |
constructor(options) { | |
this.options = options; | |
} | |
connect() { | |
isConnected = true; | |
} | |
disconnect() { | |
isConnected = false; | |
} | |
} | |
class A extends Base { | |
constructor() { | |
super(); | |
} | |
} | |
class B extends Base { | |
constructor() { | |
super(); | |
} | |
} | |
class C extends Base { | |
constructor(options) { | |
super() | |
} | |
} | |
class Factory { | |
create(type, options) { | |
const obj = ''; | |
if (type === 'A') { | |
obj = new A(options); | |
} else if (type === 'B') { | |
obj = new B(options); | |
} else if (type === 'C') { | |
obj = new C(options); | |
} | |
} | |
} | |
const obj1 = Factory.create('A', { host: "192.168.1.1" }); | |
obj1.connect(); | |
const obj2 = Factory.create('A', { host: "192.168.1.2" }); | |
obj2.connect(); | |
const obj3 = Factory.create('B', { host: "192.168.1.1" }); | |
const obj4 = Factory.create('B', { host: "192.168.1.3" }); | |
const obj5 = Factory.create('C', { host: "192.168.1.1" }); | |
// сохранить объекты в такой файл, потом прочитать это файл и создать объекты уже из него? | |
[ | |
{type: 'A', options: { host: "192.168.1.1" }, isConnected: true}, | |
{type: 'A', options: { host: "192.168.1.2" }, isConnected: true}, | |
{type: 'B', options: { host: "192.168.1.1" }, isConnected: false}, | |
{type: 'B', options: { host: "192.168.1.3" }, isConnected: false}, | |
{type: 'C', options: { host: "192.168.1.1" }, isConnected: false} | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment