设计模式(8)-适配器模式

来源:互联网 发布:淘宝店铺隐藏优惠券 编辑:程序博客网 时间:2024/06/16 00:38

适配器模式

  适配器模式属于结构型的设计模式,其特点是将一个类的接口转换成客户希望的另外一个接口,这样使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。适配器模式包括类适配器模式以及对象适配器模式。

代码

  此处以大话设计模式中的中锋球员为例子说明适配器模式,正常来说,在NBA比赛中,教练说Attack和Defence,但由于国籍的不同可能有些国际球员难以明白这个意思,比如刚到NBA的姚明,此时需要一个翻译,从而让他们知道这是进攻和防守的意思。

C++代码

  文件构成:
  —include
  ——Center.h
  ——CenterTranslator.h
  ——ChineseCenter.h
  —src
  ——Center.cpp
  ——CenterTranslator.cpp
  ——ChineseCenter.cpp
  ——main.cpp
  代码如下:
  —include/Center.h

#ifndef _CENTER_H_#define _CENTER_H_#include<string>class Center{public:    Center(const std::string& str);    virtual ~Center();    virtual void Attack();    virtual void Defence();private:    std::string name;};#endif

  —include/CenterTranslator.h

#ifndef _CENTERTRANSLATOR_H_#define _CENTERTRANSLATOR_H_#include"ChineseCenter.h"#include"Center.h"#include<string>class CenterTranslator:public Center{public:    CenterTranslator(const std::string& str);    virtual void Attack();    virtual void Defence();private:    ChineseCenter chiCenter;};#endif

  —include/ChineseCenter.h

#ifndef _CHINESECENTER_H_#define _CHINESECENTER_H_#include<string>class ChineseCenter{public:    ChineseCenter(const std::string& str);    void ChineseAttack();    void ChineseDefence();private:    std::string name;};#endif

  —src/Center.cpp

#include"Center.h"#include<string>#include<iostream>using namespace std;Center::Center(const string& str):name(str){    ;}Center::~Center(){    ;}void Center::Attack(){    cout<<"Center Attack!"<<endl;}void Center::Defence(){    cout<<"Center Defence!"<<endl;}

  —src/CenterTranslator.cpp

#include"CenterTranslator.h"#include<string>using namespace std;CenterTranslator::CenterTranslator(const string& str):Center(str),chiCenter(str){    ;}void CenterTranslator::Attack(){    chiCenter.ChineseAttack();}void CenterTranslator::Defence(){    chiCenter.ChineseDefence();}

  —src/ChineseCenter.cpp

#include"ChineseCenter.h"#include<iostream>#include<string>using namespace std;ChineseCenter::ChineseCenter(const string& str):name(str){    ;}void ChineseCenter::ChineseAttack(){    cout<<"中锋进攻"<<endl;}void ChineseCenter::ChineseDefence(){    cout<<"中锋防守"<<endl;}

  –src/main.cpp

#include<iostream>#include<memory>#include"CenterTranslator.h"#include"Center.h"using namespace std;int main(){    shared_ptr<Center> center1 = make_shared<Center>("ShaQ");    shared_ptr<Center> center2 = make_shared<CenterTranslator>("姚明");    center1->Attack();    center1->Defence();    center2->Attack();    center2->Defence();    return 0;}

Python代码

  文件结构:
  —Center.py
  —ChineseCenter.py
  —main.py
  代码如下:
  —Center.py

# -*- coding:utf-8 -*-from ChineseCenter import *class Center:    def __init__(self, str):        self.__name = str    def Attack(self):        print "Center Attack!"    def Defence(self):        print "Center Defence!"class CenterTranslator(Center):    def __init__(self, str):        Center.__init__(self, str)        self.chiCenter = ChineseCenter(str)    def Attack(self):        self.chiCenter.ChineseAttack()    def Defence(self):        self.chiCenter.ChineseDefence()

  —ChineseCenter.py

# -*- coding:utf-8 -*-class ChineseCenter:    def __init__(self, str):        self.name = str    def ChineseAttack(self):        print "中锋进攻!"    def ChineseDefence(self):        print "中锋防守!"

  —main.py

# -*- coding:utf-8 -*-from Center import *if "__main__" == __name__:    center1 = Center("ShaQ")    center2 = CenterTranslator("姚明")    center1.Attack()    center1.Defence()    center2.Attack()    center2.Defence()
0 0