asp.net 组合模式的一个例子 -k8凯发
代码如下:
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
{
///
/// 是否可以租赁
///
bool issatisfiedby(t entity);
///
/// 与操作
///
ispecification
///
/// 否操作
///
ispecification
}
///
/// 基类
///
public abstract class compositespecification
{
public abstract bool issatisfiedby(t candidate);
public ispecification
{
return new andspecification
}
public ispecification
{
return new notspecification
}
}
///
/// 与操作
///
public class andspecification
{
private ispecification
private ispecification
public andspecification(ispecification
{
this。
leftspecification = leftspecification;
this。rightspecification = rightspecification;
}
public override bool issatisfiedby(t entity)
{
return leftspecification。
issatisfiedby(entity) && rightspecification。issatisfiedby(entity);
}
}
///
///否操作
///
public class notspecification
{
private ispecification
public notspecification(ispecification
{
this。
innerspecification = innerspecification;
}
public override bool issatisfiedby(t entity)
{
return !innerspecification。
issatisfiedby(entity);
}
}
///
/// 是否达到最大的规定租赁数
///
public class hasreachedmaxspecification : compositespecification
{
public override bool issatisfiedby(customer entity)
{
return entity。
totalrentnumber > 5;
}
}
///
/// 是否激活
///
public class customeractivespecification : compositespecification
{
public override bool issatisfiedby(customer entity)
{
return entity。
isactive;
}
}
///
/// 是否欠费
///
public class customerhaslatefeesspecification : compositespecification
{
public override bool issatisfiedby(customer entity)
{
return entity。
latefees > 0;
}
}
public class customer
{
private ispecification
private ispecification
private ispecification
public customer()
{
hasreachedrentalthreshold = new hasreachedmaxspecification();
customerisactive = new customeractivespecification();
customerhaslatefees = new customerhaslatefeesspecification();
}
///
/// 用户租赁dvd数量
///
public int totalrentnumber
{
get;
set;
}
///
/// 账户是否激活
///
public bool isactive
{
get;
set;
}
///
/// 用户之前是否还欠费
///
public decimal latefees
{
get;
set;
}
public bool canrent()
{
ispecification
and(hasreachedrentalthreshold。not())。and(customerhaslatefees。not());
return canrent。issatisfiedby(this);
}
}
}
。
1.文章《asp.net 组合模式的一个例子》援引自互联网,为网友投稿收集整理,仅供学习和研究使用,内容仅代表作者本人观点,与本网站无关,侵删请点击页脚凯发k8国际手机app下载的联系方式。
2.文章《asp.net 组合模式的一个例子》仅供读者参考,本网站未对该内容进行证实,对其原创性、真实性、完整性、及时性不作任何保证。