public string Main(Receiver receiver) { PaymentHandler generalHandler = new BankPaymentHandler(); PaymentHandler moneyPaymentHandler = new MoneyPaymentHandler(); PaymentHandler payPalPaymentHandler = new PayPalPaymentHandler(); generalHandler.Successor = moneyPaymentHandler; moneyPaymentHandler.Successor = payPalPaymentHandler; return(generalHandler.Handle(receiver)); }
static void Main(string[] args) { Receiver receiver = new Receiver(false, false, true); PaymentHandler bankPaymentHandler = new BankPaymentHandler(); PaymentHandler moneyPaymentHandler = new MoneyPaymentHandler(); PaymentHandler paypalPaymentHandler = new PayPalPaymentHandler(); bankPaymentHandler.Successor = paypalPaymentHandler; paypalPaymentHandler.Successor = moneyPaymentHandler; bankPaymentHandler.Handle(receiver); }
public static void Display() { Receiver receiver = new Receiver(false, true, true); PaymentHandler bankPaymentHandler = new BankPaymentHandler(); PaymentHandler moneyPaymentHnadler = new MoneyPaymentHandler(); PaymentHandler paypalPaymentHandler = new PayPalPaymentHandler(); bankPaymentHandler.Successor = paypalPaymentHandler; paypalPaymentHandler.Successor = moneyPaymentHnadler; bankPaymentHandler.Handle(receiver); }
private static void Main(string[] args) { // Цепочка обязанностей — это поведенческий паттерн проектирования, который позволяет // передавать запросы последовательно по цепочке обработчиков. Каждый последующий обработчик // решает, может ли он обработать запрос сам и стоит ли передавать запрос дальше по цепи. // Данный класс является Клиентом. Клиент может либо сформировать цепочку обработчиков единожды, // либо перестраивать её динамически, в зависимости от логики программы. Клиент может отправлять // запросы любому из объектов цепочки, не обязательно первому из них. //определяем доступные классы var receiver = new Receiver(false, true, true); PaymentHandler bankPaymentHandler = new BankPaymentHandler(); PaymentHandler moneyPaymentHnadler = new MoneyPaymentHandler(); PaymentHandler paypalPaymentHandler = new PayPalPaymentHandler(); //выстраиваем цепочку bankPaymentHandler.Successor = paypalPaymentHandler; paypalPaymentHandler.Successor = moneyPaymentHnadler; moneyPaymentHnadler.Successor = bankPaymentHandler; bankPaymentHandler.Handle(receiver); }
public static void Main(string[] args) { //// Template Method ClothingStore clothingStore = new ClothingStore(); MobilePhoneStore mobilePhoneStore = new MobilePhoneStore(); clothingStore.Shopping(); mobilePhoneStore.Shopping(); //// Strategy Car auto = new Car(5, "Chevrolet", new PetrolMove()); auto.Move(); auto.Movable = new ElectricMove(); auto.Move(); //// Iterator var replies1 = new List <int>() { 1, 4, 7 }; var replies2 = new List <int>() { 2, 5, 8 }; var replies3 = new List <int>() { 3, 6, 9 }; var mas = new List <List <int> >() { replies1, replies2, replies3 }; var res = mas.Cartesian((x, y) => 10 * x + y); Console.WriteLine(string.Join("\n", res)); //// Builder // содаем объект пекаря Baker baker = new Baker(); // создаем билдер для ржаного хлеба BreadBuilder builder = new RyeBreadBuilder(); // выпекаем Bread ryeBread = baker.Bake(builder); Console.WriteLine(ryeBread.ToString()); // создаем билдер для пшеничного хлеба builder = new WheatBreadBuilder(); Bread wheatBread = baker.Bake(builder); Console.WriteLine(wheatBread.ToString()); //// Factory BreadFactory breadFactory = new RyeBreadFactory("ООО Хлебовал"); BreadF ryeBreadF = breadFactory.Create(); breadFactory = new WheatBreadFactory("Хлеб от Палыча"); BreadF wheatBreadF = breadFactory.Create(); ////ChainOfResponsibility Receiver receiver = new Receiver(false, true, true); PaymentHandler bankPaymentHandler = new BankPaymentHandler(); PaymentHandler moneyPaymentHnadler = new MoneyPaymentHandler(); PaymentHandler paypalPaymentHandler = new PayPalPaymentHandler(); bankPaymentHandler.Successor = paypalPaymentHandler; paypalPaymentHandler.Successor = moneyPaymentHnadler; ////ChainOfResponsibility with Delegate DelegateReceiver delegateReceiver = new DelegateReceiver(false, true, true); Pay pay = new Pay(); pay.Notify += Display; pay.BankPaymentHandler(delegateReceiver); Console.ReadLine(); }
static void Main(string[] args) { // Реализация паттерна Singleton Phone phone = new Phone(); phone.Launch("Android 12.0"); Console.WriteLine(phone.OperatingSystem.Name); phone.OperatingSystem = OperatingSystem.getOperatingSystem("IOS 9.0"); Console.WriteLine(phone.OperatingSystem.Name); Console.ReadKey(); // Реализация паттерна Factory Method Developer developer = new CSharpDeveloper("C# разработчик"); Application applicationCSharp = developer.Create(); developer = new JavaDeveloper("Java разработчик"); Application applicationJava = developer.Create(); developer = new PythonDeveloper("Python разработчик"); Application applicationPython = developer.Create(); Console.ReadKey(); // Реализация паттерна Strategy Controller controller = new Controller(new AdoProductRepository()); controller.Index(); controller._repository = new EFProductRepository(); controller.Index(); controller._repository = new DataClientProductRepository(); controller.Index(); Console.ReadKey(); // Реализация паттерна Template Method var meatPie = new MeatPie(); var cherryPie = new CherryPie(); Console.WriteLine(meatPie); meatPie.Cook(); Console.WriteLine(cherryPie); cherryPie.Cook(); Console.ReadLine(); // Реализация паттерна Chain of responsibility Receiver receiver = new Receiver(false, true, true); PaymentHandler bankPaymentHandler = new BankPaymentHandler(); PaymentHandler moneyPaymentHadler = new MoneyPaymentHandler(); PaymentHandler paypalPaymentHandler = new PayPalPaymentHandler(); bankPaymentHandler.Successor = paypalPaymentHandler; paypalPaymentHandler.Successor = moneyPaymentHadler; bankPaymentHandler.Handle(receiver); Console.ReadLine(); }