Created
September 22, 2011 03:40
-
-
Save shelling/1233968 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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