Skip to content

Commit 4784c92

Browse files
committed
Update adapter example
1 parent d849aeb commit 4784c92

File tree

13 files changed

+65
-76
lines changed

13 files changed

+65
-76
lines changed

src/StructuralPatterns/Adapter/AdapterLibrary/BillingSystemExample/BillingSystemExecutor.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ namespace AdapterLibrary.BillingSystemExample;
55
public static class BillingSystemExecutor
66
{
77
/// <summary>
8-
/// The client code doesnt get coupled to the concrete adapter class as long as it works with the adapter
9-
/// via the client interface. Thanks to this, you can introduce new types of adapters into the program without
10-
/// breaking the existing client code. This can be useful when the interface of the service class gets
11-
/// changed or replaced: you can just create a new adapter class without changing the client code.
8+
/// The client code doesn't get coupled to the concrete adapter class as long as it works with the adapter via the client interface.
9+
/// Thanks to this, you can introduce new types of adapters into the program without breaking the existing client code.
10+
/// This can be useful when the interface of the service class gets changed or replaced:
11+
/// you can just create a new adapter class without changing the client code.
1212
/// </summary>
1313
public static void Execute()
1414
{
@@ -18,9 +18,10 @@ public static void Execute()
1818
string[,] employeesInfo = hrSystem.GetEmployeesInfo();
1919

2020
// Existing HR system wants to process salaries,
21-
// but since it isn't compatible with third party billing system interface we must use adapter
21+
// but since it isn't compatible with the 3rd party billing system interface we must use adapter.
22+
// The 3rd party billing system expects List<Employee>, but the current HR system works only with string[,].
23+
// Hide that using adapter design pattern (note that the client is not aware of what's happening under the hood).
2224
ISalaryProcessor salaryProcessor = new HRSystemAdapter();
23-
2425
salaryProcessor.ProcessSalaries(employeesInfo);
2526
}
26-
}
27+
}

src/StructuralPatterns/Adapter/AdapterLibrary/BillingSystemExample/Employee.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
public class Employee
44
{
55
public int Id { get; set; }
6-
7-
public string Name { get; set; }
8-
6+
public string Name { get; set; } = string.Empty;
97
public decimal Salary { get; set; }
10-
}
8+
}

src/StructuralPatterns/Adapter/AdapterLibrary/BillingSystemExample/HRSystem.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,17 @@
22

33
/// <summary>
44
/// This class contains the functionality which the client requires,
5-
/// but its not compatible with the existing client code.
5+
/// but it's not compatible with the existing client code.
66
/// So, it requires some adaptation before the client code can use it.
77
/// </summary>
88
public class HRSystem
99
{
10-
public string[,] GetEmployeesInfo()
11-
{
12-
return new string[4, 3]
10+
public string[,] GetEmployeesInfo() =>
11+
new string[4, 3]
1312
{
1413
{ "1", "John", "5950" },
1514
{ "2", "Anna", "5800" },
1615
{ "3", "Nicole", "6100" },
1716
{ "4", "Steven", "5000" },
1817
};
19-
}
20-
}
18+
}
Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace AdapterLibrary.BillingSystemExample;
1+
using System.Globalization;
2+
3+
namespace AdapterLibrary.BillingSystemExample;
24

35
/// <summary>
46
/// This is a class which makes two incompatible systems to work together.
@@ -19,48 +21,46 @@ public HRSystemAdapter()
1921
public void ProcessSalaries(string[,] rawEmployees)
2022
{
2123
var employeesForProcessing = PrepareEmployeesForSalaryProcessing(rawEmployees);
22-
2324
_thirdPartyBillingSystem.ProcessSalary(employeesForProcessing);
2425
}
2526

2627
private List<Employee> PrepareEmployeesForSalaryProcessing(string[,] rawEmployees)
2728
{
2829
var employeesForProcessing = new List<Employee>();
2930

30-
for (int i = 0; i < rawEmployees.GetLength(0); i++)
31+
for (var i = 0; i < rawEmployees.GetLength(0); i++)
3132
{
32-
string id = null;
33-
string name = null;
34-
string salary = null;
33+
var id = string.Empty;
34+
var name = string.Empty;
35+
var salary = string.Empty;
3536

36-
for (int j = 0; j < rawEmployees.GetLength(1); j++)
37+
for (var j = 0; j < rawEmployees.GetLength(1); j++)
3738
{
38-
if (j == 0)
39-
{
40-
id = rawEmployees[i, j];
41-
}
42-
else if (j == 1)
39+
switch (j)
4340
{
44-
name = rawEmployees[i, j];
45-
}
46-
else
47-
{
48-
salary = rawEmployees[i, j];
41+
case 0:
42+
id = rawEmployees[i, j];
43+
break;
44+
case 1:
45+
name = rawEmployees[i, j];
46+
break;
47+
default:
48+
salary = rawEmployees[i, j];
49+
break;
4950
}
5051
}
5152

5253
var employee = new Employee
5354
{
54-
Id = Convert.ToInt32(id),
55+
Id = Convert.ToInt32(id, CultureInfo.InvariantCulture),
5556
Name = name,
56-
Salary = Convert.ToDecimal(salary),
57+
Salary = Convert.ToDecimal(salary, CultureInfo.InvariantCulture),
5758
};
58-
5959
employeesForProcessing.Add(employee);
6060
}
6161

6262
Console.WriteLine("Adapter converted array of employees to list of employees...");
6363

6464
return employeesForProcessing;
6565
}
66-
}
66+
}

