File tree Expand file tree Collapse file tree
DesignPatterns/AdapterPattern/AdapterPattern Expand file tree Collapse file tree Original file line number Diff line number Diff line change 4545 <ItemGroup >
4646 <Compile Include =" Adaptee.cs" />
4747 <Compile Include =" Adapter.cs" />
48+ <Compile Include =" NoteBook.cs" />
4849 <Compile Include =" Program.cs" />
4950 <Compile Include =" Properties\AssemblyInfo.cs" />
5051 <Compile Include =" Target.cs" />
Original file line number Diff line number Diff line change 1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Linq ;
4+ using System . Text ;
5+ using System . Threading . Tasks ;
6+
7+ namespace AdapterPattern
8+ {
9+ /// <summary>
10+ /// 笔记本类
11+ /// </summary>
12+ class NoteBook
13+ {
14+ public void Work ( )
15+ {
16+ Console . WriteLine ( "笔记本开始工作了!" ) ;
17+ }
18+ }
19+ }
Original file line number Diff line number Diff line change 1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Linq ;
4+ using System . Text ;
5+ using System . Threading . Tasks ;
6+
7+ namespace AdapterPattern
8+ {
9+ /// <summary>
10+ /// 220v电源接口类
11+ /// </summary>
12+ class PowerPort220V
13+ {
14+ /// <summary>
15+ /// 适配器提供电源
16+ /// </summary>
17+ public void PowerSupply ( )
18+ {
19+ Console . WriteLine ( "输出220电压!" ) ;
20+ }
21+ }
22+
23+
24+ class PowerPort110V
25+ {
26+ public void PowerSupply ( )
27+ {
28+ Console . WriteLine ( "输出110V电压!" ) ;
29+ }
30+ }
31+ }
Original file line number Diff line number Diff line change 1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Linq ;
4+ using System . Text ;
5+ using System . Threading . Tasks ;
6+
7+ namespace AdapterPattern
8+ {
9+ /// <summary>
10+ /// 适配器接口
11+ /// </summary>
12+ public interface PowerPortAdapter
13+ {
14+ void PowerSupply ( ) ;
15+ }
16+
17+ /// <summary>
18+ /// 110V电源适配器类
19+ /// </summary>
20+ class Adapter110V : PowerPort110V , PowerPortAdapter
21+ {
22+ NoteBook noteBook = new NoteBook ( ) ;
23+
24+ public void PowerSupply ( )
25+ {
26+ base . PowerSupply ( ) ;
27+ Console . WriteLine ( "适配器将电源转换成了笔记本需要的!" ) ;
28+ noteBook . Work ( ) ;
29+ }
30+ }
31+
32+ class Adapter220C : PowerPort220V , PowerPortAdapter
33+ {
34+ NoteBook noteBook = new NoteBook ( ) ;
35+
36+ public void PowerSupply ( )
37+ {
38+ base . PowerSupply ( ) ;
39+ Console . WriteLine ( "适配器将电源转换成了笔记本需要的!" ) ;
40+ noteBook . Work ( ) ;
41+ }
42+ }
43+ }
Original file line number Diff line number Diff line change @@ -17,6 +17,13 @@ static void Main(string[] args)
1717 //客户的特殊请求,调用Adaptee方法
1818 target = new Adapter ( ) ;
1919 target . Requset ( ) ;
20+
21+ //笔记本案例测试
22+ PowerPortAdapter adapter = new Adapter110V ( ) ;
23+ adapter . PowerSupply ( ) ;
24+ adapter = new Adapter220V ( ) ;
25+ adapter . PowerSupply ( ) ;
26+
2027 }
2128 }
2229}
You can’t perform that action at this time.
0 commit comments