Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import std.stdio;
- int main(string[] args)
- {
- IDatabaseFactory factory;
- if(args[0] == "MySQL")
- factory = new MySQLFactory();
- else
- factory = new PostreSQLFactory();
- updatePricelist(factory);
- return 0;
- }
- public interface IDatabase {
- //common database stuff
- alias open connect;
- public void open(string host, string db, string user, string pw);
- public void open(string host, string db, string user, string pw, const string[string] params);
- //alias close disconnect;
- //public void close();
- // exec(); prepare() etc...
- }
- public interface IDatabaseFactory {
- public IDatabase createDatabaseInstance();
- }
- final class PostgreSQL : IDatabase {
- // common
- public void open(string host,string db,string user,string pw,
- const string[string] params) {
- }
- public void open(string host,string db,string user,string pw) {
- }
- //PostgreSQL specific
- public void funkyPGstuff() {}
- }
- final class PostreSQLFactory : IDatabaseFactory {
- public IDatabase createDatabaseInstance() {
- return new PostgreSQL();
- }
- }
- final class MySQL : IDatabase {
- // common
- public void open(string host,string db,string user,string pw,
- const string[string] params) {
- }
- public void open(string host,string db,string user,string pw) {
- }
- //MySQL specific
- public void funkyMySQLstuff() {}
- }
- final class MySQLFactory : IDatabaseFactory {
- public IDatabase createDatabaseInstance() {
- return new MySQL();
- }
- }
- // Update PriceList without knowing the database
- void updatePricelist(IDatabaseFactory factory) {
- IDatabase db = factory.createDatabaseInstance();
- db.connect("localhost", "test", "bls", "secret");
- //db.exec("UPDATE ...");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement