ISO C++
/**//*
(C) OOMusou 2007 http://oomusou.cnblogs.com
Filename : DP_AdapterPattern_ObjectAdapter_Classic.cpp
Compiler : Visual C++ 8.0 / BCB 6.0 / gcc 3.4.2 / ISO C++
Description : Demo how to implement Object 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 {
public:
Adapter(Adaptee* adaptee = 0) : _adaptee(adaptee) {}
virtual void request() const;
protected:
Adaptee* _adaptee;
};
void Adapter::request() const {
if (_adaptee)
_adaptee->specificRequest();
}
int main() {
Adapter adapter(&Adaptee());
adapter.request();
}
C#
/**//*
(C) OOMusou 2007 http://oomusou.cnblogs.com
Filename : DP_AdapterPattern_ClassAdapter_Object.cs
Compiler : Visual Studio 2005 / C# 2.0
Description : Demo how to implement Class Adapter Object.
Release : 07/15/2007 1.0
*/
using System;
interface ITarget {
void request();
}
class Adaptee {
public void specificRequest() {
Console.WriteLine("Hello Adaptee!!");
}
}
class Adapter : ITarget {
protected Adaptee _adaptee = null;
public Adapter() {}
public Adapter(Adaptee adaptee) {
_adaptee = adaptee;
}
public void request() {
if (_adaptee != null)
_adaptee.specificRequest();
}
}
class ClIEnt {
static void Main() {
Adapter adapter = new Adapter(new Adaptee());
adapter.request();
}
}