Skip to content

Instantly share code, notes, and snippets.

@vrogueon
Created February 19, 2022 00:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vrogueon/a69e86b1ec196652df7efc8d6dc1fd75 to your computer and use it in GitHub Desktop.
Save vrogueon/a69e86b1ec196652df7efc8d6dc1fd75 to your computer and use it in GitHub Desktop.
Prototype Pattern
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