1. 程式人生 > >設計模式指六-Adapter模式

設計模式指六-Adapter模式

color for 比較 bsp new res tails 使用 ios

適配器模式,使用之處比較特殊,不屬於常規設計模式,主要用於不同系統之間的處理。是將一個類的接口轉換成客戶希望的另外一個接口。Adapter模式使得原本由於接口不兼容而不能一起工作的那些類可以一起工作。
系統內部的實體接口

//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 << "這個人的手機號碼是..." << 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 << "這個人outer的手機號碼是... " << 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";
    }
};

由IUserInfo接口派生的實體類,並引入外部系統實體的實例,起適配作用

//OuterUserInfo.h// 

#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();
    string name = pBaseInfo->GetUserName();
    delete pBaseInfo;
    pBaseInfo = NULL;
    return name;
}

string COuterUserInfo::GetHomeAddress()
{
    COuterUserHomeInfo *pHomeInfo = m_pOuterUser->GetUserHomeInfo();
    pHomeInfo->GetHomeAddress();
    delete pHomeInfo;
    pHomeInfo = NULL;
    return "0";
}

string COuterUserInfo::GetMobileNumber()
{
    COuterUserBaseInfo *pBaseInfo = m_pOuterUser->GetUserBaseInfo();
    string number = pBaseInfo->GetMobileNumber();
    delete pBaseInfo;
    pBaseInfo = NULL;
    return number;
}

string COuterUserInfo::GetOfficeTelNumber()
{
    COuterUserOfficeInfo *pOfficeInfo = m_pOuterUser->GetUserOfficeInfo();
    string num = pOfficeInfo->GetOfficeTelNumber();
    delete pOfficeInfo;
    pOfficeInfo = NULL;
    return num;
}

string COuterUserInfo::GetJobPosition()
{
    COuterUserOfficeInfo *pOfficeInfo = m_pOuterUser->GetUserOfficeInfo();
    string ret = pOfficeInfo->GetJobPosition();
    delete pOfficeInfo;
    pOfficeInfo = NULL;
    return ret;
}

string COuterUserInfo::GetHomeTelNumber()
{
    COuterUserHomeInfo *pHomeInfo = m_pOuterUser->GetUserHomeInfo();
    string ret = pHomeInfo->GetHomeTelNumber();
    delete pHomeInfo;
    pHomeInfo = NULL;
    return ret;
}

使用測試

//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();

    system(“pause”)

    return 0;
}

適配器模式屬於結構型模式,當出現數據接口不一致的情況下,才會使用到。

轉載自http://blog.csdn.net/phiall/article/details/52199659

設計模式指六-Adapter模式