Asp.net combination mode example _ php instance

Source: Internet
Author: User
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 );
}
}
}

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.