欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-09-03 09:33:14
复制代码 代码如下: using system; using system.collections.generic; using system.linq; using sy...
复制代码 代码如下:

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);
}
}
}