...线上博彩公司平台-河南2019年高考会变化吗

首页

AD联系:507867812

...线上博彩公司平台

时间:2019-11-19 09:20:49 作者:葡京国际娱乐老品质 浏览量:92953

...线上博彩公司平台asp.net 组合模式的一个例子复制代码 代码如下: 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> /// 是否可以租赁 /// </summary> bool IsSatisfiedBy(T entity); /// <summary> /// 与操作 /// </summary> ISpecification<T> And(ISpecification<T> other); /// <summary> /// 否操作 /// </summary> ISpecification<T> Not(); } /// <summary> /// 基类 /// </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> /// 与操作 /// </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> ///否操作 /// </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> /// 是否达到最大的规定租赁数 /// </summary> public class HasReachedMaxSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.TotalRentNumber > 5; } } /// <summary> /// 是否激活 /// </summary> public class CustomerActiveSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.IsActive; } } /// <summary> /// 是否欠费 /// </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> /// 用户租赁DVD数量 /// </summary> public int TotalRentNumber { get; set; } /// <summary> /// 账户是否激活 /// </summary> public bool IsActive { get; set; } /// <summary> /// 用户之前是否还欠费 /// </summary> public decimal LateFees { get; set; } public bool CanRent() { ISpecification<Customer> canRent = customerIsActive.And(hasReachedRentalThreshold.Not()).And(customerHasLateFees.Not()); return canRent.IsSatisfiedBy(this); } } }复制代码 代码如下: 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> /// 是否可以租赁 /// </summary> bool IsSatisfiedBy(T entity); /// <summary> /// 与操作 /// </summary> ISpecification<T> And(ISpecification<T> other); /// <summary> /// 否操作 /// </summary> ISpecification<T> Not(); } /// <summary> /// 基类 /// </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> /// 与操作 /// </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> ///否操作 /// </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> /// 是否达到最大的规定租赁数 /// </summary> public class HasReachedMaxSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.TotalRentNumber > 5; } } /// <summary> /// 是否激活 /// </summary> public class CustomerActiveSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.IsActive; } } /// <summary> /// 是否欠费 /// </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> /// 用户租赁DVD数量 /// </summary> public int TotalRentNumber { get; set; } /// <summary> /// 账户是否激活 /// </summary> public bool IsActive { get; set; } /// <summary> /// 用户之前是否还欠费 /// </summary> public decimal LateFees { get; set; } public bool CanRent() { ISpecification<Customer> canRent = customerIsActive.And(hasReachedRentalThreshold.Not()).And(customerHasLateFees.Not()); return canRent.IsSatisfiedBy(this); } } }复制代码 代码如下: 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> /// 是否可以租赁 /// </summary> bool IsSatisfiedBy(T entity); /// <summary> /// 与操作 /// </summary> ISpecification<T> And(ISpecification<T> other); /// <summary> /// 否操作 /// </summary> ISpecification<T> Not(); } /// <summary> /// 基类 /// </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> /// 与操作 /// </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> ///否操作 /// </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> /// 是否达到最大的规定租赁数 /// </summary> public class HasReachedMaxSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.TotalRentNumber > 5; } } /// <summary> /// 是否激活 /// </summary> public class CustomerActiveSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.IsActive; } } /// <summary> /// 是否欠费 /// </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> /// 用户租赁DVD数量 /// </summary> public int TotalRentNumber { get; set; } /// <summary> /// 账户是否激活 /// </summary> public bool IsActive { get; set; } /// <summary> /// 用户之前是否还欠费 /// </summary> public decimal LateFees { get; set; } public bool CanRent() { ISpecification<Customer> canRent = customerIsActive.And(hasReachedRentalThreshold.Not()).And(customerHasLateFees.Not()); return canRent.IsSatisfiedBy(this); } } }复制代码 代码如下: 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> /// 是否可以租赁 /// </summary> bool IsSatisfiedBy(T entity); /// <summary> /// 与操作 /// </summary> ISpecification<T> And(ISpecification<T> other); /// <summary> /// 否操作 /// </summary> ISpecification<T> Not(); } /// <summary> /// 基类 /// </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> /// 与操作 /// </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> ///否操作 /// </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> /// 是否达到最大的规定租赁数 /// </summary> public class HasReachedMaxSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.TotalRentNumber > 5; } } /// <summary> /// 是否激活 /// </summary> public class CustomerActiveSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.IsActive; } } /// <summary> /// 是否欠费 /// </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> /// 用户租赁DVD数量 /// </summary> public int TotalRentNumber { get; set; } /// <summary> /// 账户是否激活 /// </summary> public bool IsActive { get; set; } /// <summary> /// 用户之前是否还欠费 /// </summary> public decimal LateFees { get; set; } public bool CanRent() { ISpecification<Customer> canRent = customerIsActive.And(hasReachedRentalThreshold.Not()).And(customerHasLateFees.Not()); return canRent.IsSatisfiedBy(this); } } }复制代码 代码如下: 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> /// 是否可以租赁 /// </summary> bool IsSatisfiedBy(T entity); /// <summary> /// 与操作 /// </summary> ISpecification<T> And(ISpecification<T> other); /// <summary> /// 否操作 /// </summary> ISpecification<T> Not(); } /// <summary> /// 基类 /// </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> /// 与操作 /// </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> ///否操作 /// </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> /// 是否达到最大的规定租赁数 /// </summary> public class HasReachedMaxSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.TotalRentNumber > 5; } } /// <summary> /// 是否激活 /// </summary> public class CustomerActiveSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.IsActive; } } /// <summary> /// 是否欠费 /// </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> /// 用户租赁DVD数量 /// </summary> public int TotalRentNumber { get; set; } /// <summary> /// 账户是否激活 /// </summary> public bool IsActive { get; set; } /// <summary> /// 用户之前是否还欠费 /// </summary> public decimal LateFees { get; set; } public bool CanRent() { ISpecification<Customer> canRent = customerIsActive.And(hasReachedRentalThreshold.Not()).And(customerHasLateFees.Not()); return canRent.IsSatisfiedBy(this); } } }复制代码 代码如下: 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> /// 是否可以租赁 /// </summary> bool IsSatisfiedBy(T entity); /// <summary> /// 与操作 /// </summary> ISpecification<T> And(ISpecification<T> other); /// <summary> /// 否操作 /// </summary> ISpecification<T> Not(); } /// <summary> /// 基类 /// </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> /// 与操作 /// </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> ///否操作 /// </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> /// 是否达到最大的规定租赁数 /// </summary> public class HasReachedMaxSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.TotalRentNumber > 5; } } /// <summary> /// 是否激活 /// </summary> public class CustomerActiveSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.IsActive; } } /// <summary> /// 是否欠费 /// </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> /// 用户租赁DVD数量 /// </summary> public int TotalRentNumber { get; set; } /// <summary> /// 账户是否激活 /// </summary> public bool IsActive { get; set; } /// <summary> /// 用户之前是否还欠费 /// </summary> public decimal LateFees { get; set; } public bool CanRent() { ISpecification<Customer> canRent = customerIsActive.And(hasReachedRentalThreshold.Not()).And(customerHasLateFees.Not()); return canRent.IsSatisfiedBy(this); } } },见下图

复制代码 代码如下: 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> /// 是否可以租赁 /// </summary> bool IsSatisfiedBy(T entity); /// <summary> /// 与操作 /// </summary> ISpecification<T> And(ISpecification<T> other); /// <summary> /// 否操作 /// </summary> ISpecification<T> Not(); } /// <summary> /// 基类 /// </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> /// 与操作 /// </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> ///否操作 /// </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> /// 是否达到最大的规定租赁数 /// </summary> public class HasReachedMaxSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.TotalRentNumber > 5; } } /// <summary> /// 是否激活 /// </summary> public class CustomerActiveSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.IsActive; } } /// <summary> /// 是否欠费 /// </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> /// 用户租赁DVD数量 /// </summary> public int TotalRentNumber { get; set; } /// <summary> /// 账户是否激活 /// </summary> public bool IsActive { get; set; } /// <summary> /// 用户之前是否还欠费 /// </summary> public decimal LateFees { get; set; } public bool CanRent() { ISpecification<Customer> canRent = customerIsActive.And(hasReachedRentalThreshold.Not()).And(customerHasLateFees.Not()); return canRent.IsSatisfiedBy(this); } } }

