33,311
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <vector>
#include <cstring>
using namespace std;
class ServiceBase{
private:
char *servicetype;
public:
ServiceBase(const char *s){
strcpy(servicetype,s);
}
char *Getservicetype(){ return servicetype;}
virtual void open(const char *url) = 0;
virtual void query(const char *cmd) = 0;
virtual void close(const char *url) = 0;
void order(const char *url, const char *cmd){
open(url); query(cmd); close(url);
}
};
class FileSystem:public ServiceBase{
public:
FileSystem():ServiceBase("fs"){ cout << "constructor" << endl;}
void open(const char *url){
cout << "open a filesystem..." << endl;
}
void query(const char *cmd){
cout << "in" << cmd << endl;
cout << "in filesystem querying data..." << endl;
}
void close(const char *url){
cout << "close the fiilsystem..." << endl;
}
};
class DataBase:public ServiceBase{
public:
DataBase():ServiceBase("db"){}
void open(const char *url){
cout << "open a database..." << endl;
}
void query(const char *cmd){
cout << "in" << cmd << endl;
cout << "in database querying data..." << endl;
}
void close(const char *url){
cout << "close the database..." << endl;
}
};
class Controller{
private:
vector<ServiceBase*> pool;
public:
ServiceBase *creat(const char *s){
ServiceBase *ser = NULL;
if(strcmp(s,"db") == 0){
cout << "creat db service" << endl;
ser = new DataBase;
pool.push_back(ser);
}
else if(strcmp(s,"fs") == 0){
cout << "creat fs service" << endl;
FileSystem a;
ser = &a;
pool.push_back(ser);
}
cout <<2 ;
return ser;
}
ServiceBase *service(const char *s){
vector<ServiceBase*>::iterator it;
for(it = pool.begin(); it != pool.end(); it ++){
ServiceBase *ser = *it;
if(strcmp(ser->Getservicetype(),s) == 0){
return ser;
}
}
ServiceBase *ss = creat(s); cout <<1;
return ss;
}
};
struct request{
const char *service;
const char *url;
const char *cmd;
};
int main()
{
Controller con;
request req[4] ={ {"fs","d:\\test","salary"},
{"db","10.64.6.4","select * from user"},
{"db","10.64.6.10","selec * from book"},
{"fs","c:\\stu","student"}};
for(int i = 0; i < 4; i ++){
cout << "--------------begin----------------" << endl;
ServiceBase *ser = con.service(req[i].service);
cout << 1 << endl;
ser->order(req[i].url,req[i].cmd);
cout << "---------------end-----------------" << endl;
}
return 0;
}