
1 namespace Adapter
2 {
3 class Target
4 {
5 public Target()
6 {
7 var str = "test";
8 Console.WriteLine(str);
9 }
10 public virtual void Request()
11 {
12 Console.WriteLine("普通请求!");
13 }
14 }
15 /// <summary>
16 /// 需要适配的类。
17 /// </summary>
18 class Adatee
19 {
20 public void specificRequest()
21 {
22 Console.WriteLine("特殊请求!");
23 }
24 }
25 /// <summary>
26 /// 适配器(继承目标类)
27 /// 通过内部包装一个Adatee对象,把源有接口转换成目标接口。
28 /// </summary>
29 class Adapter:Target
30 {
31 private Adatee adatee = new Adatee();
32 public override void Request()
33 {
34 adatee.specificRequest();//把表面上调用Request()方法变成调用specificRequest()。
35 }
36 }
37 class Program
38 {
39 static void Main(string[] args)
40 {
41 Target tar = new Adapter();
42 tar.Request();//对于客户端来说,调用的就是Target的Request()方法。
43 Console.ReadKey();
44 }
45 }
46 }
47