-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStrategy.cs
More file actions
38 lines (30 loc) · 878 Bytes
/
Strategy.cs
File metadata and controls
38 lines (30 loc) · 878 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
void Main()
{
var paidWithCash = new ShoppingCart(new PayWithCash());
paidWithCash.Checkout();
var paidWithCard = new ShoppingCart(new PayWithCreditCard());
paidWithCard.Checkout();
}
public class ShoppingCart
{
readonly PaymentStrategy _paymentStrategy;
int Total { get; set; } = 5;
public ShoppingCart(PaymentStrategy paymentStrategy)
=> _paymentStrategy = paymentStrategy;
public void Checkout()
=> _paymentStrategy.Checkout(Total);
}
public interface PaymentStrategy
{
void Checkout(int amount);
}
public class PayWithCash : PaymentStrategy
{
public void Checkout(int amount)
=> $"Paying {amount}€ in cash".Dump(nameof(PayWithCash));
}
public class PayWithCreditCard : PaymentStrategy
{
public void Checkout(int amount)
=> $"Paying {amount}€ in card".Dump(nameof(PayWithCreditCard));
}