适配器模式(Adapter):将一个类的一个接口转换成客户希望的另外一个接口。Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。
1、两个类所做的事情相同或相似,但是具有不同的接口时要使用它。(DataAdapter用作DataSet和数据源之间的适配器以便检索和保存数据)
适配器模式代码
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4
5 namespace Adapter
6 {
7 class Program
8 {
9 static void Main(string[] args)
10 {
11 Player james = new Forwards("詹姆斯");
12 james.Attack();
13 Player ym = new Translator("姚明");
14 ym.Attack();
15 ym.Defense();
16 Console.ReadLine();
17 }
18 }
19 abstract class Player
20 {
21 protected string name;
22 public Player(string name)
23 {
24 this.name = name;
25 }
26 public abstract void Attack(); //进攻
27 public abstract void Defense(); //防守
28 }
29 //前锋
30 class Forwards : Player
31 {
32 public Forwards(string name)
33 : base(name)
34 {
35
36 }
37 public override void Attack()
38 {
39 Console.WriteLine("前锋 {0} 进攻",name);
40 }
41 public override void Defense()
42 {
43 Console.WriteLine("前锋 {0} 防守", name);
44 }
45 }
46 //中锋
47 class Center : Player
48 {
49 public Center(string name)
50 : base(name)
51 {
52
53 }
54 public override void Attack()
55 {
56 Console.WriteLine("中锋 {0} 进攻", name);
57 }
58 public override void Defense()
59 {
60 Console.WriteLine("中锋 {0} 防守", name);
61 }
62 }
63 //外籍中锋
64 class ForeignCenter
65 {
66 private string _name;
67
68 public string Name
69 {
70 get { return _name; }
71 set { _name = value; }
72 }
73 public void 进攻()
74 {
75 Console.WriteLine("外籍中锋 {0} 进攻",Name);
76 }
77 public void 防守()
78 {
79 Console.WriteLine("外籍中锋 {0} 防守",Name);
80 }
81
82 }
83 //翻译者
84 class Translator:Player
85 {
86 private ForeignCenter wjzf = new ForeignCenter();
87 public Translator(string name)
88 : base(name)
89 {
90 wjzf.Name = name;
91 }
92 public override void Attack()
93 {
94 wjzf.进攻();
95 }
96 public override void Defense()
97 {
98 wjzf.防守();
99 }
100 }
101
102 }
2 using System.Collections.Generic;
3 using System.Text;
4
5 namespace Adapter
6 {
7 class Program
8 {
9 static void Main(string[] args)
10 {
11 Player james = new Forwards("詹姆斯");
12 james.Attack();
13 Player ym = new Translator("姚明");
14 ym.Attack();
15 ym.Defense();
16 Console.ReadLine();
17 }
18 }
19 abstract class Player
20 {
21 protected string name;
22 public Player(string name)
23 {
24 this.name = name;
25 }
26 public abstract void Attack(); //进攻
27 public abstract void Defense(); //防守
28 }
29 //前锋
30 class Forwards : Player
31 {
32 public Forwards(string name)
33 : base(name)
34 {
35
36 }
37 public override void Attack()
38 {
39 Console.WriteLine("前锋 {0} 进攻",name);
40 }
41 public override void Defense()
42 {
43 Console.WriteLine("前锋 {0} 防守", name);
44 }
45 }
46 //中锋
47 class Center : Player
48 {
49 public Center(string name)
50 : base(name)
51 {
52
53 }
54 public override void Attack()
55 {
56 Console.WriteLine("中锋 {0} 进攻", name);
57 }
58 public override void Defense()
59 {
60 Console.WriteLine("中锋 {0} 防守", name);
61 }
62 }
63 //外籍中锋
64 class ForeignCenter
65 {
66 private string _name;
67
68 public string Name
69 {
70 get { return _name; }
71 set { _name = value; }
72 }
73 public void 进攻()
74 {
75 Console.WriteLine("外籍中锋 {0} 进攻",Name);
76 }
77 public void 防守()
78 {
79 Console.WriteLine("外籍中锋 {0} 防守",Name);
80 }
81
82 }
83 //翻译者
84 class Translator:Player
85 {
86 private ForeignCenter wjzf = new ForeignCenter();
87 public Translator(string name)
88 : base(name)
89 {
90 wjzf.Name = name;
91 }
92 public override void Attack()
93 {
94 wjzf.进攻();
95 }
96 public override void Defense()
97 {
98 wjzf.防守();
99 }
100 }
101
102 }