设计模式C++学习笔记之八(Adapter适配器模式)
适配器模式,使用之处比较特殊,不属于常规设计模式,主要用于不同系统之间的处理。是将一个类的接口转换成客户希望的另外一个接口。Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。也是一个比较简单的模式,直接上代码了。
看代码:
8.1.解释
main(),主程序
IUserInfo,本系统内接口
CUserInfo,本系统内实现类
IOuterUser,外系统接口
COuterUser,外系统实现类
COuterUserInfo,本系统内适配类
说明:COuterUserInfo实现IUserInfo接口,将外部系统实现类COuterUser转换成本系统内的接口IUserInfo。使用外部数据跟使用本系统内部数据一样。
注意:COuterUserInfo继承了IUserInfo,如果同时继承了COuterUser则是类适配器。如果COuterUserInfo只是使用了COuterUser则是对象适配器。
//IUserInfo.h//系统内部的实体接口
#pragma once
#include <iostream>
using std::string;
class IUserInfo
{
public:
IUserInfo(void)
{
}
virtual ~IUserInfo(void)
{
}
virtual string GetUserName() = 0;
virtual string GetHomeAddress() = 0;
virtual string GetMobileNumber() = 0;
virtual string GetOfficeTelNumber() = 0;
virtual string GetJobPosition() = 0;
virtual string GetHomeTelNumber() = 0;
};
//UserInfo.h//系统内部实体类
#pragma once
#include "iuserinfo.h"
#include <iostream>
using std::string;
class CUserInfo :
public IUserInfo
{
public:
CUserInfo(void);
~CUserInfo(void);
string GetUserName();
string GetHomeAddress();
string GetMobileNumber();
string GetOfficeTelNumber();
string GetJobPosition();
string GetHomeTelNumber();
};
//UserInfo.cpp
#include "StdAfx.h"
#include "UserInfo.h"
#include <iostream>
using std::cout;
using std::endl;
using std::string;
CUserInfo::CUserInfo(void)
{
}
CUserInfo::~CUserInfo(void)
{
}
string CUserInfo::GetUserName()
{
cout << "姓名叫做..." << endl;
return "0";
}
string CUserInfo::GetHomeAddress()
{
cout << "这里是员工的家庭地址..." << endl;
return "0";
}
string CUserInfo::GetMobileNumber()
{
cout << "这个人的手机号码是0000..." << endl;
return "0";
}
string CUserInfo::GetOfficeTelNumber()
{
cout << "办公室电话是..." << endl;
return "0";
}
string CUserInfo::GetJobPosition()
{
cout << "这个人的职位是BOSS..." << endl;
return "0";
}
string CUserInfo::GetHomeTelNumber()
{
cout << "员工的家庭电话是..." << endl;
return "0";
}
//IOuterUser.h//外部系统实体接口
#pragma once
#include "OuterUserBaseInfo.h"
#include "OuterUserHomeInfo.h"
#include "OuterUserOfficeInfo.h"
class IOuterUser
{
public:
IOuterUser(void)
{
}
~IOuterUser(void)
{
}
COuterUserBaseInfo * GetUserBaseInfo();
COuterUserHomeInfo * GetUserHomeInfo();
COuterUserOfficeInfo * GetUserOfficeInfo();
};
//OuterUser.h//外部系统实体类
#pragma once
#include "OuterUserBaseInfo.h"
#include "OuterUserHomeInfo.h"
#include "OuterUserOfficeInfo.h"
class IOuterUser
{
public:
IOuterUser(void)
{
}
~IOuterUser(void)
{
}
COuterUserBaseInfo * GetUserBaseInfo();
COuterUserHomeInfo * GetUserHomeInfo();
COuterUserOfficeInfo * GetUserOfficeInfo();
};
//OuterUser.cpp
#include "StdAfx.h"
#include "OuterUser.h"
#include "OuterUserBaseInfo.h"
#include "OuterUserHomeInfo.h"
#include "OuterUserOfficeInfo.h"
COuterUser::COuterUser(void)
{
}
COuterUser::~COuterUser(void)
{
}
COuterUserBaseInfo * COuterUser::GetUserBaseInfo()
{
return new COuterUserBaseInfo();
}
COuterUserHomeInfo * COuterUser::GetUserHomeInfo()
{
return new COuterUserHomeInfo();
}
COuterUserOfficeInfo * COuterUser::GetUserOfficeInfo()
{
return new COuterUserOfficeInfo();
}
//OuterUserBaseInfo.h
#pragma once
#include <iostream>
using std::cout;
using std::endl;
using std::string;
class COuterUserBaseInfo
{
public:
COuterUserBaseInfo(void)
{
}
~COuterUserBaseInfo(void)
{
}
string GetUserName()
{
cout << "姓名叫做..." << endl;
return "0";
}
string GetMobileNumber()
{
cout << "这个人的手机号码是0001..." << endl;
return "0";
}
};
//OuterUserHomeInfo.h
#pragma once
#include <iostream>
using std::cout;
using std::endl;
using std::string;
class COuterUserHomeInfo
{
public:
COuterUserHomeInfo(void)
{
}
~COuterUserHomeInfo(void)
{
}
string GetHomeAddress()
{
cout << "这里是员工的家庭地址..." << endl;
return "0";
}
string GetHomeTelNumber()
{
cout << "员工的家庭电话是..." << endl;
return "0";
}
};
//OuterUserOfficeInfo.h
#pragma once
#include <iostream>
using std::cout;
using std::endl;
using std::string;
class COuterUserOfficeInfo
{
public:
COuterUserOfficeInfo(void)
{
}
~COuterUserOfficeInfo(void)
{
}
string GetOfficeTelNumber()
{
cout << "办公室电话是..." << endl;
return "0";
}
string GetJobPosition()
{
cout << "这个人的职位是BOSS..." << endl;
return "0";
}
};
//OuterUserInfo.h//由IUserInfo接口派生的实体类,并引入外部系统实体的实例
#pragma once
#include "iuserinfo.h"
#include "OuterUser.h"
#include <iostream>
using std::string;
class COuterUserInfo :
public IUserInfo
{
public:
COuterUserInfo(void);
~COuterUserInfo(void);
string GetUserName();
string GetHomeAddress();
string GetMobileNumber();
string GetOfficeTelNumber();
string GetJobPosition();
string GetHomeTelNumber();
private:
COuterUser *m_pOuterUser;
};
//OuterUserInfo.cpp
#include "StdAfx.h"
#include "OuterUserInfo.h"
#include "OuterUserBaseInfo.h"
#include "OuterUserHomeInfo.h"
#include "OuterUserOfficeInfo.h"
#include <iostream>
using std::cout;
using std::endl;
using std::string;
COuterUserInfo::COuterUserInfo(void)
{
m_pOuterUser = new COuterUser();
}
COuterUserInfo::~COuterUserInfo(void)
{
delete m_pOuterUser;
}
string COuterUserInfo::GetUserName()
{
COuterUserBaseInfo *pBaseInfo = m_pOuterUser->GetUserBaseInfo();
pBaseInfo->GetUserName();
delete pBaseInfo;
pBaseInfo = NULL;
return "0";
}
string COuterUserInfo::GetHomeAddress()
{
COuterUserHomeInfo *pHomeInfo = m_pOuterUser->GetUserHomeInfo();
pHomeInfo->GetHomeAddress();
delete pHomeInfo;
pHomeInfo = NULL;
return "0";
}
string COuterUserInfo::GetMobileNumber()
{
COuterUserBaseInfo *pBaseInfo = m_pOuterUser->GetUserBaseInfo();
pBaseInfo->GetMobileNumber();
delete pBaseInfo;
pBaseInfo = NULL;
return "0";
}
string COuterUserInfo::GetOfficeTelNumber()
{
COuterUserOfficeInfo *pOfficeInfo = m_pOuterUser->GetUserOfficeInfo();
pOfficeInfo->GetOfficeTelNumber();
delete pOfficeInfo;
pOfficeInfo = NULL;
return "0";
}
string COuterUserInfo::GetJobPosition()
{
COuterUserOfficeInfo *pOfficeInfo = m_pOuterUser->GetUserOfficeInfo();
pOfficeInfo->GetJobPosition();
delete pOfficeInfo;
pOfficeInfo = NULL;
return "0";
}
string COuterUserInfo::GetHomeTelNumber()
{
COuterUserHomeInfo *pHomeInfo = m_pOuterUser->GetUserHomeInfo();
pHomeInfo->GetHomeTelNumber();
delete pHomeInfo;
pHomeInfo = NULL;
return "0";
}
//Adapter.cpp//使用方法
#include "stdafx.h"
#include "IOuterUser.h"
#include "IUserInfo.h"
#include "UserInfo.h"
#include "OuterUserInfo.h"
void DoIt()
{
IUserInfo *pYourGirl = new CUserInfo();
for(int i = 0; i < 101; i += 20)
{
pYourGirl->GetMobileNumber();
}
delete pYourGirl;
}
void NowDoIt()
{
IUserInfo *pYourGirl = new COuterUserInfo();
for(int i = 0; i < 101; i += 20)
{
pYourGirl->GetMobileNumber();
}
delete pYourGirl;
}
int _tmain(int argc, _TCHAR* argv[])
{
DoIt();
NowDoIt();
_CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF | _CRTDBG_ALLOC_MEM_DF);
_CrtDumpMemoryLeaks();
return 0;
}
适配器模式属于结构型模式,当出现数据接口不一致的情况下,才会使用到。例如,之前做过的一个系统,所有的组织结构都是由另一个系统来维护,但我们开发的系统需要用到这些组织结构,并且在我们的系统里组织结构是只读的。因为系统设计的原因,所以两个系统的结构设计并不一样,正好应用到了适配器模式。