Implementation
Class Adapter
ISO C++ (使用多重繼承,對ITarget使用public繼承,對Adaptee使用private繼承,因為Adaptee僅需Adapter內部使用即可。)
/**//*
(C) OOMusou 2007 http://oomusou.cnblogs.com
Filename : DP_AdapterPattern_ClassAdapter_Classic.cpp
Compiler : Visual C++ 8.0 / BCB 6.0 / gcc 3.4.2 / ISO C++
Description : Demo how to implement Class Adapter Classic.
Release : 07/15/2007 1.0
*/
#include <iOStream>
using namespace std;
class ITarget {
public:
virtual void request() const = 0;
};
class Adaptee {
public:
void specificRequest() const;
};
void Adaptee::specificRequest() const {
cout << "Hello Adaptee!!" << endl;
}
class Adapter : public ITarget, private Adaptee {
public:
virtual void request() const;
};
void Adapter::request() const {
specificRequest();
}
int main() {
Adapter adapter;
adapter.request();
}
C#
/**//*
(C) OOMusou 2007 http://oomusou.cnblogs.com
Filename : DP_AdapterPattern_ClassAdapter_Classic.cs
Compiler : Visual Studio 2005 / C# 2.0
Description : Demo how to implement Class Adapter Classic.
Release : 07/15/2007 1.0
*/
using System;
interface ITarget {
void request();
}
class Adaptee {
public void specificRequest() {
Console.WriteLine("Hello Adaptee!!");
}
}
class Adapter : Adaptee, ITarget {
public void request() {
specificRequest();
}
}
class ClIEnt {
static void Main() {
ITarget adapter = new Adapter();
adapter.request();
}
}