复制代码 代码如下: 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> /// 是否可以租赁 /// </summary> bool IsSatisfiedBy(T entity); /// <summary> /// 与操作 /// </summary> ISpecification<T> And(ISpecification<T> other); /// <summary> /// 否操作 /// </summary> ISpecification<T> Not(); } /// <summary> /// 基类 /// </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> /// 与操作 /// </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> ///否操作 /// </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> /// 是否达到最大的规定租赁数 /// </summary> public class HasReachedMaxSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.TotalRentNumber > 5; } } /// <summary> /// 是否激活 /// </summary> public class CustomerActiveSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.IsActive; } } /// <summary> /// 是否欠费 /// </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> /// 用户租赁DVD数量 /// </summary> public int TotalRentNumber { get; set; } /// <summary> /// 账户是否激活 /// </summary> public bool IsActive { get; set; } /// <summary> /// 用户之前是否还欠费 /// </summary> public decimal LateFees { get; set; } public bool CanRent() { ISpecification<Customer> canRent = customerIsActive.And(hasReachedRentalThreshold.Not()).And(customerHasLateFees.Not()); return canRent.IsSatisfiedBy(this); } } }asp.net 组合模式的一个例子,见下图

asp.net 组合模式的一个例子asp.net 组合模式的一个例子asp.net 组合模式的一个例子asp.net 组合模式的一个例子,如下图

复制代码 代码如下: 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> /// 是否可以租赁 /// </summary> bool IsSatisfiedBy(T entity); /// <summary> /// 与操作 /// </summary> ISpecification<T> And(ISpecification<T> other); /// <summary> /// 否操作 /// </summary> ISpecification<T> Not(); } /// <summary> /// 基类 /// </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> /// 与操作 /// </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> ///否操作 /// </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> /// 是否达到最大的规定租赁数 /// </summary> public class HasReachedMaxSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.TotalRentNumber > 5; } } /// <summary> /// 是否激活 /// </summary> public class CustomerActiveSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.IsActive; } } /// <summary> /// 是否欠费 /// </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> /// 用户租赁DVD数量 /// </summary> public int TotalRentNumber { get; set; } /// <summary> /// 账户是否激活 /// </summary> public bool IsActive { get; set; } /// <summary> /// 用户之前是否还欠费 /// </summary> public decimal LateFees { get; set; } public bool CanRent() { ISpecification<Customer> canRent = customerIsActive.And(hasReachedRentalThreshold.Not()).And(customerHasLateFees.Not()); return canRent.IsSatisfiedBy(this); } } }asp.net 组合模式的一个例子

复制代码 代码如下: 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> /// 是否可以租赁 /// </summary> bool IsSatisfiedBy(T entity); /// <summary> /// 与操作 /// </summary> ISpecification<T> And(ISpecification<T> other); /// <summary> /// 否操作 /// </summary> ISpecification<T> Not(); } /// <summary> /// 基类 /// </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> /// 与操作 /// </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> ///否操作 /// </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> /// 是否达到最大的规定租赁数 /// </summary> public class HasReachedMaxSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.TotalRentNumber > 5; } } /// <summary> /// 是否激活 /// </summary> public class CustomerActiveSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.IsActive; } } /// <summary> /// 是否欠费 /// </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> /// 用户租赁DVD数量 /// </summary> public int TotalRentNumber { get; set; } /// <summary> /// 账户是否激活 /// </summary> public bool IsActive { get; set; } /// <summary> /// 用户之前是否还欠费 /// </summary> public decimal LateFees { get; set; } public bool CanRent() { ISpecification<Customer> canRent = customerIsActive.And(hasReachedRentalThreshold.Not()).And(customerHasLateFees.Not()); return canRent.IsSatisfiedBy(this); } } }

如下图

asp.net 组合模式的一个例子,如下图

复制代码 代码如下: 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> /// 是否可以租赁 /// </summary> bool IsSatisfiedBy(T entity); /// <summary> /// 与操作 /// </summary> ISpecification<T> And(ISpecification<T> other); /// <summary> /// 否操作 /// </summary> ISpecification<T> Not(); } /// <summary> /// 基类 /// </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> /// 与操作 /// </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> ///否操作 /// </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> /// 是否达到最大的规定租赁数 /// </summary> public class HasReachedMaxSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.TotalRentNumber > 5; } } /// <summary> /// 是否激活 /// </summary> public class CustomerActiveSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.IsActive; } } /// <summary> /// 是否欠费 /// </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> /// 用户租赁DVD数量 /// </summary> public int TotalRentNumber { get; set; } /// <summary> /// 账户是否激活 /// </summary> public bool IsActive { get; set; } /// <summary> /// 用户之前是否还欠费 /// </summary> public decimal LateFees { get; set; } public bool CanRent() { ISpecification<Customer> canRent = customerIsActive.And(hasReachedRentalThreshold.Not()).And(customerHasLateFees.Not()); return canRent.IsSatisfiedBy(this); } } }asp.net 组合模式的一个例子,见图

...线上博彩公司平台asp.net 组合模式的一个例子复制代码 代码如下: 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> /// 是否可以租赁 /// </summary> bool IsSatisfiedBy(T entity); /// <summary> /// 与操作 /// </summary> ISpecification<T> And(ISpecification<T> other); /// <summary> /// 否操作 /// </summary> ISpecification<T> Not(); } /// <summary> /// 基类 /// </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> /// 与操作 /// </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> ///否操作 /// </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> /// 是否达到最大的规定租赁数 /// </summary> public class HasReachedMaxSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.TotalRentNumber > 5; } } /// <summary> /// 是否激活 /// </summary> public class CustomerActiveSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.IsActive; } } /// <summary> /// 是否欠费 /// </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> /// 用户租赁DVD数量 /// </summary> public int TotalRentNumber { get; set; } /// <summary> /// 账户是否激活 /// </summary> public bool IsActive { get; set; } /// <summary> /// 用户之前是否还欠费 /// </summary> public decimal LateFees { get; set; } public bool CanRent() { ISpecification<Customer> canRent = customerIsActive.And(hasReachedRentalThreshold.Not()).And(customerHasLateFees.Not()); return canRent.IsSatisfiedBy(this); } } }复制代码 代码如下: 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> /// 是否可以租赁 /// </summary> bool IsSatisfiedBy(T entity); /// <summary> /// 与操作 /// </summary> ISpecification<T> And(ISpecification<T> other); /// <summary> /// 否操作 /// </summary> ISpecification<T> Not(); } /// <summary> /// 基类 /// </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> /// 与操作 /// </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> ///否操作 /// </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> /// 是否达到最大的规定租赁数 /// </summary> public class HasReachedMaxSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.TotalRentNumber > 5; } } /// <summary> /// 是否激活 /// </summary> public class CustomerActiveSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.IsActive; } } /// <summary> /// 是否欠费 /// </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> /// 用户租赁DVD数量 /// </summary> public int TotalRentNumber { get; set; } /// <summary> /// 账户是否激活 /// </summary> public bool IsActive { get; set; } /// <summary> /// 用户之前是否还欠费 /// </summary> public decimal LateFees { get; set; } public bool CanRent() { ISpecification<Customer> canRent = customerIsActive.And(hasReachedRentalThreshold.Not()).And(customerHasLateFees.Not()); return canRent.IsSatisfiedBy(this); } } }

asp.net 组合模式的一个例子

