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
public enum UserTypes | |
{ | |
STANDARD, | |
PREMIUM, | |
DIAMOND | |
} |
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
public class User | |
{ | |
public string Name { get; set; } | |
public UserTypes Type { get; set; } | |
} |
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
public abstract class DiscountRate | |
{ | |
protected abstract double PercentOfDiscountRate { get; } | |
public abstract double CalculateDiscount(double value); | |
} |
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
public sealed class Standard : DiscountRate | |
{ | |
protected override double PercentOfDiscountRate { get => 0.0; } | |
public override double CalculateDiscount(double value) | |
{ | |
return value * PercentOfDiscountRate; | |
} | |
} | |
public sealed class Premium : DiscountRate |
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
public class PurchaseOrder | |
{ | |
public double Value { get; set; } | |
public User User { get; set; } | |
private DiscountRate _discountRate { get; set; } | |
public double CalculateOrderPrice() | |
{ | |
if (User.Type == UserTypes.STANDARD) |
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
public class User | |
{ | |
.... | |
public DiscountRate GetDiscountRate() | |
{ | |
if (Type == UserTypes.STANDARD) | |
return new Standard(); | |
else if (Type == UserTypes.PREMIUM) | |
return new Premium(); | |
else if (Type == UserTypes.DIAMOND) |
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
public class UserTypeAttribute : Attribute | |
{ | |
private Type _implementation { get; } | |
public DiscountRate CreateInstance => (DiscountRate) Activator.CreateInstance(_implementation); | |
public UserTypeAttribute(Type implementation) | |
{ | |
_implementation = implementation; | |
} |
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
public enum UserTypes | |
{ | |
[UserTypeAttribute(typeof(Standard))] | |
STANDARD, | |
[UserTypeAttribute(typeof(Premium))] | |
PREMIUM, | |
[UserTypeAttribute(typeof(Diamond))] | |
DIAMOND | |
} |
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
public static class UserTypesExtensionMethods | |
{ | |
public static DiscountRate GetDiscountRateImplementation(this Enum userType) | |
{ | |
.... | |
} | |
} |
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
public static class UserTypesExtensionMethods | |
{ | |
public static DiscountRate GetDiscountRateImplementation(this Enum userType) | |
{ | |
var attribute = typeof(UserTypes).GetFields() | |
.Where(f => f.Name == userType.ToString()) | |
.FirstOrDefault() | |
?.GetCustomAttributes(false).First() as UserTypeAttribute; | |
if (attribute != null) |
OlderNewer