时间:2021-05-28
复制代码 代码如下:
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);
}
}
}
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
在在ASP.NET中动态的生成GIF图片一文中介绍了使用ASP.NET生成一个GIF图片的方法,本文对这个例子稍加修改就可以生成比较实用的饼图了。源代码如下:/
我们开发ASP.NET站点时,如果将jQueryUIDatepicker与ASP.NET的验证控件(如:RequiredFieldValidator)组合使用:
ASP.NET通过两种方式支持缓存:通过CacheAPI存储任意数据,通过页面输出缓存经常被访问的页面。让我们来看一个例子。一个电子商务的站点,它的目录一般一周
错误发生的原因是当ASP.NET检测到Web站点配置为使用ASP.NET4.0,本地ASP.NET4.0的组件会传递一个不能扩展的URL到ASP.NET的管理程
在你将现有的用户登录(SignIn)站点从ASP.NET迁移至ASP.NETCore时,你将面临这样一个问题——如何让ASP.NET与ASP.NETCore用户