-
Notifications
You must be signed in to change notification settings - Fork 34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proxy Design Pattern #55
Open
JHamide
wants to merge
13
commits into
iCodeNext:main
Choose a base branch
from
JHamide:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
1828e7c
FactoryDesignPatternLerning_StepOne
JHamide 2876c3e
FactoryDesignPatternLerning_StepTwo
JHamide 0bb215a
FactoryDesignPatternLerning_StepOne
JHamide d6fc975
FactoryDesignPatternLerning_StepFour
JHamide 2512077
FactoryDesignPatternLerning_StepFive
JHamide 59aed40
FactoryDesignPatternLerning_StepSex
JHamide 729ea86
FactoryDesignPatternLerning_StepSeven
JHamide e5db27d
FactoryDesignPatternLerning_Step8:
JHamide ffeead0
FactoryDesignPatternLerning_Step9:
JHamide 883f47b
FactoryDesignPatternLerning_Step10:
JHamide 90b8ae6
Add a new behavior like Truck
09b5ea6
Merge branch 'iCodeNext:main' into main
JHamide 95a92cd
Adapter Design Pattern: Step One
JHamide File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
| ||
namespace Examples.Jalilpour | ||
{ | ||
public enum CargoType | ||
{ | ||
Air, | ||
Ship, | ||
Train | ||
} | ||
|
||
public class DeliveryService | ||
{ | ||
public Delivery Create(CargoType cargoType) | ||
{ | ||
if (cargoType == CargoType.Air) | ||
{ | ||
return new AirDeliveryFactory().CreateDelivery(); | ||
} | ||
else if (cargoType == CargoType.Ship) | ||
{ | ||
return new ShipDeliveryFactory().CreateDelivery(); | ||
} | ||
else if (cargoType == CargoType.Train) | ||
{ | ||
return new TrainDeliveryFactory().CreateDelivery(); | ||
} | ||
throw new InvalidOperationException(); | ||
} | ||
} | ||
|
||
public abstract class Delivery | ||
{ | ||
public abstract void DeliverCargo(); | ||
} | ||
|
||
public class AirDelivery : Delivery | ||
{ | ||
public override void DeliverCargo() | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} | ||
|
||
public class TrainDelivery : Delivery | ||
{ | ||
public override void DeliverCargo() | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} | ||
|
||
public class ShipDelivery : Delivery | ||
{ | ||
public ShipDelivery() | ||
{ | ||
|
||
} | ||
public ShipDelivery(string origin, string destination) | ||
{ | ||
|
||
} | ||
public override void DeliverCargo() | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} | ||
|
||
|
||
|
||
public abstract class DeliveryFactory | ||
{ | ||
public abstract Delivery CreateDelivery(); | ||
} | ||
|
||
public class AirDeliveryFactory : DeliveryFactory | ||
{ | ||
private static AirDelivery airDelivery; | ||
public override Delivery CreateDelivery() | ||
{ | ||
if (airDelivery == null) | ||
airDelivery = new AirDelivery(); | ||
return airDelivery; | ||
} | ||
} | ||
|
||
public class ShipDeliveryFactory : DeliveryFactory | ||
{ | ||
public override Delivery CreateDelivery() | ||
{ | ||
return new ShipDelivery("BandarAbbas", "Astarakhan"); | ||
} | ||
} | ||
|
||
public class TrainDeliveryFactory : DeliveryFactory | ||
{ | ||
private static bool hasAlreadyInstance; | ||
public override Delivery CreateDelivery() | ||
{ | ||
if (hasAlreadyInstance) | ||
{ | ||
init(); | ||
return new TrainDelivery(); | ||
} | ||
else | ||
{ | ||
hasAlreadyInstance = true; | ||
return new TrainDelivery(); | ||
} | ||
} | ||
private void init() | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} | ||
|
||
|
||
|
||
/// <summary> | ||
/// 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. | ||
/// </summary> | ||
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() | ||
{ | ||
Truck truck = new(); | ||
truck.DeliverCargoByTruck(); | ||
} | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
|
||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you have to add null object pattern here.