Copy Code code as follows:
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Namespace Test
{
Class Program
{
static void Main (string[] args)
{
var customer = new Customer
{
IsActive = True,
Latefees = 100M,
Totalrentnumber = 10
};
Console.WriteLine (Customer. Canrent ());
Console.readkey ();
}
}
public interface ispecification<t>
{
<summary>
Whether you can lease
</summary>
BOOL IsSatisfiedBy (T entity);
<summary>
and operation
</summary>
Ispecification<t> and (ispecification<t> other);
<summary>
No action
</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>)
{
Return to New andspecification<t> (this, other);
}
Public ispecification<t> not ()
{
Return to 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)
{
This.leftspecification = leftspecification;
This.rightspecification = rightspecification;
}
public override bool IsSatisfiedBy (T entity)
{
return Leftspecification.issatisfiedby (Entity) && Rightspecification.issatisfiedby (entity);
}
}
<summary>
No action
</summary>
public class Notspecification<t>: compositespecification<t>
{
Private ispecification<t> innerspecification;
Public notspecification (ispecification<t> innerspecification)
{
This.innerspecification = innerspecification;
}
public override bool IsSatisfiedBy (T entity)
{
return!innerspecification.issatisfiedby (entity);
}
}
<summary>
Whether the maximum number of leases is reached
</summary>
public class Hasreachedmaxspecification:compositespecification<customer>
{
public override bool IsSatisfiedBy (Customer entity)
{
return entity. Totalrentnumber > 5;
}
}
<summary>
Whether to activate
</summary>
public class Customeractivespecification:compositespecification<customer>
{
public override bool IsSatisfiedBy (Customer entity)
{
return entity. IsActive;
}
}
<summary>
Whether the fee is owed
</summary>
public class Customerhaslatefeesspecification:compositespecification<customer>
{
public override bool IsSatisfiedBy (Customer entity)
{
return entity. Latefees > 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 users renting DVDs
</summary>
public int Totalrentnumber
{
Get
Set
}
<summary>
Whether the account is active
</summary>
public bool IsActive
{
Get
Set
}
<summary>
Whether the user owes a fee before
</summary>
Public decimal Latefees
{
Get
Set
}
public bool Canrent ()
{
ispecification<customer> canrent = Customerisactive.and (Hasreachedrentalthreshold.not ()). and (Customerhaslatefees.not ());
Return Canrent.issatisfiedby (this);
}
}
}