-
-
Save MightyBlow/68356c347367c9a16432039388232db5 to your computer and use it in GitHub Desktop.
Замена условной логики полиморфизмом
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace CarService | |
{ | |
internal class Program | |
{ | |
internal static void Main(string[] args) | |
{ | |
var orderForm = new Order(); | |
PaymentSystem[] _paymentSystems = new[] | |
{ | |
new PaymentSystem("QIWI"), | |
new PaymentSystem("WebMoney"), | |
new PaymentSystem("Card") | |
}; | |
var paymentHandler = new PaymentHandler(_paymentSystems); | |
string selectedSystemId = orderForm.ShowForm(GetPaymentSystemIds(paymentHandler)); | |
paymentHandler.RequestPayment(selectedSystemId); | |
paymentHandler.ShowPaymentResult(selectedSystemId); | |
} | |
private static string[] GetPaymentSystemIds(PaymentHandler paymentHandler) | |
{ | |
string[] availablePaymentSystemIds = { }; | |
foreach (PaymentSystem paymentHandlerPaymentSystem in paymentHandler.PaymentSystems) | |
availablePaymentSystemIds.Append(paymentHandlerPaymentSystem.SystemId).ToArray(); | |
return availablePaymentSystemIds; | |
} | |
} | |
public interface IPaymentSystem | |
{ | |
void RequestPayment(string systemId); | |
void ShowPaymentResult(string systemId); | |
} | |
public class Order | |
{ | |
private string[] _paymentSystemsIds; | |
public string ShowForm(string[] paymentSystemsIds) | |
{ | |
_paymentSystemsIds = paymentSystemsIds; | |
Console.WriteLine(GetNamePaymentSystem()); | |
//симуляция веб интерфейса | |
Console.WriteLine("Какое системой вы хотите совершить оплату?"); | |
return Console.ReadLine(); | |
} | |
private string GetNamePaymentSystem() | |
{ | |
string infoPaymentSystem = "Мы принимаем: "; | |
foreach (string paymentSystemsId in _paymentSystemsIds) | |
infoPaymentSystem += $"{paymentSystemsId}, "; | |
return infoPaymentSystem + ":"; | |
} | |
} | |
public class PaymentHandler : IPaymentSystem | |
{ | |
private PaymentSystem[] _paymentSystems; | |
public PaymentHandler(PaymentSystem[] paymentSystems) => | |
_paymentSystems = paymentSystems; | |
public PaymentSystem[] PaymentSystems => | |
_paymentSystems; | |
public void RequestPayment(string systemId) | |
{ | |
_paymentSystems = _paymentSystems.Where(paymentSystem => paymentSystem.SystemId != systemId).ToArray(); | |
GetPaymentSystem(systemId).StartTransaction(); | |
} | |
public void ShowPaymentResult(string systemId) | |
{ | |
Console.WriteLine($"Вы оплатили с помощью {systemId}"); | |
GetPaymentSystem(systemId).FinishTransaction(); | |
} | |
private PaymentSystem GetPaymentSystem(string systemId) => | |
_paymentSystems.First(paymentSystem => paymentSystem.SystemId == systemId); | |
} | |
public class PaymentSystem | |
{ | |
private string _systemId; | |
public PaymentSystem(string systemId) => | |
_systemId = systemId; | |
public string SystemId => | |
_systemId; | |
public void StartTransaction() => | |
Console.WriteLine($"Перевод на страницу {_systemId}..."); | |
public void FinishTransaction() | |
{ | |
Console.WriteLine($"Проверка платежа через {_systemId}..."); | |
Console.WriteLine("Оплата прошла успешно!"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment