An example of asp.net combination mode to facilitate learning asp.net friends as a reference
The Code is 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 = 100 M,
Total1_number = 10
};
Console. WriteLine (customer. CanRent ());
Console. ReadKey ();
}
}
Public interface ISpecification
{
///
/// Can I rent it?
///
Bool IsSatisfiedBy (T entity );
///
/// And operations
///
ISpecification And (ISpecification Other );
///
/// No operation
///
ISpecification Not ();
}
///
/// Base class
///
Public abstract class CompositeSpecification : ISpecification
{
Public abstract bool IsSatisfiedBy (T candidate );
Public ISpecification And (ISpecification Other)
{
Return new AndSpecification (This, other );
}
Public ISpecification Not ()
{
Return new NotSpecification (This );
}
}
///
/// And operations
///
Public class AndSpecification : CompositeSpecification
{
Private ISpecification LeftSpecification;
Private ISpecification RightSpecification;
Public AndSpecification (ISpecification LeftSpecification, ISpecification RightSpecification)
{
This. leftSpecification = leftSpecification;
This. rightSpecification = rightSpecification;
}
Public override bool IsSatisfiedBy (T entity)
{
Return leftSpecification. IsSatisfiedBy (entity) & rightSpecification. IsSatisfiedBy (entity );
}
}
///
/// No operation
///
Public class NotSpecification : CompositeSpecification
{
Private ISpecification InnerSpecification;
Public NotSpecification (ISpecification InnerSpecification)
{
This. innerSpecification = innerSpecification;
}
Public override bool IsSatisfiedBy (T entity)
{
Return! InnerSpecification. IsSatisfiedBy (entity );
}
}
///
/// Determines whether the maximum number of required leases is reached
///
Public class HasReachedMaxSpecification: CompositeSpecification
{
Public override bool IsSatisfiedBy (Customer entity)
{
Return entity. totalequalnumber> 5;
}
}
///
/// Activate
///
Public class CustomerActiveSpecification: CompositeSpecification
{
Public override bool IsSatisfiedBy (Customer entity)
{
Return entity. IsActive;
}
}
///
/// Whether the account is in arrears
///
Public class CustomerHasLateFeesSpecification: CompositeSpecification
{
Public override bool IsSatisfiedBy (Customer entity)
{
Return entity. LateFees> 0;
}
}
Public class Customer
{
Private ISpecification HasReachedRentalThreshold;
Private ISpecification CustomerIsActive;
Private ISpecification CustomerHasLateFees;
Public Customer ()
{
HasReachedRentalThreshold = new HasReachedMaxSpecification ();
CustomerIsActive = new CustomerActiveSpecification ();
CustomerHasLateFees = new CustomerHasLateFeesSpecification ();
}
///
/// Number of DVDs rented by the user
///
Public int TotalRentNumber
{
Get;
Set;
}
///
/// Whether the account is activated
///
Public bool IsActive
{
Get;
Set;
}
///
/// Whether the user is in arrears
///
Public decimal LateFees
{
Get;
Set;
}
Public bool CanRent ()
{
ISpecification CanRent = customerIsActive. And (hasReachedRentalThreshold. Not (). And (customerHasLateFees. Not ());
Return canRent. IsSatisfiedBy (this );
}
}
}