Scenario:
Create a e-Retail Checkout.
Solution:
This pattern hides the complexities of the larger system and provides a simpler interface to the client. Example: The Checkout, involves multiple steps to move the Order ahead, but through Facade we expose only Checkout.TryProcess method to the client and hiding the complexity.
- Checkout
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63 | namespace TestProject
{
public class Shopper
{
public int Id { get; set; }
public string? Name { get; set; }
public string? Address { get; set; }
}
public class OrderService
{
public bool TryCreateOrder(string order, out Guid? orderId)
{
orderId = Guid.NewGuid();
return true;
}
}
public class ValidationService
{
public bool IsAddressValid(string? address)
{
return true;
}
}
public class FraudService
{
public bool IsShopperLegit(Shopper shopper)
{
return true;
}
}
public class Checkout
{
public bool TryProcess(string order, Shopper shopper, out Guid? orderId)
{
var orderService = new OrderService();
var fraudService = new FraudService();
var validationService = new ValidationService();
if (validationService.IsAddressValid(shopper.Address))
{
Console.WriteLine("Shopper address is valid");
if (fraudService.IsShopperLegit(shopper))
{
Console.WriteLine("Shopper passed Fraud");
}
if (orderService.TryCreateOrder(order, out orderId))
{
Console.WriteLine($"Order created. Order ID: {orderId}");
return true;
}
}
orderId = null;
return false;
}
}
} |
- Call Checkout/TrProcess which internally does validation, fraud check and order creation and if all successful returns the orderId.
var checkout = new Checkout(); Console.WriteLine(checkout.TryProcess("order details", new Shopper
{
Address = "Test",
Name = "Ram"
}, out var orderId)
? $"Order Created Successfully"
: $"Order Creation Failed");
Console.ReadLine();
| |
|
No comments:
Post a Comment