Skip to content

Commit aa4867a

Browse files
committed
Update strategy pattern examples
1 parent 1a7769b commit aa4867a

File tree

16 files changed

+54
-82
lines changed

16 files changed

+54
-82
lines changed

src/BehavioralPatterns/Strategy/StrategyLibrary/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
ShippingExecutor.Execute();
1414
SortingExecutor.Execute();
1515
}
16-
}
16+
}

src/BehavioralPatterns/Strategy/StrategyLibrary/ShippingExample/Address.cs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,11 @@
22

33
public class Address
44
{
5-
public string ContactName { get; set; }
6-
7-
public string AddressLine1 { get; set; }
8-
9-
public string AddressLine2 { get; set; }
10-
11-
public string City { get; set; }
12-
13-
public string Region { get; set; }
14-
15-
public string Country { get; set; }
16-
17-
public string PostalCode { get; set; }
18-
}
5+
public string ContactName { get; set; } = string.Empty;
6+
public string AddressLine1 { get; set; } = string.Empty;
7+
public string AddressLine2 { get; set; } = string.Empty;
8+
public string City { get; set; } = string.Empty;
9+
public string Region { get; set; } = string.Empty;
10+
public string Country { get; set; } = string.Empty;
11+
public string PostalCode { get; set; } = string.Empty;
12+
}

src/BehavioralPatterns/Strategy/StrategyLibrary/ShippingExample/Order.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@
22

33
public class Order
44
{
5-
public string Description { get; set; }
5+
public Order(string description, Address origin, Address destination)
6+
{
7+
Description = description;
8+
Origin = origin;
9+
Destination = destination;
10+
}
611

7-
public Address Origin { get; set; }
8-
9-
public Address Destination { get; set; }
10-
}
12+
public string Description { get; }
13+
public Address Origin { get; }
14+
public Address Destination { get; }
15+
}

src/BehavioralPatterns/Strategy/StrategyLibrary/ShippingExample/ShippingCostCalculationService.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ public ShippingCostCalculationService(IShippingProvider shippingCostStrategy)
1111
_shippingProvider = shippingCostStrategy;
1212
}
1313

14-
public decimal Calculate(Order order)
15-
{
16-
return _shippingProvider.CalculateCost(order);
17-
}
18-
}
14+
public decimal Calculate(Order order) =>
15+
_shippingProvider.CalculateCost(order);
16+
}

src/BehavioralPatterns/Strategy/StrategyLibrary/ShippingExample/ShippingExecutor.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,12 @@ public static void Execute()
3232
PostalCode = "02367",
3333
};
3434

35-
var order = new Order
36-
{
37-
Origin = originAddress,
38-
Destination = destinationAddress,
39-
Description = "Xiaomi Mi 10T Pro",
40-
};
41-
35+
var order = new Order("Xiaomi Mi 10T Pro", originAddress, destinationAddress);
4236
IShippingProvider fedEx = new FedEx();
4337
var shippingCostCalculationService = new ShippingCostCalculationService(fedEx);
4438

4539
var price = shippingCostCalculationService.Calculate(order);
4640

4741
Console.WriteLine($"Shipping price for {order.Description} is {price:C}");
4842
}
49-
}
43+
}

src/BehavioralPatterns/Strategy/StrategyLibrary/ShippingExample/ShippingProviders/Common/IShippingProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
public interface IShippingProvider
44
{
55
decimal CalculateCost(Order order);
6-
}
6+
}

src/BehavioralPatterns/Strategy/StrategyLibrary/ShippingExample/ShippingProviders/FedEx.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,5 @@ namespace StrategyLibrary.ShippingExample.ShippingProviders;
44

55
public class FedEx : IShippingProvider
66
{
7-
public decimal CalculateCost(Order order)
8-
{
9-
return 10;
10-
}
11-
}
7+
public decimal CalculateCost(Order order) => 10;
8+
}

src/BehavioralPatterns/Strategy/StrategyLibrary/ShippingExample/ShippingProviders/RoyalMail.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,5 @@ namespace StrategyLibrary.ShippingExample.ShippingProviders;
44

55
public class RoyalMail : IShippingProvider
66
{
7-
public decimal CalculateCost(Order order)
8-
{
9-
return 8.5m;
10-
}
11-
}
7+
public decimal CalculateCost(Order order) => 8.5m;
8+
}

src/BehavioralPatterns/Strategy/StrategyLibrary/ShippingExample/ShippingProviders/UnitedParcelService.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,5 @@ namespace StrategyLibrary.ShippingExample.ShippingProviders;
44

55
public class UnitedParcelService : IShippingProvider
66
{
7-
public decimal CalculateCost(Order order)
8-
{
9-
return 9;
10-
}
11-
}
7+
public decimal CalculateCost(Order order) => 9;
8+
}

src/BehavioralPatterns/Strategy/StrategyLibrary/SortingExample/Person.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,8 @@ public Person(string firstName, string lastName, int yearOfBirth)
1010
}
1111

1212
public string FirstName { get; set; }
13-
1413
public string LastName { get; set; }
15-
1614
public int YearOfBirth { get; set; }
1715

18-
public override string ToString()
19-
{
20-
return $"{FirstName} {LastName} - {YearOfBirth}";
21-
}
22-
}
16+
public override string ToString() => $"{FirstName} {LastName} - {YearOfBirth}";
17+
}

0 commit comments

Comments
 (0)