asp.net 组合模式的一个例子
程序员文章站
2022-04-20 23:02:35
复制代码 代码如下: 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);
}
}
}
推荐阅读
-
java 用递归获取一个目录下的所有文件路径的小例子
-
在ASP.NET 2.0中操作数据之六十:创建一个自定义的Database-Driven Site Map Provider
-
java实现把两个有序数组合并到一个数组的实例
-
使用JSP + JAVABEAN + XML 开发的一个例子
-
一个分页的类及调用的例子
-
asp.net实现在XmlTextWriter中写入一个CDATA的方法
-
详解java创建一个女朋友类(对象啥的new一个就是)==建造者模式,一键重写
-
在ASP.NET 2.0中操作数据之六十:创建一个自定义的Database-Driven Site Map Provider
-
MySQL一个索引最多有多少个列?真实的测试例子
-
asp.net实现上传图片时判断图片的模式GRB或CMYK的方法