SoFunction
Updated on 2025-03-07

An example of a combination pattern


using System;
using ;
using ;
using ;
namespace Test
{
class Program
{
static void Main(string[] args)
{
var customer = new Customer
{
IsActive = true,
LateFees = 100M,
TotalRentNumber = 10
};
(());
();
}
}
public interface ISpecification<T>
{
/// <summary>
/// Is it possible to rent
/// </summary>
bool IsSatisfiedBy(T entity);
/// <summary>
/// and operation
/// </summary>
ISpecification<T> And(ISpecification<T> other);
/// <summary>
/// No operation
/// </summary>
ISpecification<T> Not();
}
/// <summary>
/// Base class
/// </summary>
public abstract class CompositeSpecification<T> : ISpecification<T>
{
public abstract bool IsSatisfiedBy(T candidate);
public ISpecification<T> And(ISpecification<T> other)
{
return new AndSpecification<T>(this, other);
}
public ISpecification<T> Not()
{
return new NotSpecification<T>(this);
}
}
/// <summary>
/// and operation
/// </summary>
public class AndSpecification<T> : CompositeSpecification<T>
{
private ISpecification<T> leftSpecification;
private ISpecification<T> rightSpecification;
public AndSpecification(ISpecification<T> leftSpecification, ISpecification<T> rightSpecification)
{
= leftSpecification;
= rightSpecification;
}
public override bool IsSatisfiedBy(T entity)
{
return (entity) && (entity);
}
}
///<summary>
///No operation
/// </summary>
public class NotSpecification<T> : CompositeSpecification<T>
{
private ISpecification<T> innerSpecification;
public NotSpecification(ISpecification<T> innerSpecification)
{
= innerSpecification;
}
public override bool IsSatisfiedBy(T entity)
{
return !(entity);
}
}
/// <summary>
/// Whether the maximum required number of leases is reached
/// </summary>
public class HasReachedMaxSpecification : CompositeSpecification<Customer>
{
public override bool IsSatisfiedBy(Customer entity)
{
return > 5;
}
}
/// <summary>
/// Whether to activate
/// </summary>
public class CustomerActiveSpecification : CompositeSpecification<Customer>
{
public override bool IsSatisfiedBy(Customer entity)
{
return ;
}
}
/// <summary>
/// Is it arrears?
/// </summary>
public class CustomerHasLateFeesSpecification : CompositeSpecification<Customer>
{
public override bool IsSatisfiedBy(Customer entity)
{
return > 0;
}
}
public class Customer
{
private ISpecification<Customer> hasReachedRentalThreshold;
private ISpecification<Customer> customerIsActive;
private ISpecification<Customer> customerHasLateFees;
public Customer()
{
hasReachedRentalThreshold = new HasReachedMaxSpecification();
customerIsActive = new CustomerActiveSpecification();
customerHasLateFees = new CustomerHasLateFeesSpecification();
}
/// <summary>
/// Number of DVDs leased by users
/// </summary>
public int TotalRentNumber
{
get;
set;
}
/// <summary>
/// Whether the account is activated
/// </summary>
public bool IsActive
{
get;
set;
}
/// <summary>
/// Is the user still owed the fees before
/// </summary>
public decimal LateFees
{
get;
set;
}
public bool CanRent()
{
ISpecification<Customer> canRent = (()).And(());
return (this);
}
}
}