程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> 我的Design Pattern之旅[6]:Adapter Pattern(OO)(4)

我的Design Pattern之旅[6]:Adapter Pattern(OO)(4)

編輯:關於C語言

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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved