Skip to content

Commit cbb43f3

Browse files
committed
add adapter design pattern
1 parent 3440087 commit cbb43f3

File tree

6 files changed

+69
-2
lines changed

6 files changed

+69
-2
lines changed

Src/DesignPatterns.sln

+9-2
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AbstractFactory", "Creation
1919
EndProject
2020
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Prototype", "Creational\Prototype\Prototype.csproj", "{2E5A8CCE-007E-4329-B6FB-319BDE24912E}"
2121
EndProject
22-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Builder", "Creational\Builder\Builder.csproj", "{3CA25D8B-0622-4CF7-BF21-28E7ADF52FE8}"
22+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Builder", "Creational\Builder\Builder.csproj", "{3CA25D8B-0622-4CF7-BF21-28E7ADF52FE8}"
2323
EndProject
24-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ObjectPool", "Creational\ObjectPool\ObjectPool.csproj", "{20C6B7F9-0822-4C7B-A827-7EEF63147225}"
24+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ObjectPool", "Creational\ObjectPool\ObjectPool.csproj", "{20C6B7F9-0822-4C7B-A827-7EEF63147225}"
25+
EndProject
26+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Adapter", "Structural\Adapter\Adapter.csproj", "{3C0DB726-A712-442A-A314-D8081F35474D}"
2527
EndProject
2628
Global
2729
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -57,6 +59,10 @@ Global
5759
{20C6B7F9-0822-4C7B-A827-7EEF63147225}.Debug|Any CPU.Build.0 = Debug|Any CPU
5860
{20C6B7F9-0822-4C7B-A827-7EEF63147225}.Release|Any CPU.ActiveCfg = Release|Any CPU
5961
{20C6B7F9-0822-4C7B-A827-7EEF63147225}.Release|Any CPU.Build.0 = Release|Any CPU
62+
{3C0DB726-A712-442A-A314-D8081F35474D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
63+
{3C0DB726-A712-442A-A314-D8081F35474D}.Debug|Any CPU.Build.0 = Debug|Any CPU
64+
{3C0DB726-A712-442A-A314-D8081F35474D}.Release|Any CPU.ActiveCfg = Release|Any CPU
65+
{3C0DB726-A712-442A-A314-D8081F35474D}.Release|Any CPU.Build.0 = Release|Any CPU
6066
EndGlobalSection
6167
GlobalSection(SolutionProperties) = preSolution
6268
HideSolutionNode = FALSE
@@ -69,6 +75,7 @@ Global
6975
{2E5A8CCE-007E-4329-B6FB-319BDE24912E} = {DAF1C73D-0282-4B44-B749-B1A6747D24AA}
7076
{3CA25D8B-0622-4CF7-BF21-28E7ADF52FE8} = {DAF1C73D-0282-4B44-B749-B1A6747D24AA}
7177
{20C6B7F9-0822-4C7B-A827-7EEF63147225} = {DAF1C73D-0282-4B44-B749-B1A6747D24AA}
78+
{3C0DB726-A712-442A-A314-D8081F35474D} = {860BC4CA-F738-497E-99D7-CD06270D96EE}
7279
EndGlobalSection
7380
GlobalSection(ExtensibilityGlobals) = postSolution
7481
SolutionGuid = {3FFEEAAE-F81B-4BCD-89B6-CD8AA0075382}

Src/Structural/Adapter/Adapter.csproj

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp3.1</TargetFramework>
6+
</PropertyGroup>
7+
8+
</Project>

Src/Structural/Adapter/Car.cs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace Adapter
2+
{
3+
public class Car
4+
{
5+
public string Move()
6+
{
7+
return "Move car .. ";
8+
}
9+
}
10+
}
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace Adapter
2+
{
3+
public interface IRailAdapter
4+
{
5+
string Move();
6+
}
7+
}

Src/Structural/Adapter/Program.cs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
3+
namespace Adapter
4+
{
5+
class Program
6+
{
7+
static void Main(string[] args)
8+
{
9+
Car car = new Car();
10+
IRailAdapter railAdapter = new RailAdapter(car);
11+
12+
Console.WriteLine("Adaptee interface is incompatible with the client.");
13+
Console.WriteLine("But with adapter client can call it's method.");
14+
15+
Console.WriteLine(railAdapter.Move());
16+
}
17+
}
18+
}

Src/Structural/Adapter/RailAdapter.cs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace Adapter
2+
{
3+
public class RailAdapter : IRailAdapter
4+
{
5+
private readonly Car _car;
6+
7+
public RailAdapter(Car car)
8+
{
9+
this._car = car;
10+
}
11+
12+
public string Move()
13+
{
14+
return $"'{this._car.Move()}' to rail";
15+
}
16+
}
17+
}

0 commit comments

Comments
 (0)