Skip to content

Commit 6f523b1

Browse files
committed
适配器模式
1 parent 235c3c1 commit 6f523b1

File tree

6 files changed

+213
-0
lines changed

6 files changed

+213
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{A0D5F127-471C-47AC-A9E0-F2BB72D2EF8F}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>AdapterPattern</RootNamespace>
11+
<AssemblyName>AdapterPattern</AssemblyName>
12+
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
15+
</PropertyGroup>
16+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
17+
<PlatformTarget>AnyCPU</PlatformTarget>
18+
<DebugSymbols>true</DebugSymbols>
19+
<DebugType>full</DebugType>
20+
<Optimize>false</Optimize>
21+
<OutputPath>bin\Debug\</OutputPath>
22+
<DefineConstants>DEBUG;TRACE</DefineConstants>
23+
<ErrorReport>prompt</ErrorReport>
24+
<WarningLevel>4</WarningLevel>
25+
</PropertyGroup>
26+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
27+
<PlatformTarget>AnyCPU</PlatformTarget>
28+
<DebugType>pdbonly</DebugType>
29+
<Optimize>true</Optimize>
30+
<OutputPath>bin\Release\</OutputPath>
31+
<DefineConstants>TRACE</DefineConstants>
32+
<ErrorReport>prompt</ErrorReport>
33+
<WarningLevel>4</WarningLevel>
34+
</PropertyGroup>
35+
<ItemGroup>
36+
<Reference Include="System" />
37+
<Reference Include="System.Core" />
38+
<Reference Include="System.Xml.Linq" />
39+
<Reference Include="System.Data.DataSetExtensions" />
40+
<Reference Include="Microsoft.CSharp" />
41+
<Reference Include="System.Data" />
42+
<Reference Include="System.Net.Http" />
43+
<Reference Include="System.Xml" />
44+
</ItemGroup>
45+
<ItemGroup>
46+
<Compile Include="IChargingLine.cs" />
47+
<Compile Include="Program.cs" />
48+
<Compile Include="Properties\AssemblyInfo.cs" />
49+
</ItemGroup>
50+
<ItemGroup>
51+
<None Include="App.config" />
52+
</ItemGroup>
53+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
54+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
55+
Other similar extension points exist, see Microsoft.Common.targets.
56+
<Target Name="BeforeBuild">
57+
</Target>
58+
<Target Name="AfterBuild">
59+
</Target>
60+
-->
61+
</Project>

AdapterPattern/App.config

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
5+
</startup>
6+
</configuration>

AdapterPattern/IChargingLine.cs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
/// 最终要适配成的目标角色
12+
/// </summary>
13+
public interface IChargingLine
14+
{
15+
/// <summary>
16+
/// 充电方法
17+
/// </summary>
18+
void Charging();
19+
}
20+
21+
/// <summary>
22+
/// USB数据线(支持USB-Micro端口的设备)
23+
/// </summary>
24+
public class USBMicroLine : IChargingLine
25+
{
26+
public void Charging()
27+
{
28+
Console.WriteLine("为支持USB-Micro端口的设备充电!");
29+
}
30+
}
31+
32+
/// <summary>
33+
/// 原装数据线
34+
/// 未定义充电标准的充电方法
35+
/// </summary>
36+
public class USBLine
37+
{
38+
public void Charge()
39+
{
40+
Console.WriteLine("为设备充电!");
41+
}
42+
}
43+
44+
/// <summary>
45+
/// 苹果充电线适配器
46+
/// </summary>
47+
public class USBlightingLineAdapter : USBLine, IChargingLine
48+
{
49+
public void Charging()
50+
{
51+
Console.WriteLine("对USB-Lighting端口的数据线进行适配!");
52+
base.Charge();
53+
}
54+
}
55+
56+
/// <summary>
57+
/// 小米5充电线适配器
58+
/// </summary>
59+
public class USBTypecLineAdapter: USBLine,IChargingLine
60+
{
61+
public void Charging()
62+
{
63+
Console.WriteLine("对USB-TypeC端口的数据线进行适配!");
64+
base.Charge();
65+
}
66+
}
67+
}

