写好的代码就是为了以后少写一些代码。
适配器模式就是把原来的类封装成新的接口,而不用进行大的改动。比方说我需要一个实现一个栈功能,其实我没必要开发很多底层的内容,只需要把双向列表进行封装就可以实现了;我需要实现一个队列功能,其实也只需要把双向列表进行封装就可以了。既然只是简单的封装的话,其实是用不到适配器模式的。除了使用双向列表实现队列和栈,数组也可以实现。这样的话,根据栈和队列的接口可以实现各种各样的适配情况。设计模式是为了实现接口不变,实现丰富的功能。所以适配器模式就如下代码所示:
C++示例代码:
#pragma once
template<typename T>
class 栈
{
public:
栈(){}
virtual ~栈(){}
public:
virtual void 压栈(T t) = 0;
virtual T 出栈()=0;
};
#pragma once
#include "栈.h"
#include <list>
using namespace std;
template<typename T>
class 栈list :
public 栈<T>, private list<T>
{
public:
栈list(){}
virtual ~栈list(){}
void 压栈(T t)
{
this->push_front(t);
}
T 出栈()
{
T t = (T)this->front();
this->pop_front();
return t;
}
};
#pragma once
#include "栈.h"
#include "栈list.h"
template<typename T>
class 列表栈 :
public 栈<T>
{
public:
列表栈(){}
virtual ~列表栈(){}
void 压栈(T t)
{
stack.压栈(t);
}
T 出栈()
{
return stack.出栈();
}
private:
栈list<T> stack;
};
#pragma once
template<typename T>
class 队列
{
public:
队列(){}
virtual ~队列(){}
virtual void 入队列(T t) = 0;
virtual T 出队列() = 0;
};
#pragma once
#include "队列.h"
#include <list>
using namespace std;
template<typename T>
class 队列list :
public 队列<T>, private list<T>
{
public:
队列list(){}
virtual ~队列list(){}
void 入队列(T t)
{
this->push_front(t);
}
T 出队列()
{
T t = (T)this->back();
this->pop_back();
return t;
}
};
#pragma once
#include "队列.h"
#include "队列list.h"
template<typename T>
class 列表队列 :
public 队列<T>
{
public:
列表队列(){}
virtual ~列表队列(){}
void 入队列(T t)
{
queue.入队列(t);
}
T 出队列()
{
return queue.出队列();
}
private:
队列list<T> queue;
};
// 适配器模式.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "栈.h"
#include "列表栈.h"
#include "队列.h"
#include "列表队列.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
栈<int>* stack = new 列表栈<int>();
stack->压栈(1);
stack->压栈(100);
int v1 = stack->出栈();
int v2 = stack->出栈();
cout << "v1 = " << v1 << "; v2 = " << v2 << "." << endl;
delete stack;
队列<int>* queue = new 列表队列<int>();
queue->入队列(1);
queue->入队列(100);
v1 = queue->出队列();
v2 = queue->出队列();
cout << "v1 = " << v1 << "; v2 = " << v2 << "." << endl;
delete queue;
getchar();
return 0;
}
C#示例代码:
/*
* 由SharpDevelop创建。
* 用户: hangba
* 日期: 2016/3/11
* 时间: 13:49
*
* 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
*/
using System;
namespace 适配器模式
{
/// <summary>
/// Description of Stack.
/// </summary>
public interface Stack<T>
{
void Push(T t);
T Pop();
}
}
/*
* 由SharpDevelop创建。
* 用户: hangba
* 日期: 2016/3/11
* 时间: 13:54
*
* 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
*/
using System;
using System.Collections.Generic;
namespace 适配器模式
{
/// <summary>
/// Description of LinkedListStack.
/// </summary>
public class LinkedListStack<T> : Stack<T>
{
private Stack<T> stack = new StackLinkedList<T>();
public LinkedListStack()
{
}
public void Push(T t)
{
stack.Push(t);
}
public T Pop()
{
return stack.Pop();
}
}
}
/*
* 由SharpDevelop创建。
* 用户: hangba
* 日期: 2016/3/11
* 时间: 13:54
*
* 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
*/
using System;
using System.Collections.Generic;
namespace 适配器模式
{
/// <summary>
/// Description of LinkedListStack.
/// </summary>
public class LinkedListStack<T> : Stack<T>
{
private Stack<T> stack = new StackLinkedList<T>();
public LinkedListStack()
{
}
public void Push(T t)
{
stack.Push(t);
}
public T Pop()
{
return stack.Pop();
}
}
}
/*
* 由SharpDevelop创建。
* 用户: hangba
* 日期: 2016/3/10
* 时间: 9:20
*
* 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
*/
using System;
using System.Collections.Generic;
namespace 适配器模式
{
class Program
{
public static void Main(string[] args)
{
Stack<int> s = new LinkedListStack<int>();
s.Push(1);
s.Push(100);
int val1 = s.Pop();
int val2 = s.Pop();
Console.WriteLine("val1 = {0}; val2 = {1}.", val1, val2);
// TODO: Implement Functionality Here
Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
}
}
}