Skip to content

Instantly share code, notes, and snippets.

@shelling
Created September 22, 2011 03:40
Show Gist options
  • Select an option

  • Save shelling/1233968 to your computer and use it in GitHub Desktop.

Select an option

Save shelling/1233968 to your computer and use it in GitHub Desktop.
#include <iostream>
using namespace std;
class DBClient { // DBClient for production
public:
virtual bool connect() { // should be virtual, this may be the disadvantage.
return true;
}
};
class FakeDBClient : public DBClient { // DBClient for testing
public:
bool connect() {
return false;
}
};
class Model { // Production code, the base of ORM.
public:
void foo(DBClient *client) {
if ( client->connect() ) {
cout << "client ok" << endl;
} else {
cout << "client fail" << endl;
}
}
};
int main() {
Model *model = new Model();
DBClient *dbc = new DBClient();
model->foo(dbc);
FakeDBClient *fdbc = new FakeDBClient();
model->foo(fdbc);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment