cpp的适配器模式
//
// main.cpp
// adpatermodel
//
// Created by bikang on 16/11/16.
// Copyright (c) 2016年 bikang. All rights reserved.
//
#include <iostream>
#include <cstdlib>
using namespace std;
//以前只需要name
class UserInfo{
public:
UserInfo(){}
virtual ~UserInfo(){}
string getUserName(){
return "name : tom";
}
};
class MyObj{
public:
MyObj(){}
virtual ~MyObj(){}
void virtual write(){
UserInfo *ui = new UserInfo();
cout << ui->getUserName();
delete ui;
}
};
//现在需要提供age
class UserInfoAdapter:public UserInfo{
public:
UserInfoAdapter(){}
virtual ~UserInfoAdapter(){}
string getAge(){
return "18";
}
string getUserinfo(){
return this->getUserName()+" age:"+getAge();
}
};
//新的类
class MyObjAdapter:public MyObj{
public:
MyObjAdapter(){}
virtual ~MyObjAdapter(){}
void write(){
UserInfoAdapter *uia = new UserInfoAdapter();
cout << uia->getUserinfo();
}
};
int main(int argc, const char * argv[]) {
// insert code here...
std::cout << "Hello, World!\n";
MyObj *mj = new MyObj;
mj->write();
cout << endl;
MyObj *mj2 = new MyObjAdapter;
mj2->write();
return 0;
}