src/StructuralPatterns/Adapter/AdapterLibrary/BillingSystemExample/ISalaryProcessor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
/// </summary>
66
public interface ISalaryProcessor
77
{
8-
void ProcessSalaries(string[,] employees);
9-
}
8+
void ProcessSalaries(string[,] rawEmployees);
9+
}

src/StructuralPatterns/Adapter/AdapterLibrary/BillingSystemExample/ThirdPartyBillingSystem.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ public class ThirdPartyBillingSystem
88
{
99
public void ProcessSalary(List<Employee> employees)
1010
{
11-
foreach (Employee employee in employees)
11+
foreach (var employee in employees)
1212
{
1313
Console.WriteLine($"EUR {employee.Salary} salary credited to {employee.Name}'s account.");
1414
}
1515
}
16-
}
16+
}

src/StructuralPatterns/Adapter/AdapterLibrary/Executor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ public override void Execute()
1313
BillingSystemExecutor.Execute();
1414
MovieBroadcasterExecutor.Execute();
1515
}
16-
}
16+
}

src/StructuralPatterns/Adapter/AdapterLibrary/MovieBroadcasterExample/BroadcastAdapter.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
namespace AdapterLibrary.MovieBroadcasterExample;
55

66
/// <summary>
7-
/// There is no way to pass an xDocument from registry to the third party broadcaster,
7+
/// There is no way to pass an xDocument from the registry to the third party broadcaster,
88
/// so we need to create the adapter, which will make these two parties work together.
99
/// </summary>
1010
public class BroadcastAdapter : IBroadcaster
@@ -21,29 +21,29 @@ public BroadcastAdapter(MovieRegistry movieRegistry)
2121
public void BroadcastToExternalPartners()
2222
{
2323
string jsonMovies = ConvertRegistryMoviesToJson();
24-
2524
_thirdPartyBroadcaster.Broadcast(jsonMovies);
2625
}
2726

2827
private string ConvertRegistryMoviesToJson()
2928
{
3029
XDocument xmlMovies = _movieRegistry.GetAll();
31-
32-
Console.WriteLine("Movies from internal registry...");
30+
Console.WriteLine("Movies from the internal registry...");
3331
Console.WriteLine(xmlMovies);
3432

3533
IEnumerable<Movie> modelMovies = xmlMovies
36-
.Element("Movies")
34+
.Element("Movies")!
3735
.Elements("Movie")
3836
.Select(movie => new Movie
3937
{
40-
Name = movie.Attribute("Name").Value,
41-
ReleaseDate = DateTime.Parse(movie.Attribute("ReleaseDate").Value),
42-
Rating = Convert.ToDouble(movie.Attribute("Rating").Value),
38+
Name = movie.Attribute(nameof(Movie.Name))!.Value,
39+
ReleaseDate = DateTime.Parse(movie.Attribute(nameof(Movie.ReleaseDate))!.Value),
40+
Rating = Convert.ToDouble(movie.Attribute(nameof(Movie.Rating))!.Value),
4341
});
4442

4543
string jsonMovies = JsonConvert.SerializeObject(modelMovies, Formatting.Indented);
44+
Console.WriteLine("\nMovies from the internal registry are converted to JSON format");
45+
Console.WriteLine(jsonMovies);
4646

4747
return jsonMovies;
4848
}
49-
}
49+
}

src/StructuralPatterns/Adapter/AdapterLibrary/MovieBroadcasterExample/IBroadcaster.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
public interface IBroadcaster
44
{
55
void BroadcastToExternalPartners();
6-
}
6+
}

src/StructuralPatterns/Adapter/AdapterLibrary/MovieBroadcasterExample/Movie.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
public class Movie
44
{
5-
public string Name { get; set; }
6-
5+
public string Name { get; set; } = string.Empty;
76
public DateTime ReleaseDate { get; set; }
8-
97
public double Rating { get; set; }
10-
}
8+
}

0 commit comments

Comments
 (0)