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

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

編輯:關於C語言

C++/CLI

/**//*
(C) OOMusou 2007 http://oomusou.cnblogs.com

Filename  : DP_AdapterPattern_ObjectAdapter_Classic.cpp
Compiler  : Visual C++ 8.0 / C++/CLI
Description : Demo how to implement Object Adapter Classic.
Release   : 07/15/2007 1.0
*/
#include "stdafx.h"

using namespace System;

interface class ITarget {
 void request();
};

ref class Adaptee {
public:
 void specificRequest();
};

void Adaptee::specificRequest() {
  Console::WriteLine("Hello Adaptee!!");
}

ref class Adapter : public ITarget {
protected:
 Adaptee^ _adaptee;

public:
 Adapter() : _adaptee(nullptr) {}
 Adapter(Adaptee^ adaptee) : _adaptee(adaptee) {}

public:
 virtual void request();
};

void Adapter::request() {
 if (_adaptee != nullptr)
  _adaptee->specificRequest();
}

int main() {
 Adapter^ adapter = gcnew Adapter(gcnew Adaptee);
 adapter->request();
}

VB

'
'(C) OOMusou 2007 http://oomusou.cnblogs.com

'Filename  : DP_AdapterPattern_ObjectAdapter_Classic.vb
'Compiler  : VB 9
'Description : Demo how to implement Object Adapter Classic.
'Release   : 07/15/2007 1.0
'
Imports System

Interface ITargetInterface ITarget
 Sub request()Sub request()
End Interface

Class AdapteeClass Adaptee
 Public Sub specificRequest()Sub specificRequest()
  Console.WriteLine("Hello Adaptee!!")
 End Sub
End Class

Class AdapterClass Adapter
 Implements ITarget

 Protected _adaptee As Adaptee

 Public Sub New()Sub New(Optional ByRef adaptee As Adaptee = Nothing)
  _adaptee = adaptee
 End Sub

 Public Sub request()Sub request() Implements ITarget.request
  If _adaptee IsNot Nothing Then
   _adaptee.specificRequest()
  End If

 End Sub
End Class

Class ClientClass ClIEnt
 Shared Sub Main()Sub Main()
  Dim adapter As ITarget = New Adapter(New Adaptee())
  adapter.request()
 End Sub
End Class

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