-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Leonard Sperry
committed
Nov 27, 2021
1 parent
814f0bc
commit 524db5e
Showing
6 changed files
with
83 additions
and
4 deletions.
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
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
File renamed without changes.
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
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,69 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace Samples.SOLID.OCP | ||
{ | ||
enum LineItemType | ||
{ | ||
Product, Service, Tax | ||
} | ||
|
||
class LineItemValidator | ||
{ | ||
public bool IsValid(LineItem lineItem) | ||
{ | ||
switch (lineItem.LineItemType) | ||
{ | ||
case LineItemType.Product: | ||
return ValidateProduct(lineItem); | ||
case LineItemType.Service: | ||
return ValidateService(lineItem); | ||
case LineItemType.Tax: | ||
return ValidateTax(lineItem); | ||
default: | ||
throw new Exception("unrecognized type"); | ||
} | ||
} | ||
|
||
private bool ValidateTax(LineItem lineItem) | ||
{ | ||
return lineItem.Discount == 0; | ||
} | ||
|
||
private bool ValidateService(LineItem lineItem) | ||
{ | ||
return lineItem.Description != null; | ||
} | ||
|
||
private bool ValidateProduct(LineItem lineItem) | ||
{ | ||
return lineItem.Amount >= 0; | ||
} | ||
} | ||
|
||
abstract class LineItem | ||
{ | ||
public LineItemType LineItemType { get; set; } | ||
public decimal Amount { get; set; } | ||
public string Description { get; set; } | ||
public decimal Discount { get; set; } | ||
} | ||
|
||
class ExtendableLineItemValidator | ||
{ | ||
List<Func<LineItem, bool>> _validations = | ||
new List<Func<LineItem, bool>>(); | ||
|
||
void AddValidation(Func<LineItem, bool> validation) | ||
{ | ||
_validations.Add(validation); | ||
} | ||
|
||
bool Validate(LineItem lineItem) | ||
{ | ||
return _validations.All(v => v(lineItem)); | ||
} | ||
} | ||
} |
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