复制代码 代码如下: 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> /// 是否可以租赁 /// </summary> bool IsSatisfiedBy(T entity); /// <summary> /// 与操作 /// </summary> ISpecification<T> And(ISpecification<T> other); /// <summary> /// 否操作 /// </summary> ISpecification<T> Not(); } /// <summary> /// 基类 /// </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> /// 与操作 /// </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> ///否操作 /// </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> /// 是否达到最大的规定租赁数 /// </summary> public class HasReachedMaxSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.TotalRentNumber > 5; } } /// <summary> /// 是否激活 /// </summary> public class CustomerActiveSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.IsActive; } } /// <summary> /// 是否欠费 /// </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> /// 用户租赁DVD数量 /// </summary> public int TotalRentNumber { get; set; } /// <summary> /// 账户是否激活 /// </summary> public bool IsActive { get; set; } /// <summary> /// 用户之前是否还欠费 /// </summary> public decimal LateFees { get; set; } public bool CanRent() { ISpecification<Customer> canRent = customerIsActive.And(hasReachedRentalThreshold.Not()).And(customerHasLateFees.Not()); return canRent.IsSatisfiedBy(this); } } }复制代码 代码如下: 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> /// 是否可以租赁 /// </summary> bool IsSatisfiedBy(T entity); /// <summary> /// 与操作 /// </summary> ISpecification<T> And(ISpecification<T> other); /// <summary> /// 否操作 /// </summary> ISpecification<T> Not(); } /// <summary> /// 基类 /// </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> /// 与操作 /// </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> ///否操作 /// </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> /// 是否达到最大的规定租赁数 /// </summary> public class HasReachedMaxSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.TotalRentNumber > 5; } } /// <summary> /// 是否激活 /// </summary> public class CustomerActiveSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.IsActive; } } /// <summary> /// 是否欠费 /// </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> /// 用户租赁DVD数量 /// </summary> public int TotalRentNumber { get; set; } /// <summary> /// 账户是否激活 /// </summary> public bool IsActive { get; set; } /// <summary> /// 用户之前是否还欠费 /// </summary> public decimal LateFees { get; set; } public bool CanRent() { ISpecification<Customer> canRent = customerIsActive.And(hasReachedRentalThreshold.Not()).And(customerHasLateFees.Not()); return canRent.IsSatisfiedBy(this); } } }asp.net 组合模式的一个例子复制代码 代码如下: 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> /// 是否可以租赁 /// </summary> bool IsSatisfiedBy(T entity); /// <summary> /// 与操作 /// </summary> ISpecification<T> And(ISpecification<T> other); /// <summary> /// 否操作 /// </summary> ISpecification<T> Not(); } /// <summary> /// 基类 /// </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> /// 与操作 /// </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> ///否操作 /// </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> /// 是否达到最大的规定租赁数 /// </summary> public class HasReachedMaxSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.TotalRentNumber > 5; } } /// <summary> /// 是否激活 /// </summary> public class CustomerActiveSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.IsActive; } } /// <summary> /// 是否欠费 /// </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> /// 用户租赁DVD数量 /// </summary> public int TotalRentNumber { get; set; } /// <summary> /// 账户是否激活 /// </summary> public bool IsActive { get; set; } /// <summary> /// 用户之前是否还欠费 /// </summary> public decimal LateFees { get; set; } public bool CanRent() { ISpecification<Customer> canRent = customerIsActive.And(hasReachedRentalThreshold.Not()).And(customerHasLateFees.Not()); return canRent.IsSatisfiedBy(this); } } }asp.net 组合模式的一个例子asp.net 组合模式的一个例子

复制代码 代码如下: 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> /// 是否可以租赁 /// </summary> bool IsSatisfiedBy(T entity); /// <summary> /// 与操作 /// </summary> ISpecification<T> And(ISpecification<T> other); /// <summary> /// 否操作 /// </summary> ISpecification<T> Not(); } /// <summary> /// 基类 /// </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> /// 与操作 /// </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> ///否操作 /// </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> /// 是否达到最大的规定租赁数 /// </summary> public class HasReachedMaxSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.TotalRentNumber > 5; } } /// <summary> /// 是否激活 /// </summary> public class CustomerActiveSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.IsActive; } } /// <summary> /// 是否欠费 /// </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> /// 用户租赁DVD数量 /// </summary> public int TotalRentNumber { get; set; } /// <summary> /// 账户是否激活 /// </summary> public bool IsActive { get; set; } /// <summary> /// 用户之前是否还欠费 /// </summary> public decimal LateFees { get; set; } public bool CanRent() { ISpecification<Customer> canRent = customerIsActive.And(hasReachedRentalThreshold.Not()).And(customerHasLateFees.Not()); return canRent.IsSatisfiedBy(this); } } }

asp.net 组合模式的一个例子

