Skip to content

Instantly share code, notes, and snippets.

@marhoily
Created August 27, 2018 07:12
Show Gist options
  • Save marhoily/d860fbedc5df7ed2a657a877cda8a37b to your computer and use it in GitHub Desktop.
Save marhoily/d860fbedc5df7ed2a657a877cda8a37b to your computer and use it in GitHub Desktop.
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