From 1828e7c4f527ca6e9d21dbef2cdca8497dc7a572 Mon Sep 17 00:00:00 2001 From: Hamideh Jalilpour Date: Fri, 29 Nov 2024 12:38:01 +0330 Subject: [PATCH 01/12] FactoryDesignPatternLerning_StepOne --- Examples/Jalilpour/CargoService.cs | 38 ++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 Examples/Jalilpour/CargoService.cs diff --git a/Examples/Jalilpour/CargoService.cs b/Examples/Jalilpour/CargoService.cs new file mode 100644 index 0000000..6160fbf --- /dev/null +++ b/Examples/Jalilpour/CargoService.cs @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Examples.Jalilpour +{ + public class CargoService + { + public object DeliverCargo(string cargoType) + { + if (cargoType == "Air") + return new AirService(); + else if (cargoType == "Ship") + return new TrainService(); + else if (cargoType == "Trin") + return new ShipService(); + else + throw new ArgumentException("Invalid Cargo Type"); + } + } + + public class AirService + { + + } + + public class TrainService + { + + } + + public class ShipService + { + + } +} From 2876c3eeae381f40bdeffe35341420cfa6716b1b Mon Sep 17 00:00:00 2001 From: Hamideh Jalilpour Date: Fri, 29 Nov 2024 12:50:42 +0330 Subject: [PATCH 02/12] FactoryDesignPatternLerning_StepTwo --- Examples/Jalilpour/CargoService.cs | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/Examples/Jalilpour/CargoService.cs b/Examples/Jalilpour/CargoService.cs index 6160fbf..bf6ddc9 100644 --- a/Examples/Jalilpour/CargoService.cs +++ b/Examples/Jalilpour/CargoService.cs @@ -6,32 +6,37 @@ namespace Examples.Jalilpour { - public class CargoService + public class DeliverFactory { - public object DeliverCargo(string cargoType) + public Deliver Create(string cargoType) { if (cargoType == "Air") - return new AirService(); + return new Air(); else if (cargoType == "Ship") - return new TrainService(); + return new Train(); else if (cargoType == "Trin") - return new ShipService(); + return new Ship(); else throw new ArgumentException("Invalid Cargo Type"); } } - public class AirService + public class Deliver { } - public class TrainService + public class Air : Deliver { } - public class ShipService + public class Train : Deliver + { + + } + + public class Ship : Deliver { } From 0bb215ad227f0e1938850c5ac0d9f40ba5defde5 Mon Sep 17 00:00:00 2001 From: Hamideh Jalilpour Date: Fri, 29 Nov 2024 13:05:49 +0330 Subject: [PATCH 03/12] FactoryDesignPatternLerning_StepOne --- .../{CargoService.cs => DeliverFactory.cs} | 24 +++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) rename Examples/Jalilpour/{CargoService.cs => DeliverFactory.cs} (55%) diff --git a/Examples/Jalilpour/CargoService.cs b/Examples/Jalilpour/DeliverFactory.cs similarity index 55% rename from Examples/Jalilpour/CargoService.cs rename to Examples/Jalilpour/DeliverFactory.cs index bf6ddc9..81b878c 100644 --- a/Examples/Jalilpour/CargoService.cs +++ b/Examples/Jalilpour/DeliverFactory.cs @@ -6,15 +6,22 @@ namespace Examples.Jalilpour { + enum CargoType + { + Air, + Ship, + Train + } + public class DeliverFactory { - public Deliver Create(string cargoType) + public Deliver Create(CargoType cargoType) { - if (cargoType == "Air") + if (cargoType == CargoType.Air) return new Air(); - else if (cargoType == "Ship") + else if (cargoType == CargoType.Train) return new Train(); - else if (cargoType == "Trin") + else if (cargoType == CargoType.Ship) return new Ship(); else throw new ArgumentException("Invalid Cargo Type"); @@ -40,4 +47,13 @@ public class Ship : Deliver { } + + public class Program() + { + public void main() + { + DeliverFactory deliverFactory = new(); + var instance = deliverFactory.Create(CargoType.Air); + } + } } From d6fc9758c8edc20b6480978bf3ed0fce2b7fdacb Mon Sep 17 00:00:00 2001 From: Hamideh Jalilpour Date: Fri, 29 Nov 2024 13:40:00 +0330 Subject: [PATCH 04/12] FactoryDesignPatternLerning_StepFour --- .../{DeliverFactory.cs => DeliveryFactory.cs} | 38 ++++++++++--------- 1 file changed, 21 insertions(+), 17 deletions(-) rename Examples/Jalilpour/{DeliverFactory.cs => DeliveryFactory.cs} (50%) diff --git a/Examples/Jalilpour/DeliverFactory.cs b/Examples/Jalilpour/DeliveryFactory.cs similarity index 50% rename from Examples/Jalilpour/DeliverFactory.cs rename to Examples/Jalilpour/DeliveryFactory.cs index 81b878c..6572378 100644 --- a/Examples/Jalilpour/DeliverFactory.cs +++ b/Examples/Jalilpour/DeliveryFactory.cs @@ -1,9 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - + namespace Examples.Jalilpour { enum CargoType @@ -13,9 +8,9 @@ enum CargoType Train } - public class DeliverFactory + public class DeliveryFactory { - public Deliver Create(CargoType cargoType) + public Delivery Create(CargoType cargoType) { if (cargoType == CargoType.Air) return new Air(); @@ -28,31 +23,40 @@ public Deliver Create(CargoType cargoType) } } - public class Deliver + public abstract class Delivery { - + abstract void DeliverCargo(); } - public class Air : Deliver + public class Air : Delivery { - + private override void DeliverCargo() + { + throw new NotImplementedException(); + } } - public class Train : Deliver + public class Train : Delivery { - + private override void DeliverCargo() + { + throw new NotImplementedException(); + } } - public class Ship : Deliver + public class Ship : Delivery { - + private override void DeliverCargo() + { + throw new NotImplementedException(); + } } public class Program() { public void main() { - DeliverFactory deliverFactory = new(); + DeliveryFactory deliverFactory = new(); var instance = deliverFactory.Create(CargoType.Air); } } From 2512077583ad7034e09917181db9868b8c20a00a Mon Sep 17 00:00:00 2001 From: Hamideh Jalilpour Date: Fri, 29 Nov 2024 13:42:17 +0330 Subject: [PATCH 05/12] FactoryDesignPatternLerning_StepFive --- Examples/Jalilpour/DeliveryFactory.cs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Examples/Jalilpour/DeliveryFactory.cs b/Examples/Jalilpour/DeliveryFactory.cs index 6572378..5ddde32 100644 --- a/Examples/Jalilpour/DeliveryFactory.cs +++ b/Examples/Jalilpour/DeliveryFactory.cs @@ -23,30 +23,30 @@ public Delivery Create(CargoType cargoType) } } - public abstract class Delivery + public interface IDelivery { - abstract void DeliverCargo(); + void DeliverCargo(); } - public class Air : Delivery + public class Air : IDelivery { - private override void DeliverCargo() + private void DeliverCargo() { throw new NotImplementedException(); } } - public class Train : Delivery + public class Train : IDelivery { - private override void DeliverCargo() + private void DeliverCargo() { throw new NotImplementedException(); } } - public class Ship : Delivery + public class Ship : IDelivery { - private override void DeliverCargo() + private void DeliverCargo() { throw new NotImplementedException(); } From 59aed405df9090c70fd035ad414f45cb533a0aa0 Mon Sep 17 00:00:00 2001 From: Hamideh Jalilpour Date: Fri, 29 Nov 2024 14:26:04 +0330 Subject: [PATCH 06/12] FactoryDesignPatternLerning_StepSex --- Examples/Jalilpour/DeliveryFactory.cs | 47 ++++++++++++++++++++++----- 1 file changed, 38 insertions(+), 9 deletions(-) diff --git a/Examples/Jalilpour/DeliveryFactory.cs b/Examples/Jalilpour/DeliveryFactory.cs index 5ddde32..1cd8fbf 100644 --- a/Examples/Jalilpour/DeliveryFactory.cs +++ b/Examples/Jalilpour/DeliveryFactory.cs @@ -8,7 +8,7 @@ enum CargoType Train } - public class DeliveryFactory + public class DeliveryService { public Delivery Create(CargoType cargoType) { @@ -23,30 +23,30 @@ public Delivery Create(CargoType cargoType) } } - public interface IDelivery + public abstract class Delivery { - void DeliverCargo(); + public abstract void DeliverCargo(); } - public class Air : IDelivery + public class AirDelivery : Delivery { - private void DeliverCargo() + public override void DeliverCargo() { throw new NotImplementedException(); } } - public class Train : IDelivery + public class TrainDelivery : Delivery { - private void DeliverCargo() + public override void DeliverCargo() { throw new NotImplementedException(); } } - public class Ship : IDelivery + public class ShipDelivery : Delivery { - private void DeliverCargo() + public override void DeliverCargo() { throw new NotImplementedException(); } @@ -60,4 +60,33 @@ public void main() var instance = deliverFactory.Create(CargoType.Air); } } + + public abstract class DeliveryFactory + { + public abstract Delivery CreateDelivery(); + } + + public class AirDeliveryFactory : DeliveryFactory + { + public override Delivery CreateDelivery() + { + + } + } + + public class ShipDeliveryFactory : DeliveryFactory + { + public override Delivery CreateDelivery() + { + + } + } + + public class TrainDeliveryFactory : DeliveryFactory + { + public override Delivery CreateDelivery() + { + + } + } } From 729ea86e14b51c05299d7ca9b27e37d250aa665f Mon Sep 17 00:00:00 2001 From: Hamideh Jalilpour Date: Fri, 29 Nov 2024 14:54:35 +0330 Subject: [PATCH 07/12] FactoryDesignPatternLerning_StepSeven --- Examples/Jalilpour/DeliveryFactory.cs | 39 ++++++++++++++++----------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/Examples/Jalilpour/DeliveryFactory.cs b/Examples/Jalilpour/DeliveryFactory.cs index 1cd8fbf..673b5d4 100644 --- a/Examples/Jalilpour/DeliveryFactory.cs +++ b/Examples/Jalilpour/DeliveryFactory.cs @@ -13,13 +13,18 @@ public class DeliveryService public Delivery Create(CargoType cargoType) { if (cargoType == CargoType.Air) - return new Air(); - else if (cargoType == CargoType.Train) - return new Train(); + { + return new AirDeliveryFactory().CreateDelivery(); + } else if (cargoType == CargoType.Ship) - return new Ship(); - else - throw new ArgumentException("Invalid Cargo Type"); + { + return new ShipDeliveryFactory().CreateDelivery(); + } + else if (cargoType == CargoType.Train) + { + return new TrainDeliveryFactory().CreateDelivery(); + } + throw new InvalidArgumentException(); } } @@ -52,14 +57,6 @@ public override void DeliverCargo() } } - public class Program() - { - public void main() - { - DeliveryFactory deliverFactory = new(); - var instance = deliverFactory.Create(CargoType.Air); - } - } public abstract class DeliveryFactory { @@ -70,7 +67,7 @@ public class AirDeliveryFactory : DeliveryFactory { public override Delivery CreateDelivery() { - + return new AirDelivery(); } } @@ -78,7 +75,7 @@ public class ShipDeliveryFactory : DeliveryFactory { public override Delivery CreateDelivery() { - + return new ShipDelivery(); } } @@ -86,7 +83,17 @@ public class TrainDeliveryFactory : DeliveryFactory { public override Delivery CreateDelivery() { + return new TrainDelivery(); + } + } + public class Program() + { + public void main() + { + DeliveryService deliverFactory = new(); + var instance = deliverFactory.Create(CargoType.Air); + instance.DeliverCargo(); } } } From e5db27d96e39ec0dc9894a141ad29bdb94cb2999 Mon Sep 17 00:00:00 2001 From: Hamideh Jalilpour Date: Fri, 29 Nov 2024 15:21:12 +0330 Subject: [PATCH 08/12] FactoryDesignPatternLerning_Step8: For AIR we should not have more than one object. --- Examples/Jalilpour/DeliveryFactory.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Examples/Jalilpour/DeliveryFactory.cs b/Examples/Jalilpour/DeliveryFactory.cs index 673b5d4..30613cb 100644 --- a/Examples/Jalilpour/DeliveryFactory.cs +++ b/Examples/Jalilpour/DeliveryFactory.cs @@ -65,9 +65,12 @@ public abstract class DeliveryFactory public class AirDeliveryFactory : DeliveryFactory { + private static AirDelivery airDelivery; public override Delivery CreateDelivery() { - return new AirDelivery(); + if (airDelivery == null) + airDelivery = new AirDelivery(); + return airDelivery; } } @@ -87,6 +90,7 @@ public override Delivery CreateDelivery() } } + public class Program() { public void main() From ffeead06748d17ecebc83f35d7b5bcc721285e9c Mon Sep 17 00:00:00 2001 From: Hamideh Jalilpour Date: Fri, 29 Nov 2024 15:27:00 +0330 Subject: [PATCH 09/12] FactoryDesignPatternLerning_Step9: -The ship must be able to take 2 parameters, origin and destination, in its constructor method. --- Examples/Jalilpour/DeliveryFactory.cs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Examples/Jalilpour/DeliveryFactory.cs b/Examples/Jalilpour/DeliveryFactory.cs index 30613cb..1724b1a 100644 --- a/Examples/Jalilpour/DeliveryFactory.cs +++ b/Examples/Jalilpour/DeliveryFactory.cs @@ -51,6 +51,14 @@ public override void DeliverCargo() public class ShipDelivery : Delivery { + public ShipDelivery() + { + + } + public ShipDelivery(string origin,string destination) + { + + } public override void DeliverCargo() { throw new NotImplementedException(); @@ -78,7 +86,7 @@ public class ShipDeliveryFactory : DeliveryFactory { public override Delivery CreateDelivery() { - return new ShipDelivery(); + return new ShipDelivery("BandarAbbas","Astarakhan"); } } From 883f47bf33f955bff8b3a37637e59b3f6654344c Mon Sep 17 00:00:00 2001 From: Hamideh Jalilpour Date: Fri, 29 Nov 2024 15:35:18 +0330 Subject: [PATCH 10/12] FactoryDesignPatternLerning_Step10: - The train must call a method first after the instantiation has occurred. --- Examples/Jalilpour/DeliveryFactory.cs | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/Examples/Jalilpour/DeliveryFactory.cs b/Examples/Jalilpour/DeliveryFactory.cs index 1724b1a..1f130cc 100644 --- a/Examples/Jalilpour/DeliveryFactory.cs +++ b/Examples/Jalilpour/DeliveryFactory.cs @@ -53,9 +53,9 @@ public class ShipDelivery : Delivery { public ShipDelivery() { - + } - public ShipDelivery(string origin,string destination) + public ShipDelivery(string origin, string destination) { } @@ -86,15 +86,29 @@ public class ShipDeliveryFactory : DeliveryFactory { public override Delivery CreateDelivery() { - return new ShipDelivery("BandarAbbas","Astarakhan"); + return new ShipDelivery("BandarAbbas", "Astarakhan"); } } public class TrainDeliveryFactory : DeliveryFactory { + private static bool hasAlreadyInstance; public override Delivery CreateDelivery() { - return new TrainDelivery(); + if (hasAlreadyInstance) + { + init(); + return new TrainDelivery(); + } + else + { + hasAlreadyInstance = true; + return new TrainDelivery(); + } + } + private void init() + { + throw NotImplementedException(); } } From 90b8ae69aed2b41fd2aa11ca6ccde89fc296a757 Mon Sep 17 00:00:00 2001 From: Jalilpour Date: Sun, 1 Dec 2024 10:33:39 +0330 Subject: [PATCH 11/12] Add a new behavior like Truck --- Examples/Jalilpour/DeliveryFactory.cs | 34 ++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/Examples/Jalilpour/DeliveryFactory.cs b/Examples/Jalilpour/DeliveryFactory.cs index 1f130cc..8042a62 100644 --- a/Examples/Jalilpour/DeliveryFactory.cs +++ b/Examples/Jalilpour/DeliveryFactory.cs @@ -1,7 +1,7 @@  namespace Examples.Jalilpour { - enum CargoType + public enum CargoType { Air, Ship, @@ -24,7 +24,7 @@ public Delivery Create(CargoType cargoType) { return new TrainDeliveryFactory().CreateDelivery(); } - throw new InvalidArgumentException(); + throw new InvalidOperationException(); } } @@ -66,6 +66,7 @@ public override void DeliverCargo() } + public abstract class DeliveryFactory { public abstract Delivery CreateDelivery(); @@ -108,18 +109,39 @@ public override Delivery CreateDelivery() } private void init() { - throw NotImplementedException(); + throw new NotImplementedException(); + } + } + + + + /// + /// Imagine it is a library and we make it as a package. so users or customers, + /// can't change your code, but they want to add a new behavior like Truck. so, how you can solve it. + /// + public class Truck : Delivery + { + public override void DeliverCargo() + { + Console.WriteLine("Delivering..."); + } + } + public static class DeliveryFactoryExtentionMethod + { + public static void DeliverCargoByTruck(this Delivery truck) + { + throw new NotImplementedException(); } } + /// public class Program() { public void main() { - DeliveryService deliverFactory = new(); - var instance = deliverFactory.Create(CargoType.Air); - instance.DeliverCargo(); + Truck truck = new(); + truck.DeliverCargoByTruck(); } } } From 95a92cdc55586a1876f3d8e0358e02c49a2efff0 Mon Sep 17 00:00:00 2001 From: Hamideh Jalilpour Date: Sat, 7 Dec 2024 13:28:07 +0330 Subject: [PATCH 12/12] Adapter Design Pattern: Step One --- Payment/Ecommerce.cs | 91 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 Payment/Ecommerce.cs diff --git a/Payment/Ecommerce.cs b/Payment/Ecommerce.cs new file mode 100644 index 0000000..6d8ab24 --- /dev/null +++ b/Payment/Ecommerce.cs @@ -0,0 +1,91 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Security.Cryptography; +using System.Text; +using System.Threading.Tasks; + +namespace Payment +{ + public enum PaymetType + { + CreditCard, + PayPal, + CryptoCurrency + } + public class Ecommerce + { + public void Pay(PaymetType paymetType, decimal amount) + { + switch (paymetType) + { + case PaymetType.CreditCard: + new CreditCardProcessor().Pay(amount); + break; + case PaymetType.PayPal: + new PayPalProcessor().Pay(amount); + break; + case PaymetType.CryptoCurrency: + new CryptoAdapter(new CryptoCurrencyProcessor()).Pay(amount); + break; + default: throw new Exception("NullPaymentProcessor"); + } + } + + + interface IPaymentProcessor + { + public void Pay(decimal amount); + } + + public class CreditCardProcessor : IPaymentProcessor + { + public void Pay(decimal amonut) + { + Console.WriteLine(amonut + (2 * amonut / 100)); + } + } + + public class PayPalProcessor : IPaymentProcessor + { + public void Pay(decimal amonut) + { + throw new NotImplementedException(); + } + } + + + // Supppose this class comes from a third party library + #region CryptoCurrencyProcessor + public interface ICryptoCurrencyProcessor + { + public bool PayByCrypto(decimal amount); + } + public class CryptoCurrencyProcessor : ICryptoCurrencyProcessor + { + public bool PayByCrypto(decimal amonut) + { + throw new NotImplementedException(); + } + } + #endregion + + + // The CryptoAdapter makes the CryptoCurrencyProcessor compatible with the PaymentProcessor interface + class CryptoAdapter : IPaymentProcessor + { + private readonly CryptoCurrencyProcessor _cryptoCurrencyProcessor; + + public CryptoAdapter(CryptoCurrencyProcessor cryptoCurrencyProcessor) + { + _cryptoCurrencyProcessor = cryptoCurrencyProcessor; + } + + public void Pay(decimal amonut) + { + _cryptoCurrencyProcessor.PayByCrypto(amonut); + } + } + + } +}