asp.net 组合模式的一个例子asp.net 组合模式的一个例子复制代码 代码如下: 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> /// 是否可以租赁 /// </summary> bool IsSatisfiedBy(T entity); /// <summary> /// 与操作 /// </summary> ISpecification<T> And(ISpecification<T> other); /// <summary> /// 否操作 /// </summary> ISpecification<T> Not(); } /// <summary> /// 基类 /// </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> /// 与操作 /// </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> ///否操作 /// </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> /// 是否达到最大的规定租赁数 /// </summary> public class HasReachedMaxSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.TotalRentNumber > 5; } } /// <summary> /// 是否激活 /// </summary> public class CustomerActiveSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.IsActive; } } /// <summary> /// 是否欠费 /// </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> /// 用户租赁DVD数量 /// </summary> public int TotalRentNumber { get; set; } /// <summary> /// 账户是否激活 /// </summary> public bool IsActive { get; set; } /// <summary> /// 用户之前是否还欠费 /// </summary> public decimal LateFees { get; set; } public bool CanRent() { ISpecification<Customer> canRent = customerIsActive.And(hasReachedRentalThreshold.Not()).And(customerHasLateFees.Not()); return canRent.IsSatisfiedBy(this); } } }复制代码 代码如下: 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> /// 是否可以租赁 /// </summary> bool IsSatisfiedBy(T entity); /// <summary> /// 与操作 /// </summary> ISpecification<T> And(ISpecification<T> other); /// <summary> /// 否操作 /// </summary> ISpecification<T> Not(); } /// <summary> /// 基类 /// </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> /// 与操作 /// </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> ///否操作 /// </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> /// 是否达到最大的规定租赁数 /// </summary> public class HasReachedMaxSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.TotalRentNumber > 5; } } /// <summary> /// 是否激活 /// </summary> public class CustomerActiveSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.IsActive; } } /// <summary> /// 是否欠费 /// </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> /// 用户租赁DVD数量 /// </summary> public int TotalRentNumber { get; set; } /// <summary> /// 账户是否激活 /// </summary> public bool IsActive { get; set; } /// <summary> /// 用户之前是否还欠费 /// </summary> public decimal LateFees { get; set; } public bool CanRent() { ISpecification<Customer> canRent = customerIsActive.And(hasReachedRentalThreshold.Not()).And(customerHasLateFees.Not()); return canRent.IsSatisfiedBy(this); } } }复制代码 代码如下: 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> /// 是否可以租赁 /// </summary> bool IsSatisfiedBy(T entity); /// <summary> /// 与操作 /// </summary> ISpecification<T> And(ISpecification<T> other); /// <summary> /// 否操作 /// </summary> ISpecification<T> Not(); } /// <summary> /// 基类 /// </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> /// 与操作 /// </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> ///否操作 /// </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> /// 是否达到最大的规定租赁数 /// </summary> public class HasReachedMaxSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.TotalRentNumber > 5; } } /// <summary> /// 是否激活 /// </summary> public class CustomerActiveSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.IsActive; } } /// <summary> /// 是否欠费 /// </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> /// 用户租赁DVD数量 /// </summary> public int TotalRentNumber { get; set; } /// <summary> /// 账户是否激活 /// </summary> public bool IsActive { get; set; } /// <summary> /// 用户之前是否还欠费 /// </summary> public decimal LateFees { get; set; } public bool CanRent() { ISpecification<Customer> canRent = customerIsActive.And(hasReachedRentalThreshold.Not()).And(customerHasLateFees.Not()); return canRent.IsSatisfiedBy(this); } } }asp.net 组合模式的一个例子asp.net 组合模式的一个例子复制代码 代码如下: 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> /// 是否可以租赁 /// </summary> bool IsSatisfiedBy(T entity); /// <summary> /// 与操作 /// </summary> ISpecification<T> And(ISpecification<T> other); /// <summary> /// 否操作 /// </summary> ISpecification<T> Not(); } /// <summary> /// 基类 /// </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> /// 与操作 /// </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> ///否操作 /// </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> /// 是否达到最大的规定租赁数 /// </summary> public class HasReachedMaxSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.TotalRentNumber > 5; } } /// <summary> /// 是否激活 /// </summary> public class CustomerActiveSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.IsActive; } } /// <summary> /// 是否欠费 /// </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> /// 用户租赁DVD数量 /// </summary> public int TotalRentNumber { get; set; } /// <summary> /// 账户是否激活 /// </summary> public bool IsActive { get; set; } /// <summary> /// 用户之前是否还欠费 /// </summary> public decimal LateFees { get; set; } public bool CanRent() { ISpecification<Customer> canRent = customerIsActive.And(hasReachedRentalThreshold.Not()).And(customerHasLateFees.Not()); return canRent.IsSatisfiedBy(this); } } }复制代码 代码如下: 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> /// 是否可以租赁 /// </summary> bool IsSatisfiedBy(T entity); /// <summary> /// 与操作 /// </summary> ISpecification<T> And(ISpecification<T> other); /// <summary> /// 否操作 /// </summary> ISpecification<T> Not(); } /// <summary> /// 基类 /// </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> /// 与操作 /// </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> ///否操作 /// </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> /// 是否达到最大的规定租赁数 /// </summary> public class HasReachedMaxSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.TotalRentNumber > 5; } } /// <summary> /// 是否激活 /// </summary> public class CustomerActiveSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.IsActive; } } /// <summary> /// 是否欠费 /// </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> /// 用户租赁DVD数量 /// </summary> public int TotalRentNumber { get; set; } /// <summary> /// 账户是否激活 /// </summary> public bool IsActive { get; set; } /// <summary> /// 用户之前是否还欠费 /// </summary> public decimal LateFees { get; set; } public bool CanRent() { ISpecification<Customer> canRent = customerIsActive.And(hasReachedRentalThreshold.Not()).And(customerHasLateFees.Not()); return canRent.IsSatisfiedBy(this); } } }复制代码 代码如下: 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> /// 是否可以租赁 /// </summary> bool IsSatisfiedBy(T entity); /// <summary> /// 与操作 /// </summary> ISpecification<T> And(ISpecification<T> other); /// <summary> /// 否操作 /// </summary> ISpecification<T> Not(); } /// <summary> /// 基类 /// </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> /// 与操作 /// </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> ///否操作 /// </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> /// 是否达到最大的规定租赁数 /// </summary> public class HasReachedMaxSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.TotalRentNumber > 5; } } /// <summary> /// 是否激活 /// </summary> public class CustomerActiveSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.IsActive; } } /// <summary> /// 是否欠费 /// </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> /// 用户租赁DVD数量 /// </summary> public int TotalRentNumber { get; set; } /// <summary> /// 账户是否激活 /// </summary> public bool IsActive { get; set; } /// <summary> /// 用户之前是否还欠费 /// </summary> public decimal LateFees { get; set; } public bool CanRent() { ISpecification<Customer> canRent = customerIsActive.And(hasReachedRentalThreshold.Not()).And(customerHasLateFees.Not()); return canRent.IsSatisfiedBy(this); } } }asp.net 组合模式的一个例子复制代码 代码如下: 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> /// 是否可以租赁 /// </summary> bool IsSatisfiedBy(T entity); /// <summary> /// 与操作 /// </summary> ISpecification<T> And(ISpecification<T> other); /// <summary> /// 否操作 /// </summary> ISpecification<T> Not(); } /// <summary> /// 基类 /// </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> /// 与操作 /// </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> ///否操作 /// </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> /// 是否达到最大的规定租赁数 /// </summary> public class HasReachedMaxSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.TotalRentNumber > 5; } } /// <summary> /// 是否激活 /// </summary> public class CustomerActiveSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.IsActive; } } /// <summary> /// 是否欠费 /// </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> /// 用户租赁DVD数量 /// </summary> public int TotalRentNumber { get; set; } /// <summary> /// 账户是否激活 /// </summary> public bool IsActive { get; set; } /// <summary> /// 用户之前是否还欠费 /// </summary> public decimal LateFees { get; set; } public bool CanRent() { ISpecification<Customer> canRent = customerIsActive.And(hasReachedRentalThreshold.Not()).And(customerHasLateFees.Not()); return canRent.IsSatisfiedBy(this); } } }复制代码 代码如下: 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> /// 是否可以租赁 /// </summary> bool IsSatisfiedBy(T entity); /// <summary> /// 与操作 /// </summary> ISpecification<T> And(ISpecification<T> other); /// <summary> /// 否操作 /// </summary> ISpecification<T> Not(); } /// <summary> /// 基类 /// </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> /// 与操作 /// </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> ///否操作 /// </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> /// 是否达到最大的规定租赁数 /// </summary> public class HasReachedMaxSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.TotalRentNumber > 5; } } /// <summary> /// 是否激活 /// </summary> public class CustomerActiveSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.IsActive; } } /// <summary> /// 是否欠费 /// </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> /// 用户租赁DVD数量 /// </summary> public int TotalRentNumber { get; set; } /// <summary> /// 账户是否激活 /// </summary> public bool IsActive { get; set; } /// <summary> /// 用户之前是否还欠费 /// </summary> public decimal LateFees { get; set; } public bool CanRent() { ISpecification<Customer> canRent = customerIsActive.And(hasReachedRentalThreshold.Not()).And(customerHasLateFees.Not()); return canRent.IsSatisfiedBy(this); } } }。

asp.net 组合模式的一个例子

...线上博彩公司平台复制代码 代码如下: 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> /// 是否可以租赁 /// </summary> bool IsSatisfiedBy(T entity); /// <summary> /// 与操作 /// </summary> ISpecification<T> And(ISpecification<T> other); /// <summary> /// 否操作 /// </summary> ISpecification<T> Not(); } /// <summary> /// 基类 /// </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> /// 与操作 /// </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> ///否操作 /// </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> /// 是否达到最大的规定租赁数 /// </summary> public class HasReachedMaxSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.TotalRentNumber > 5; } } /// <summary> /// 是否激活 /// </summary> public class CustomerActiveSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.IsActive; } } /// <summary> /// 是否欠费 /// </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> /// 用户租赁DVD数量 /// </summary> public int TotalRentNumber { get; set; } /// <summary> /// 账户是否激活 /// </summary> public bool IsActive { get; set; } /// <summary> /// 用户之前是否还欠费 /// </summary> public decimal LateFees { get; set; } public bool CanRent() { ISpecification<Customer> canRent = customerIsActive.And(hasReachedRentalThreshold.Not()).And(customerHasLateFees.Not()); return canRent.IsSatisfiedBy(this); } } }

复制代码 代码如下: 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> /// 是否可以租赁 /// </summary> bool IsSatisfiedBy(T entity); /// <summary> /// 与操作 /// </summary> ISpecification<T> And(ISpecification<T> other); /// <summary> /// 否操作 /// </summary> ISpecification<T> Not(); } /// <summary> /// 基类 /// </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> /// 与操作 /// </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> ///否操作 /// </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> /// 是否达到最大的规定租赁数 /// </summary> public class HasReachedMaxSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.TotalRentNumber > 5; } } /// <summary> /// 是否激活 /// </summary> public class CustomerActiveSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.IsActive; } } /// <summary> /// 是否欠费 /// </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> /// 用户租赁DVD数量 /// </summary> public int TotalRentNumber { get; set; } /// <summary> /// 账户是否激活 /// </summary> public bool IsActive { get; set; } /// <summary> /// 用户之前是否还欠费 /// </summary> public decimal LateFees { get; set; } public bool CanRent() { ISpecification<Customer> canRent = customerIsActive.And(hasReachedRentalThreshold.Not()).And(customerHasLateFees.Not()); return canRent.IsSatisfiedBy(this); } } }asp.net 组合模式的一个例子复制代码 代码如下: 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> /// 是否可以租赁 /// </summary> bool IsSatisfiedBy(T entity); /// <summary> /// 与操作 /// </summary> ISpecification<T> And(ISpecification<T> other); /// <summary> /// 否操作 /// </summary> ISpecification<T> Not(); } /// <summary> /// 基类 /// </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> /// 与操作 /// </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> ///否操作 /// </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> /// 是否达到最大的规定租赁数 /// </summary> public class HasReachedMaxSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.TotalRentNumber > 5; } } /// <summary> /// 是否激活 /// </summary> public class CustomerActiveSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.IsActive; } } /// <summary> /// 是否欠费 /// </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> /// 用户租赁DVD数量 /// </summary> public int TotalRentNumber { get; set; } /// <summary> /// 账户是否激活 /// </summary> public bool IsActive { get; set; } /// <summary> /// 用户之前是否还欠费 /// </summary> public decimal LateFees { get; set; } public bool CanRent() { ISpecification<Customer> canRent = customerIsActive.And(hasReachedRentalThreshold.Not()).And(customerHasLateFees.Not()); return canRent.IsSatisfiedBy(this); } } }asp.net 组合模式的一个例子复制代码 代码如下: 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> /// 是否可以租赁 /// </summary> bool IsSatisfiedBy(T entity); /// <summary> /// 与操作 /// </summary> ISpecification<T> And(ISpecification<T> other); /// <summary> /// 否操作 /// </summary> ISpecification<T> Not(); } /// <summary> /// 基类 /// </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> /// 与操作 /// </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> ///否操作 /// </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> /// 是否达到最大的规定租赁数 /// </summary> public class HasReachedMaxSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.TotalRentNumber > 5; } } /// <summary> /// 是否激活 /// </summary> public class CustomerActiveSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.IsActive; } } /// <summary> /// 是否欠费 /// </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> /// 用户租赁DVD数量 /// </summary> public int TotalRentNumber { get; set; } /// <summary> /// 账户是否激活 /// </summary> public bool IsActive { get; set; } /// <summary> /// 用户之前是否还欠费 /// </summary> public decimal LateFees { get; set; } public bool CanRent() { ISpecification<Customer> canRent = customerIsActive.And(hasReachedRentalThreshold.Not()).And(customerHasLateFees.Not()); return canRent.IsSatisfiedBy(this); } } }复制代码 代码如下: 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> /// 是否可以租赁 /// </summary> bool IsSatisfiedBy(T entity); /// <summary> /// 与操作 /// </summary> ISpecification<T> And(ISpecification<T> other); /// <summary> /// 否操作 /// </summary> ISpecification<T> Not(); } /// <summary> /// 基类 /// </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> /// 与操作 /// </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> ///否操作 /// </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> /// 是否达到最大的规定租赁数 /// </summary> public class HasReachedMaxSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.TotalRentNumber > 5; } } /// <summary> /// 是否激活 /// </summary> public class CustomerActiveSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.IsActive; } } /// <summary> /// 是否欠费 /// </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> /// 用户租赁DVD数量 /// </summary> public int TotalRentNumber { get; set; } /// <summary> /// 账户是否激活 /// </summary> public bool IsActive { get; set; } /// <summary> /// 用户之前是否还欠费 /// </summary> public decimal LateFees { get; set; } public bool CanRent() { ISpecification<Customer> canRent = customerIsActive.And(hasReachedRentalThreshold.Not()).And(customerHasLateFees.Not()); return canRent.IsSatisfiedBy(this); } } }复制代码 代码如下: 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> /// 是否可以租赁 /// </summary> bool IsSatisfiedBy(T entity); /// <summary> /// 与操作 /// </summary> ISpecification<T> And(ISpecification<T> other); /// <summary> /// 否操作 /// </summary> ISpecification<T> Not(); } /// <summary> /// 基类 /// </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> /// 与操作 /// </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> ///否操作 /// </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> /// 是否达到最大的规定租赁数 /// </summary> public class HasReachedMaxSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.TotalRentNumber > 5; } } /// <summary> /// 是否激活 /// </summary> public class CustomerActiveSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.IsActive; } } /// <summary> /// 是否欠费 /// </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> /// 用户租赁DVD数量 /// </summary> public int TotalRentNumber { get; set; } /// <summary> /// 账户是否激活 /// </summary> public bool IsActive { get; set; } /// <summary> /// 用户之前是否还欠费 /// </summary> public decimal LateFees { get; set; } public bool CanRent() { ISpecification<Customer> canRent = customerIsActive.And(hasReachedRentalThreshold.Not()).And(customerHasLateFees.Not()); return canRent.IsSatisfiedBy(this); } } }asp.net 组合模式的一个例子复制代码 代码如下: 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> /// 是否可以租赁 /// </summary> bool IsSatisfiedBy(T entity); /// <summary> /// 与操作 /// </summary> ISpecification<T> And(ISpecification<T> other); /// <summary> /// 否操作 /// </summary> ISpecification<T> Not(); } /// <summary> /// 基类 /// </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> /// 与操作 /// </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> ///否操作 /// </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> /// 是否达到最大的规定租赁数 /// </summary> public class HasReachedMaxSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.TotalRentNumber > 5; } } /// <summary> /// 是否激活 /// </summary> public class CustomerActiveSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.IsActive; } } /// <summary> /// 是否欠费 /// </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> /// 用户租赁DVD数量 /// </summary> public int TotalRentNumber { get; set; } /// <summary> /// 账户是否激活 /// </summary> public bool IsActive { get; set; } /// <summary> /// 用户之前是否还欠费 /// </summary> public decimal LateFees { get; set; } public bool CanRent() { ISpecification<Customer> canRent = customerIsActive.And(hasReachedRentalThreshold.Not()).And(customerHasLateFees.Not()); return canRent.IsSatisfiedBy(this); } } }。

asp.net 组合模式的一个例子

1.asp.net 组合模式的一个例子

