Created
February 19, 2022 00:13
-
-
Save vrogueon/a69e86b1ec196652df7efc8d6dc1fd75 to your computer and use it in GitHub Desktop.
Prototype Pattern
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 ConnectionPrototype { | |
constructor(proto){ | |
this.proto = proto; | |
return this.clone(); | |
} | |
clone(){ | |
let connection = new Connection( | |
this.proto.driver, | |
this.proto.server, | |
this.proto.database, | |
this.proto.user, | |
this.proto.password | |
); | |
return connection; | |
} | |
} | |
class Connection{ | |
constructor(driver, server, database, user, password){ | |
this.driver = driver; | |
this.server = server; | |
this.database = database; | |
this.user = user; | |
this.password = password; | |
this.status = 0; | |
} | |
getConnection(){ | |
this.status = 1; | |
return this.driver + "://server=" +this.server+ ";database=" +this.database +";user="+this.user+ ";password="+this.password; | |
} | |
close(){ | |
this.status = 0; | |
} | |
} | |
let protoConnection = new Connection("engine","localhost","beerDB","sa","123456"); | |
console.log({...protoConnection}) | |
let connectionMysqlPrototype = new Connection("mysql","localhost","beerDB","sa","123456"); | |
let connectionSQLServer = new ConnectionPrototype(protoConnection); | |
connectionSQLServer.driver = "SQLServer"; | |
let connectionMysql = new ConnectionPrototype(connectionMysqlPrototype); | |
console.log(connectionSQLServer); | |
console.log(connectionMysql); | |
console.log(connectionSQLServer.getConnection()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment