std.database design suggestion
bls
bizprac at orange.fr
Tue Oct 11 10:01:17 PDT 2011
Am 11.10.2011 13:37, schrieb Kagamin:
> bls Wrote:
>
>> abstract class DatabaseFactory {
>>
>> public abstract Database GetDatabase();
>> }
>
> the method is usually called CreateInstance.
I think this is true for the Factory _method_ pattern. I am using the
_Abstract_ Factory pattern.
See http://en.wikipedia.org/wiki/Abstract_factory_pattern
Though createDatabaseInstance () seems to be reasonable/ (follows common
coding conventions)
-- slightly modified snippet--
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(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(const string[string] params) {
}
//PostgreSQL specific
public void funkyPGstuff() {}
}
final class PostreSQLFactory : IDatabaseFactory {
public IDatabase createDatabaseInstance() {
return new PostgreSQL();
}
}
final class MySQL : IDatabase {
// common
public void open(const string[string] params) {
}
//MySQL specific
public void funkyMySQLstuff() {}
}
final class MySQLFactory : IDatabaseFactory {
public IDatabase createDatabaseInstance() {
return new MySQL();
}
}
// Update PriceList without knowing the db backend
void updatePricelist(IDatabaseFactory factory) {
IDatabase db = factory.createDatabaseInstance();
db.connect([
"host" : "localhost",
"database" : "test",
"user" : "bls",
"password" : "secret"
]);
//db.exec("UPDATE ...");
}
More information about the Digitalmars-d
mailing list