复制代码 代码如下: 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> /// 是否可以租赁 /// </summary> bool IsSatisfiedBy(T entity); /// <summary> /// 与操作 /// </summary> ISpecification<T> And(ISpecification<T> other); /// <summary> /// 否操作 /// </summary> ISpecification<T> Not(); } /// <summary> /// 基类 /// </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> /// 与操作 /// </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> ///否操作 /// </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> /// 是否达到最大的规定租赁数 /// </summary> public class HasReachedMaxSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.TotalRentNumber > 5; } } /// <summary> /// 是否激活 /// </summary> public class CustomerActiveSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.IsActive; } } /// <summary> /// 是否欠费 /// </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> /// 用户租赁DVD数量 /// </summary> public int TotalRentNumber { get; set; } /// <summary> /// 账户是否激活 /// </summary> public bool IsActive { get; set; } /// <summary> /// 用户之前是否还欠费 /// </summary> public decimal LateFees { get; set; } public bool CanRent() { ISpecification<Customer> canRent = customerIsActive.And(hasReachedRentalThreshold.Not()).And(customerHasLateFees.Not()); return canRent.IsSatisfiedBy(this); } } }复制代码 代码如下: 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> /// 是否可以租赁 /// </summary> bool IsSatisfiedBy(T entity); /// <summary> /// 与操作 /// </summary> ISpecification<T> And(ISpecification<T> other); /// <summary> /// 否操作 /// </summary> ISpecification<T> Not(); } /// <summary> /// 基类 /// </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> /// 与操作 /// </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> ///否操作 /// </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> /// 是否达到最大的规定租赁数 /// </summary> public class HasReachedMaxSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.TotalRentNumber > 5; } } /// <summary> /// 是否激活 /// </summary> public class CustomerActiveSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.IsActive; } } /// <summary> /// 是否欠费 /// </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> /// 用户租赁DVD数量 /// </summary> public int TotalRentNumber { get; set; } /// <summary> /// 账户是否激活 /// </summary> public bool IsActive { get; set; } /// <summary> /// 用户之前是否还欠费 /// </summary> public decimal LateFees { get; set; } public bool CanRent() { ISpecification<Customer> canRent = customerIsActive.And(hasReachedRentalThreshold.Not()).And(customerHasLateFees.Not()); return canRent.IsSatisfiedBy(this); } } }asp.net 组合模式的一个例子asp.net 组合模式的一个例子复制代码 代码如下: 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> /// 是否可以租赁 /// </summary> bool IsSatisfiedBy(T entity); /// <summary> /// 与操作 /// </summary> ISpecification<T> And(ISpecification<T> other); /// <summary> /// 否操作 /// </summary> ISpecification<T> Not(); } /// <summary> /// 基类 /// </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> /// 与操作 /// </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> ///否操作 /// </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> /// 是否达到最大的规定租赁数 /// </summary> public class HasReachedMaxSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.TotalRentNumber > 5; } } /// <summary> /// 是否激活 /// </summary> public class CustomerActiveSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.IsActive; } } /// <summary> /// 是否欠费 /// </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> /// 用户租赁DVD数量 /// </summary> public int TotalRentNumber { get; set; } /// <summary> /// 账户是否激活 /// </summary> public bool IsActive { get; set; } /// <summary> /// 用户之前是否还欠费 /// </summary> public decimal LateFees { get; set; } public bool CanRent() { ISpecification<Customer> canRent = customerIsActive.And(hasReachedRentalThreshold.Not()).And(customerHasLateFees.Not()); return canRent.IsSatisfiedBy(this); } } }asp.net 组合模式的一个例子asp.net 组合模式的一个例子复制代码 代码如下: 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> /// 是否可以租赁 /// </summary> bool IsSatisfiedBy(T entity); /// <summary> /// 与操作 /// </summary> ISpecification<T> And(ISpecification<T> other); /// <summary> /// 否操作 /// </summary> ISpecification<T> Not(); } /// <summary> /// 基类 /// </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> /// 与操作 /// </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> ///否操作 /// </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> /// 是否达到最大的规定租赁数 /// </summary> public class HasReachedMaxSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.TotalRentNumber > 5; } } /// <summary> /// 是否激活 /// </summary> public class CustomerActiveSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.IsActive; } } /// <summary> /// 是否欠费 /// </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> /// 用户租赁DVD数量 /// </summary> public int TotalRentNumber { get; set; } /// <summary> /// 账户是否激活 /// </summary> public bool IsActive { get; set; } /// <summary> /// 用户之前是否还欠费 /// </summary> public decimal LateFees { get; set; } public bool CanRent() { ISpecification<Customer> canRent = customerIsActive.And(hasReachedRentalThreshold.Not()).And(customerHasLateFees.Not()); return canRent.IsSatisfiedBy(this); } } }asp.net 组合模式的一个例子复制代码 代码如下: 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> /// 是否可以租赁 /// </summary> bool IsSatisfiedBy(T entity); /// <summary> /// 与操作 /// </summary> ISpecification<T> And(ISpecification<T> other); /// <summary> /// 否操作 /// </summary> ISpecification<T> Not(); } /// <summary> /// 基类 /// </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> /// 与操作 /// </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> ///否操作 /// </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> /// 是否达到最大的规定租赁数 /// </summary> public class HasReachedMaxSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.TotalRentNumber > 5; } } /// <summary> /// 是否激活 /// </summary> public class CustomerActiveSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.IsActive; } } /// <summary> /// 是否欠费 /// </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> /// 用户租赁DVD数量 /// </summary> public int TotalRentNumber { get; set; } /// <summary> /// 账户是否激活 /// </summary> public bool IsActive { get; set; } /// <summary> /// 用户之前是否还欠费 /// </summary> public decimal LateFees { get; set; } public bool CanRent() { ISpecification<Customer> canRent = customerIsActive.And(hasReachedRentalThreshold.Not()).And(customerHasLateFees.Not()); return canRent.IsSatisfiedBy(this); } } }asp.net 组合模式的一个例子asp.net 组合模式的一个例子asp.net 组合模式的一个例子复制代码 代码如下: 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> /// 是否可以租赁 /// </summary> bool IsSatisfiedBy(T entity); /// <summary> /// 与操作 /// </summary> ISpecification<T> And(ISpecification<T> other); /// <summary> /// 否操作 /// </summary> ISpecification<T> Not(); } /// <summary> /// 基类 /// </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> /// 与操作 /// </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> ///否操作 /// </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> /// 是否达到最大的规定租赁数 /// </summary> public class HasReachedMaxSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.TotalRentNumber > 5; } } /// <summary> /// 是否激活 /// </summary> public class CustomerActiveSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.IsActive; } } /// <summary> /// 是否欠费 /// </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> /// 用户租赁DVD数量 /// </summary> public int TotalRentNumber { get; set; } /// <summary> /// 账户是否激活 /// </summary> public bool IsActive { get; set; } /// <summary> /// 用户之前是否还欠费 /// </summary> public decimal LateFees { get; set; } public bool CanRent() { ISpecification<Customer> canRent = customerIsActive.And(hasReachedRentalThreshold.Not()).And(customerHasLateFees.Not()); return canRent.IsSatisfiedBy(this); } } }asp.net 组合模式的一个例子

2.复制代码 代码如下: 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> /// 是否可以租赁 /// </summary> bool IsSatisfiedBy(T entity); /// <summary> /// 与操作 /// </summary> ISpecification<T> And(ISpecification<T> other); /// <summary> /// 否操作 /// </summary> ISpecification<T> Not(); } /// <summary> /// 基类 /// </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> /// 与操作 /// </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> ///否操作 /// </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> /// 是否达到最大的规定租赁数 /// </summary> public class HasReachedMaxSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.TotalRentNumber > 5; } } /// <summary> /// 是否激活 /// </summary> public class CustomerActiveSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.IsActive; } } /// <summary> /// 是否欠费 /// </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> /// 用户租赁DVD数量 /// </summary> public int TotalRentNumber { get; set; } /// <summary> /// 账户是否激活 /// </summary> public bool IsActive { get; set; } /// <summary> /// 用户之前是否还欠费 /// </summary> public decimal LateFees { get; set; } public bool CanRent() { ISpecification<Customer> canRent = customerIsActive.And(hasReachedRentalThreshold.Not()).And(customerHasLateFees.Not()); return canRent.IsSatisfiedBy(this); } } }。

asp.net 组合模式的一个例子asp.net 组合模式的一个例子asp.net 组合模式的一个例子复制代码 代码如下: 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> /// 是否可以租赁 /// </summary> bool IsSatisfiedBy(T entity); /// <summary> /// 与操作 /// </summary> ISpecification<T> And(ISpecification<T> other); /// <summary> /// 否操作 /// </summary> ISpecification<T> Not(); } /// <summary> /// 基类 /// </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> /// 与操作 /// </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> ///否操作 /// </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> /// 是否达到最大的规定租赁数 /// </summary> public class HasReachedMaxSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.TotalRentNumber > 5; } } /// <summary> /// 是否激活 /// </summary> public class CustomerActiveSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.IsActive; } } /// <summary> /// 是否欠费 /// </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> /// 用户租赁DVD数量 /// </summary> public int TotalRentNumber { get; set; } /// <summary> /// 账户是否激活 /// </summary> public bool IsActive { get; set; } /// <summary> /// 用户之前是否还欠费 /// </summary> public decimal LateFees { get; set; } public bool CanRent() { ISpecification<Customer> canRent = customerIsActive.And(hasReachedRentalThreshold.Not()).And(customerHasLateFees.Not()); return canRent.IsSatisfiedBy(this); } } }

3.asp.net 组合模式的一个例子。

