复制代码 代码如下:
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,组合模式
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
桃源资源网 Design By www.nqtax.com
暂无“asp.net 组合模式的一个例子”评论...
P70系列延期,华为新旗舰将在下月发布
3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。
而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?
根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。