www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.announce - std.database a design suggestion

reply bls <bizprac orange.fr> writes:
Hi, what do you people think about using the GoF
Factory (design) pattern ?
F.I.

abstract class Database {

	//common database stuff
	public abstract void connect(string user, string pw);
	// execSql(); prepare() etc...
}

abstract class DatabaseFactory {

    public abstract Database GetDatabase();
}

class PostgreSQL:Database {

	// common
    public override void connect(string user, string pw) {

    }
    //PostgreSQL specific
    public void funkyPGstuff() {}
}

class PostreSQLFactory:DatabaseFactory {

    public override Database GetDatabase() {

       return new PostgreSQL();

    }
}

class MySQL:Database {

	// common
    public override void connect(string user, string pw) {

    }
    //MySQL specific
    public void funkyMySQLstuff() {}
}

class MySQLFactory:DatabaseFactory {

    public override Database GetDatabase() {

       return new MySQL();

    }
}
Oct 10 2011
parent Michal Minich <michal.minich gmail.com> writes:
On Mon, 10 Oct 2011 12:02:13 +0200, bls wrote:

 Hi, what do you people think about using the GoF Factory (design)
 pattern ?
 F.I.
for one, in this case, better to use interface only, and connect method can return instance of Database class, saving one factory and one method. interface IDatabase { SqlCommand createCommand (); } class MySql : IDatabase { static MySql connect (..specific args,probably connection string); } But yes, using factory classes and methods is good approach to this design problem.
Oct 10 2011