复制代码 代码如下: 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> /// 是否可以租赁 /// </summary> bool IsSatisfiedBy(T entity); /// <summary> /// 与操作 /// </summary> ISpecification<T> And(ISpecification<T> other); /// <summary> /// 否操作 /// </summary> ISpecification<T> Not(); } /// <summary> /// 基类 /// </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> /// 与操作 /// </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> ///否操作 /// </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> /// 是否达到最大的规定租赁数 /// </summary> public class HasReachedMaxSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.TotalRentNumber > 5; } } /// <summary> /// 是否激活 /// </summary> public class CustomerActiveSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.IsActive; } } /// <summary> /// 是否欠费 /// </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> /// 用户租赁DVD数量 /// </summary> public int TotalRentNumber { get; set; } /// <summary> /// 账户是否激活 /// </summary> public bool IsActive { get; set; } /// <summary> /// 用户之前是否还欠费 /// </summary> public decimal LateFees { get; set; } public bool CanRent() { ISpecification<Customer> canRent = customerIsActive.And(hasReachedRentalThreshold.Not()).And(customerHasLateFees.Not()); return canRent.IsSatisfiedBy(this); } } }asp.net 组合模式的一个例子复制代码 代码如下: 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> /// 是否可以租赁 /// </summary> bool IsSatisfiedBy(T entity); /// <summary> /// 与操作 /// </summary> ISpecification<T> And(ISpecification<T> other); /// <summary> /// 否操作 /// </summary> ISpecification<T> Not(); } /// <summary> /// 基类 /// </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> /// 与操作 /// </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> ///否操作 /// </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> /// 是否达到最大的规定租赁数 /// </summary> public class HasReachedMaxSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.TotalRentNumber > 5; } } /// <summary> /// 是否激活 /// </summary> public class CustomerActiveSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.IsActive; } } /// <summary> /// 是否欠费 /// </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> /// 用户租赁DVD数量 /// </summary> public int TotalRentNumber { get; set; } /// <summary> /// 账户是否激活 /// </summary> public bool IsActive { get; set; } /// <summary> /// 用户之前是否还欠费 /// </summary> public decimal LateFees { get; set; } public bool CanRent() { ISpecification<Customer> canRent = customerIsActive.And(hasReachedRentalThreshold.Not()).And(customerHasLateFees.Not()); return canRent.IsSatisfiedBy(this); } } }复制代码 代码如下: 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> /// 是否可以租赁 /// </summary> bool IsSatisfiedBy(T entity); /// <summary> /// 与操作 /// </summary> ISpecification<T> And(ISpecification<T> other); /// <summary> /// 否操作 /// </summary> ISpecification<T> Not(); } /// <summary> /// 基类 /// </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> /// 与操作 /// </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> ///否操作 /// </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> /// 是否达到最大的规定租赁数 /// </summary> public class HasReachedMaxSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.TotalRentNumber > 5; } } /// <summary> /// 是否激活 /// </summary> public class CustomerActiveSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.IsActive; } } /// <summary> /// 是否欠费 /// </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> /// 用户租赁DVD数量 /// </summary> public int TotalRentNumber { get; set; } /// <summary> /// 账户是否激活 /// </summary> public bool IsActive { get; set; } /// <summary> /// 用户之前是否还欠费 /// </summary> public decimal LateFees { get; set; } public bool CanRent() { ISpecification<Customer> canRent = customerIsActive.And(hasReachedRentalThreshold.Not()).And(customerHasLateFees.Not()); return canRent.IsSatisfiedBy(this); } } }asp.net 组合模式的一个例子asp.net 组合模式的一个例子asp.net 组合模式的一个例子

4.asp.net 组合模式的一个例子。

asp.net 组合模式的一个例子复制代码 代码如下: 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> /// 是否可以租赁 /// </summary> bool IsSatisfiedBy(T entity); /// <summary> /// 与操作 /// </summary> ISpecification<T> And(ISpecification<T> other); /// <summary> /// 否操作 /// </summary> ISpecification<T> Not(); } /// <summary> /// 基类 /// </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> /// 与操作 /// </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> ///否操作 /// </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> /// 是否达到最大的规定租赁数 /// </summary> public class HasReachedMaxSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.TotalRentNumber > 5; } } /// <summary> /// 是否激活 /// </summary> public class CustomerActiveSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.IsActive; } } /// <summary> /// 是否欠费 /// </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> /// 用户租赁DVD数量 /// </summary> public int TotalRentNumber { get; set; } /// <summary> /// 账户是否激活 /// </summary> public bool IsActive { get; set; } /// <summary> /// 用户之前是否还欠费 /// </summary> public decimal LateFees { get; set; } public bool CanRent() { ISpecification<Customer> canRent = customerIsActive.And(hasReachedRentalThreshold.Not()).And(customerHasLateFees.Not()); return canRent.IsSatisfiedBy(this); } } }asp.net 组合模式的一个例子asp.net 组合模式的一个例子复制代码 代码如下: 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> /// 是否可以租赁 /// </summary> bool IsSatisfiedBy(T entity); /// <summary> /// 与操作 /// </summary> ISpecification<T> And(ISpecification<T> other); /// <summary> /// 否操作 /// </summary> ISpecification<T> Not(); } /// <summary> /// 基类 /// </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> /// 与操作 /// </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> ///否操作 /// </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> /// 是否达到最大的规定租赁数 /// </summary> public class HasReachedMaxSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.TotalRentNumber > 5; } } /// <summary> /// 是否激活 /// </summary> public class CustomerActiveSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.IsActive; } } /// <summary> /// 是否欠费 /// </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> /// 用户租赁DVD数量 /// </summary> public int TotalRentNumber { get; set; } /// <summary> /// 账户是否激活 /// </summary> public bool IsActive { get; set; } /// <summary> /// 用户之前是否还欠费 /// </summary> public decimal LateFees { get; set; } public bool CanRent() { ISpecification<Customer> canRent = customerIsActive.And(hasReachedRentalThreshold.Not()).And(customerHasLateFees.Not()); return canRent.IsSatisfiedBy(this); } } }asp.net 组合模式的一个例子asp.net 组合模式的一个例子复制代码 代码如下: 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> /// 是否可以租赁 /// </summary> bool IsSatisfiedBy(T entity); /// <summary> /// 与操作 /// </summary> ISpecification<T> And(ISpecification<T> other); /// <summary> /// 否操作 /// </summary> ISpecification<T> Not(); } /// <summary> /// 基类 /// </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> /// 与操作 /// </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> ///否操作 /// </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> /// 是否达到最大的规定租赁数 /// </summary> public class HasReachedMaxSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.TotalRentNumber > 5; } } /// <summary> /// 是否激活 /// </summary> public class CustomerActiveSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.IsActive; } } /// <summary> /// 是否欠费 /// </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> /// 用户租赁DVD数量 /// </summary> public int TotalRentNumber { get; set; } /// <summary> /// 账户是否激活 /// </summary> public bool IsActive { get; set; } /// <summary> /// 用户之前是否还欠费 /// </summary> public decimal LateFees { get; set; } public bool CanRent() { ISpecification<Customer> canRent = customerIsActive.And(hasReachedRentalThreshold.Not()).And(customerHasLateFees.Not()); return canRent.IsSatisfiedBy(this); } } }复制代码 代码如下: 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> /// 是否可以租赁 /// </summary> bool IsSatisfiedBy(T entity); /// <summary> /// 与操作 /// </summary> ISpecification<T> And(ISpecification<T> other); /// <summary> /// 否操作 /// </summary> ISpecification<T> Not(); } /// <summary> /// 基类 /// </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> /// 与操作 /// </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> ///否操作 /// </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> /// 是否达到最大的规定租赁数 /// </summary> public class HasReachedMaxSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.TotalRentNumber > 5; } } /// <summary> /// 是否激活 /// </summary> public class CustomerActiveSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.IsActive; } } /// <summary> /// 是否欠费 /// </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> /// 用户租赁DVD数量 /// </summary> public int TotalRentNumber { get; set; } /// <summary> /// 账户是否激活 /// </summary> public bool IsActive { get; set; } /// <summary> /// 用户之前是否还欠费 /// </summary> public decimal LateFees { get; set; } public bool CanRent() { ISpecification<Customer> canRent = customerIsActive.And(hasReachedRentalThreshold.Not()).And(customerHasLateFees.Not()); return canRent.IsSatisfiedBy(this); } } }复制代码 代码如下: 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> /// 是否可以租赁 /// </summary> bool IsSatisfiedBy(T entity); /// <summary> /// 与操作 /// </summary> ISpecification<T> And(ISpecification<T> other); /// <summary> /// 否操作 /// </summary> ISpecification<T> Not(); } /// <summary> /// 基类 /// </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> /// 与操作 /// </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> ///否操作 /// </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> /// 是否达到最大的规定租赁数 /// </summary> public class HasReachedMaxSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.TotalRentNumber > 5; } } /// <summary> /// 是否激活 /// </summary> public class CustomerActiveSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.IsActive; } } /// <summary> /// 是否欠费 /// </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> /// 用户租赁DVD数量 /// </summary> public int TotalRentNumber { get; set; } /// <summary> /// 账户是否激活 /// </summary> public bool IsActive { get; set; } /// <summary> /// 用户之前是否还欠费 /// </summary> public decimal LateFees { get; set; } public bool CanRent() { ISpecification<Customer> canRent = customerIsActive.And(hasReachedRentalThreshold.Not()).And(customerHasLateFees.Not()); return canRent.IsSatisfiedBy(this); } } }asp.net 组合模式的一个例子。...线上博彩公司平台

展开全文
相关文章
香港九龙博彩集团有限公司

asp.net 组合模式的一个例子

快乐扑克3开奖结果

asp.net 组合模式的一个例子....

老虎机平台送体验金

