设计模式指六-Adapter模式

Posted 万载一梦

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了设计模式指六-Adapter模式相关的知识,希望对你有一定的参考价值。

适配器模式,使用之处比较特殊,不属于常规设计模式,主要用于不同系统之间的处理。是将一个类的接口转换成客户希望的另外一个接口。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模式的主要内容,如果未能解决你的问题,请参考以下文章

Builder设计模式,模板设计模式,Adapter设计模式笔记

Android框架设计模式——Adapter Method

设计模式--适配器模式(Adapter)详解

设计模式之适配器模式 Adapter

设计模式之Adapter(适配器)(转)

Java读源代码学设计模式:适配器Adapter