Created
August 27, 2018 07:12
-
-
Save marhoily/d860fbedc5df7ed2a657a877cda8a37b to your computer and use it in GitHub Desktop.
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
using FluentAssertions; | |
using Xunit; | |
namespace DesignPatters | |
{ | |
public sealed class Pattern3 | |
{ | |
public abstract class Approver | |
{ | |
protected Approver Next { get; } | |
protected Approver(Approver next = null) { Next = next; } | |
protected abstract bool ApproveCore(decimal amount); | |
public bool ApproveExpense(decimal amount) | |
{ | |
var approver = this; | |
while (approver != null) | |
{ | |
if (!approver.ApproveCore(amount)) | |
return false; | |
approver = approver.Next; | |
} | |
return true; | |
} | |
} | |
public class Director : Approver | |
{ | |
public Director(Approver next = null) : base(next) { } | |
protected override bool ApproveCore(decimal amount) => amount < 10000; | |
} | |
public class President : Approver | |
{ | |
public President(Approver next = null) : base(next) { } | |
protected override bool ApproveCore(decimal amount) => amount < 1000; | |
} | |
public class VicePresident : Approver | |
{ | |
public VicePresident(Approver next = null) : base(next) { } | |
protected override bool ApproveCore(decimal amount) => amount < 100; | |
} | |
[Fact] | |
public void Test() | |
{ | |
var approver = new Director( | |
new President( | |
new VicePresident())); | |
approver.ApproveExpense(10).Should().BeTrue(); | |
approver.ApproveExpense(150).Should().BeFalse(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment