适配器模式的主要作用是使得原本不能一起工作的类,通过某种方式, 可以一起工作。
1 /// <summary>
2 /// 比方说 手机数据线要连接 优盘, 中间必须要一个转换接口 才可以连接一起工作
3 /// 将一个类的接口转换成客户希望的另一个接口, Adapter 模式使得
4 /// 原本由于接口不兼容而不可以一起工作的那些类可以一起工作
5 /// </summary>
6 class Program
7 {
8 static void Main(string[] args)
9 {
10 MobileData mobile = new Adapter();
11 mobile.GetPower();
12 Console.Read();
13 }
14 }
15
16 public class MobileData
17 {
18 public virtual void GetPower()
19 {
20 Console.WriteLine("手机数据线可以用来充电");
21 }
22 }
23
24 public class Adapter : MobileData
25 {
26 USBDriver usb = new USBDriver();
27 public override void GetPower()
28 {
29 usb.Storage();
30 }
31 }
32
33 public class USBDriver
34 {
35 public void Storage()
36 {
37 Console.WriteLine("U盘可以存储数据");
38 }
39 }