适配器模式将不改变原有实现的基础上,原来本不兼容的接口转换为兼容的接口:
eg.1
arraylistTOother
1 using System;
2 using System.Collections;
3
4 //本例是个对象适配器
5 //将一个ArrayList类型的对象适配成为一个可供新程序调用的Istack类型的对象
6
7 interface IStack //客户希望的接口
8 {
9 void push(object item);
10 object Pop();
11 object Peek();
12 }
13
14 class Adapter : IStack //适配对象
15 {
16 ArrayList adpatee;//被适配的对象,由外部传入,这里简写用以代表。
17
18 public Adapter()
19 {
20 adpatee = new ArrayList();
21 }
22
23 public void Push(object item)
24 {
25 adpatee.Add(item);
26 }
27
28 object Pop()
29 {
30 adpatee.RemoveAt(adpatee.Count - 1);
31 }
32
33 object Peek()
34 {
35 return adpatee[adpatee.Count - 1];
36 }
37
38 }
1 using System;
2 using System.Collections;
3
4 //本例是个对象适配器
5 //将一个ArrayList类型的对象适配成为一个可供新程序调用的Istack类型的对象
6
7 interface IStack //客户希望的接口
8 {
9 void push(object item);
10 object Pop();
11 object Peek();
12 }
13
14 class Adapter : IStack //适配对象
15 {
16 ArrayList adpatee;//被适配的对象,由外部传入,这里简写用以代表。
17
18 public Adapter()
19 {
20 adpatee = new ArrayList();
21 }
22
23 public void Push(object item)
24 {
25 adpatee.Add(item);
26 }
27
28 object Pop()
29 {
30 adpatee.RemoveAt(adpatee.Count - 1);
31 }
32
33 object Peek()
34 {
35 return adpatee[adpatee.Count - 1];
36 }
37
38 }
eg.2
类适配器
1 using System;
2 using System.Collections;
3
4
5 //类适配器
6 //上例的另外一种写法
7
8 interface IStack //客户希望的接口
9 {
10 void push(object item);
11 object Pop();
12 object Peek();
13 }
14
15
16 class Adapter : ArrayList, IStack //适配对象
17 {
18 ArrayList adpatee;//被适配的对象
19
20 public void Push(object item)
21 {
22 this.Add(item);
23 }
24
25 object Pop()
26 {
27 this.RemoveAt(list.Count - 1);
28 }
29
30 object Peek()
31 {
32 return this[this.Count - 1];
33 }
34
35 }
36
37
38
39
40 //现有类
41 class ExistingClass
42 {
43 public void SpecificRequest1()
44 { }
45 public void SpecificRequest2()
46 { }
47 }
48
49
50 //另外一个系统
51 class MySystem
52 {
53 public void Process(ITarget target)
54 {
55
56 }
57 }
58
59 //新环境使用的接口
60 interface ITarget
61 {
62 void Request();
63 }
64
65 class Adapter : ITarget
66 {
67 ExistingClass adaptee;
68 public void Request()
69 {
70 adaptee.SpecificRequest1();
71 adaptee.SpecificRequest2();
72 }
73
74 }
1 using System;
2 using System.Collections;
3
4
5 //类适配器
6 //上例的另外一种写法
7
8 interface IStack //客户希望的接口
9 {
10 void push(object item);
11 object Pop();
12 object Peek();
13 }
14
15
16 class Adapter : ArrayList, IStack //适配对象
17 {
18 ArrayList adpatee;//被适配的对象
19
20 public void Push(object item)
21 {
22 this.Add(item);
23 }
24
25 object Pop()
26 {
27 this.RemoveAt(list.Count - 1);
28 }
29
30 object Peek()
31 {
32 return this[this.Count - 1];
33 }
34
35 }
36
37
38
39
40 //现有类
41 class ExistingClass
42 {
43 public void SpecificRequest1()
44 { }
45 public void SpecificRequest2()
46 { }
47 }
48
49
50 //另外一个系统
51 class MySystem
52 {
53 public void Process(ITarget target)
54 {
55
56 }
57 }
58
59 //新环境使用的接口
60 interface ITarget
61 {
62 void Request();
63 }
64
65 class Adapter : ITarget
66 {
67 ExistingClass adaptee;
68 public void Request()
69 {
70 adaptee.SpecificRequest1();
71 adaptee.SpecificRequest2();
72 }
73
74 }
eg.3
适配器1
1
2
3 using System.Data.SqlClient;
4 using System.Data;
5
6
7 class EmployeeDAO
8 {
9 public DataSet GetAllEmployee()
10 {
11 DbDataAdapter dataAdapter = new SqlDataAdapter();
12 //.
13
14 DataSet dataSet = new DataSet();
15 dataAdapter.Fill(dataSet);
16
17 }
18 }
19
20 class DataSet
21 {
22 DbDataAdapter dataAdaoter;//抽象类
23
24 public DataSet(DbDataAdapter dataAdaoter)
25 {
26 this.DataTable = dataAdapter.Fill();
27 }
28 }
29
30
31 //按照适配器模式写Dataset是不是更好呢:
32 DataSet dataSet = new DataSet(new sqlDataAdapter(..))
1
2
3 using System.Data.SqlClient;
4 using System.Data;
5
6
7 class EmployeeDAO
8 {
9 public DataSet GetAllEmployee()
10 {
11 DbDataAdapter dataAdapter = new SqlDataAdapter();
12 //.
13
14 DataSet dataSet = new DataSet();
15 dataAdapter.Fill(dataSet);
16
17 }
18 }
19
20 class DataSet
21 {
22 DbDataAdapter dataAdaoter;//抽象类
23
24 public DataSet(DbDataAdapter dataAdaoter)
25 {
26 this.DataTable = dataAdapter.Fill();
27 }
28 }
29
30
31 //按照适配器模式写Dataset是不是更好呢:
32 DataSet dataSet = new DataSet(new sqlDataAdapter(..))
eg.4
比较适配器
using System;
using System.Collections;
class Employee
{
int age;
public int Age
{
get { return this.age}
set { this.age = value}
}
}
class EmployeeSortAdapter : IComparer
{
public int Compare(object obj1, object obj2)
{
if (obj1.GetType() != typeof(Employee)
|| obj2.GetType() != typeof(Employee))
{
throw new Exception();
}
Employee e1 = (Employee)obj1;
Employee e2 = (Employee)obj2;
if (e1.Age == e2.Age)
{
return 0;
}
else if (e1.Age > e2.Age)
{
return 1;
}
else if (e1.Age < e2.Age)
{
return -1;
}
}
}
class App
{
public static void Main()
{
Emplyee[] emplyee = new Employee[100];
//..
Array.Sort(emplyee,new EmployeeSortAdapter());
}
}
using System;
using System.Collections;
class Employee
{
int age;
public int Age
{
get { return this.age}
set { this.age = value}
}
}
class EmployeeSortAdapter : IComparer
{
public int Compare(object obj1, object obj2)
{
if (obj1.GetType() != typeof(Employee)
|| obj2.GetType() != typeof(Employee))
{
throw new Exception();
}
Employee e1 = (Employee)obj1;
Employee e2 = (Employee)obj2;
if (e1.Age == e2.Age)
{
return 0;
}
else if (e1.Age > e2.Age)
{
return 1;
}
else if (e1.Age < e2.Age)
{
return -1;
}
}
}
class App
{
public static void Main()
{
Emplyee[] emplyee = new Employee[100];
//..
Array.Sort(emplyee,new EmployeeSortAdapter());
}
}