An example of asp.net Combination Mode

Source: Internet
Author: User

Copy codeThe 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 <T>
{
/// <Summary>
/// Can I rent it?
/// </Summary>
Bool IsSatisfiedBy (T entity );
/// <Summary>
/// And operations
/// </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 operations
/// </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 operation
/// </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>
/// Determines whether the maximum number of required leases is reached
/// </Summary>
Public class HasReachedMaxSpecification: CompositeSpecification <Customer>
{
Public override bool IsSatisfiedBy (Customer entity)
{
Return entity. totalequalnumber> 5;
}
}
/// <Summary>
/// Activate
/// </Summary>
Public class CustomerActiveSpecification: CompositeSpecification <Customer>
{
Public override bool IsSatisfiedBy (Customer entity)
{
Return entity. IsActive;
}
}
/// <Summary>
/// Whether the account is in arrears
/// </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> mermerhaslatefees;
Public Customer ()
{
HasReachedRentalThreshold = new HasReachedMaxSpecification ();
CustomerIsActive = new CustomerActiveSpecification ();
CustomerHasLateFees = new CustomerHasLateFeesSpecification ();
}
/// <Summary>
/// Number of DVDs rented by the user
/// </Summary>
Public int TotalRentNumber
{
Get;
Set;
}
/// <Summary>
/// Whether the account is activated
/// </Summary>
Public bool IsActive
{
Get;
Set;
}
/// <Summary>
/// Whether the user is in arrears
/// </Summary>
Public decimal LateFees
{
Get;
Set;
}
Public bool CanRent ()
{
ISpecification <Customer> canRent = customerIsActive. And (hasReachedRentalThreshold. Not (). And (mermerhaslatefees. Not ());
Return canRent. IsSatisfiedBy (this );
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.