나는 다른 공급자의 많은 데이터베이스에 동시에 연결할 수있는 새로운 앱을 시작하고 있습니다.하나님 클래스에 대한 대안
분명히class Bd
{
private:
TUniConnection* mpConnection;
public:
Bd::Bd()
{
mpConnection = new TUniConnection(NULL);
}
void Bd::setProvider(UnicodeString provider)
{
mpConnection->ProviderName = provider;
}
void Bd::connect()
{
mpConnection->Connect();
}
UnicodeString Bd::getProvider() const
{ return mpConnection->ProviderName; }
Bd::~Bd()
{ delete mpConnection; }
};
// In the right path to become a singleton-helper-utility-god-class
class App
{
public:
// Default bd. Just a shortcut.
Bd* bd;
App()
{ bd = getBd("BDMain"); }
~App()
{ delBd("BDMain"); }
Bd* getBd(UnicodeString key)
{
if(mpBdList[key] != NULL)
{
return mpBdList[key];
}
mpBdList[key] = new Bd;
return mpBdList[key];
}
void delBd(UnicodeString key)
{
delete mpBdList[key];
mpBdList[key] = NULL;
}
private:
std::map<UnicodeString, Bd *>mpBdList;
};
// Just an example of use.
int main()
{
// Consider this instance global/singleton/etc
App* app = new App;
app->bd->setProvider("Oracle");
app->connect();
// Outside the main, probably in some form (this method don't exist - yet)
app->bd->query("blah blah blah");
app->getBd("settings")->setProvider("SQLite");
app->getBd("settings")->connect();
app->getBd("settings")->query("blah blah blah");
}
아직 작동하지 않습니다,하지만 너희들은 내 생각의 단서를 가질 수 있습니다 쉽게 모든 연결을 사용하는 패턴으로 생각
, 난이 코드 (C++를)했다. 이론적으로는 완벽 해 보입니다. 쉽고 짧은 코드로 기본 연결 (app-> bd)에 액세스 할 수 있습니다. 다른 연결과 동일합니다. 나에게 완벽 할지라도, 모두는 그것이 반 패턴 및 모두라고 말하고 있습니다.
어떻게이 "도우미"클래스 없이도 거의 동일한 결과를 얻을 수 있으며 매개 변수/생성자에 아무 것도 전달하지 않고도 모든 양식과 클래스에 내 연결/설정을 공유 할 수 있습니다.
감사합니다.
새 전화를 걸지 않아도됩니다. 스택에 객체를 만들면됩니다. 예 : 'App app; 또는'std :: map mpBdList; ' –