Skip to content

Commit 7d8a998

Browse files
committed
电源适配器案例
1 parent e7e0de6 commit 7d8a998

5 files changed

Lines changed: 101 additions & 0 deletions

File tree

DesignPatterns/AdapterPattern/AdapterPattern/AdapterPattern.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
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" />
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
}

DesignPatterns/AdapterPattern/AdapterPattern/Program.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff 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
}

0 commit comments

Comments
 (0)