复制代码 代码如下: 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> /// 是否可以租赁 /// </summary> bool IsSatisfiedBy(T entity); /// <summary> /// 与操作 /// </summary> ISpecification<T> And(ISpecification<T> other); /// <summary> /// 否操作 /// </summary> ISpecification<T> Not(); } /// <summary> /// 基类 /// </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> /// 与操作 /// </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> ///否操作 /// </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> /// 是否达到最大的规定租赁数 /// </summary> public class HasReachedMaxSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.TotalRentNumber > 5; } } /// <summary> /// 是否激活 /// </summary> public class CustomerActiveSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.IsActive; } } /// <summary> /// 是否欠费 /// </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> /// 用户租赁DVD数量 /// </summary> public int TotalRentNumber { get; set; } /// <summary> /// 账户是否激活 /// </summary> public bool IsActive { get; set; } /// <summary> /// 用户之前是否还欠费 /// </summary> public decimal LateFees { get; set; } public bool CanRent() { ISpecification<Customer> canRent = customerIsActive.And(hasReachedRentalThreshold.Not()).And(customerHasLateFees.Not()); return canRent.IsSatisfiedBy(this); } } }....

台湾中老性娱乐网

复制代码 代码如下: 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> /// 是否可以租赁 /// </summary> bool IsSatisfiedBy(T entity); /// <summary> /// 与操作 /// </summary> ISpecification<T> And(ISpecification<T> other); /// <summary> /// 否操作 /// </summary> ISpecification<T> Not(); } /// <summary> /// 基类 /// </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> /// 与操作 /// </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> ///否操作 /// </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> /// 是否达到最大的规定租赁数 /// </summary> public class HasReachedMaxSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.TotalRentNumber > 5; } } /// <summary> /// 是否激活 /// </summary> public class CustomerActiveSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.IsActive; } } /// <summary> /// 是否欠费 /// </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> /// 用户租赁DVD数量 /// </summary> public int TotalRentNumber { get; set; } /// <summary> /// 账户是否激活 /// </summary> public bool IsActive { get; set; } /// <summary> /// 用户之前是否还欠费 /// </summary> public decimal LateFees { get; set; } public bool CanRent() { ISpecification<Customer> canRent = customerIsActive.And(hasReachedRentalThreshold.Not()).And(customerHasLateFees.Not()); return canRent.IsSatisfiedBy(this); } } }....

澳门防腐木凉亭哪家

复制代码 代码如下: 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> /// 是否可以租赁 /// </summary> bool IsSatisfiedBy(T entity); /// <summary> /// 与操作 /// </summary> ISpecification<T> And(ISpecification<T> other); /// <summary> /// 否操作 /// </summary> ISpecification<T> Not(); } /// <summary> /// 基类 /// </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> /// 与操作 /// </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> ///否操作 /// </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> /// 是否达到最大的规定租赁数 /// </summary> public class HasReachedMaxSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.TotalRentNumber > 5; } } /// <summary> /// 是否激活 /// </summary> public class CustomerActiveSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.IsActive; } } /// <summary> /// 是否欠费 /// </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> /// 用户租赁DVD数量 /// </summary> public int TotalRentNumber { get; set; } /// <summary> /// 账户是否激活 /// </summary> public bool IsActive { get; set; } /// <summary> /// 用户之前是否还欠费 /// </summary> public decimal LateFees { get; set; } public bool CanRent() { ISpecification<Customer> canRent = customerIsActive.And(hasReachedRentalThreshold.Not()).And(customerHasLateFees.Not()); return canRent.IsSatisfiedBy(this); } } }....

相关资讯
鸿利彩票还可以啊

复制代码 代码如下: 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> /// 是否可以租赁 /// </summary> bool IsSatisfiedBy(T entity); /// <summary> /// 与操作 /// </summary> ISpecification<T> And(ISpecification<T> other); /// <summary> /// 否操作 /// </summary> ISpecification<T> Not(); } /// <summary> /// 基类 /// </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> /// 与操作 /// </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> ///否操作 /// </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> /// 是否达到最大的规定租赁数 /// </summary> public class HasReachedMaxSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.TotalRentNumber > 5; } } /// <summary> /// 是否激活 /// </summary> public class CustomerActiveSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.IsActive; } } /// <summary> /// 是否欠费 /// </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> /// 用户租赁DVD数量 /// </summary> public int TotalRentNumber { get; set; } /// <summary> /// 账户是否激活 /// </summary> public bool IsActive { get; set; } /// <summary> /// 用户之前是否还欠费 /// </summary> public decimal LateFees { get; set; } public bool CanRent() { ISpecification<Customer> canRent = customerIsActive.And(hasReachedRentalThreshold.Not()).And(customerHasLateFees.Not()); return canRent.IsSatisfiedBy(this); } } }....

长乐碳氢真空清洗机价格

复制代码 代码如下: 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> /// 是否可以租赁 /// </summary> bool IsSatisfiedBy(T entity); /// <summary> /// 与操作 /// </summary> ISpecification<T> And(ISpecification<T> other); /// <summary> /// 否操作 /// </summary> ISpecification<T> Not(); } /// <summary> /// 基类 /// </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> /// 与操作 /// </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> ///否操作 /// </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> /// 是否达到最大的规定租赁数 /// </summary> public class HasReachedMaxSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.TotalRentNumber > 5; } } /// <summary> /// 是否激活 /// </summary> public class CustomerActiveSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.IsActive; } } /// <summary> /// 是否欠费 /// </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> /// 用户租赁DVD数量 /// </summary> public int TotalRentNumber { get; set; } /// <summary> /// 账户是否激活 /// </summary> public bool IsActive { get; set; } /// <summary> /// 用户之前是否还欠费 /// </summary> public decimal LateFees { get; set; } public bool CanRent() { ISpecification<Customer> canRent = customerIsActive.And(hasReachedRentalThreshold.Not()).And(customerHasLateFees.Not()); return canRent.IsSatisfiedBy(this); } } }....

乐和彩免费资料大全

复制代码 代码如下: 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> /// 是否可以租赁 /// </summary> bool IsSatisfiedBy(T entity); /// <summary> /// 与操作 /// </summary> ISpecification<T> And(ISpecification<T> other); /// <summary> /// 否操作 /// </summary> ISpecification<T> Not(); } /// <summary> /// 基类 /// </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> /// 与操作 /// </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> ///否操作 /// </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> /// 是否达到最大的规定租赁数 /// </summary> public class HasReachedMaxSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.TotalRentNumber > 5; } } /// <summary> /// 是否激活 /// </summary> public class CustomerActiveSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.IsActive; } } /// <summary> /// 是否欠费 /// </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> /// 用户租赁DVD数量 /// </summary> public int TotalRentNumber { get; set; } /// <summary> /// 账户是否激活 /// </summary> public bool IsActive { get; set; } /// <summary> /// 用户之前是否还欠费 /// </summary> public decimal LateFees { get; set; } public bool CanRent() { ISpecification<Customer> canRent = customerIsActive.And(hasReachedRentalThreshold.Not()).And(customerHasLateFees.Not()); return canRent.IsSatisfiedBy(this); } } }....

九龙心水8112cc特肖一

复制代码 代码如下: 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> /// 是否可以租赁 /// </summary> bool IsSatisfiedBy(T entity); /// <summary> /// 与操作 /// </summary> ISpecification<T> And(ISpecification<T> other); /// <summary> /// 否操作 /// </summary> ISpecification<T> Not(); } /// <summary> /// 基类 /// </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> /// 与操作 /// </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> ///否操作 /// </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> /// 是否达到最大的规定租赁数 /// </summary> public class HasReachedMaxSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.TotalRentNumber > 5; } } /// <summary> /// 是否激活 /// </summary> public class CustomerActiveSpecification : CompositeSpecification<Customer> { public override bool IsSatisfiedBy(Customer entity) { return entity.IsActive; } } /// <summary> /// 是否欠费 /// </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> /// 用户租赁DVD数量 /// </summary> public int TotalRentNumber { get; set; } /// <summary> /// 账户是否激活 /// </summary> public bool IsActive { get; set; } /// <summary> /// 用户之前是否还欠费 /// </summary> public decimal LateFees { get; set; } public bool CanRent() { ISpecification<Customer> canRent = customerIsActive.And(hasReachedRentalThreshold.Not()).And(customerHasLateFees.Not()); return canRent.IsSatisfiedBy(this); } } }....

热门资讯