AdapterPattern/Program.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
class Program
10+
{
11+
static void Main(string[] args)
12+
{
13+
//在未定义充电标准之前,各个厂家充电线的实现各不相同,但都可以为自家品牌设备充电
14+
USBLine usbLine = new USBLine();
15+
usbLine.Charge();
16+
17+
Console.WriteLine("-------------------");
18+
//随着电器设备越来越多,各家的充电设备不能通用,造成很多不便,为了共用,厂家联合推出标准充电接口。
19+
20+
//USB-Micro实现标准接口。
21+
IChargingLine microLine = new USBMicroLine();
22+
microLine.Charging();
23+
24+
Console.WriteLine("-------------------");
25+
26+
//现在手里有一个未实现充电标准的充电线,通过适配器,为小米5设备充电
27+
IChargingLine typeCLineAdapter = new USBTypecLineAdapter();
28+
typeCLineAdapter.Charging();
29+
30+
Console.WriteLine("-------------------");
31+
32+
//现在手里有一个未实现充电标准的充电线,通过适配器,为苹果设备充电
33+
IChargingLine lightingLineAdapter = new USBlightingLineAdapter();
34+
lightingLineAdapter.Charging();
35+
}
36+
}
37+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// 有关程序集的一般信息由以下
6+
// 控制。更改这些特性值可修改
7+
// 与程序集关联的信息。
8+
[assembly: AssemblyTitle("AdapterPattern")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("Microsoft")]
12+
[assembly: AssemblyProduct("AdapterPattern")]
13+
[assembly: AssemblyCopyright("Copyright © Microsoft 2016")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
//将 ComVisible 设置为 false 将使此程序集中的类型
18+
//对 COM 组件不可见。 如果需要从 COM 访问此程序集中的类型,
19+
//请将此类型的 ComVisible 特性设置为 true。
20+
[assembly: ComVisible(false)]
21+
22+
// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
23+
[assembly: Guid("a0d5f127-471c-47ac-a9e0-f2bb72d2ef8f")]
24+
25+
// 程序集的版本信息由下列四个值组成:
26+
//
27+
// 主版本
28+
// 次版本
29+
// 生成号
30+
// 修订号
31+
//
32+
//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值,
33+
// 方法是按如下所示使用“*”: :
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]

DesignPattern.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ChainOfResponsibility", "Ch
2323
EndProject
2424
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DecoratorPattern", "DecoratorPattern\DecoratorPattern.csproj", "{AD530192-DE5C-469F-B2C0-9314CA5CA686}"
2525
EndProject
26+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AdapterPattern", "AdapterPattern\AdapterPattern.csproj", "{A0D5F127-471C-47AC-A9E0-F2BB72D2EF8F}"
27+
EndProject
2628
Global
2729
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2830
Debug|Any CPU = Debug|Any CPU
@@ -69,6 +71,10 @@ Global
6971
{AD530192-DE5C-469F-B2C0-9314CA5CA686}.Debug|Any CPU.Build.0 = Debug|Any CPU
7072
{AD530192-DE5C-469F-B2C0-9314CA5CA686}.Release|Any CPU.ActiveCfg = Release|Any CPU
7173
{AD530192-DE5C-469F-B2C0-9314CA5CA686}.Release|Any CPU.Build.0 = Release|Any CPU
74+
{A0D5F127-471C-47AC-A9E0-F2BB72D2EF8F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
75+
{A0D5F127-471C-47AC-A9E0-F2BB72D2EF8F}.Debug|Any CPU.Build.0 = Debug|Any CPU
76+
{A0D5F127-471C-47AC-A9E0-F2BB72D2EF8F}.Release|Any CPU.ActiveCfg = Release|Any CPU
77+
{A0D5F127-471C-47AC-A9E0-F2BB72D2EF8F}.Release|Any CPU.Build.0 = Release|Any CPU
7278
EndGlobalSection
7379
GlobalSection(SolutionProperties) = preSolution
7480
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)