asp.net 组合模式的一个例子 -下载和记娱乐

2023-07-08 10:41 9次浏览 问答

代码如下:
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 and(ispecification other);
///
/// 否操作
///

ispecification not();
}
///
/// 基类
///

public abstract class compositespecification : ispecification
{
public abstract bool issatisfiedby(t candidate);
public ispecification and(ispecification other)
{
return new andspecification(this, other);
}
public ispecification not()
{
return new notspecification(this);
}
}
///
/// 与操作
///

public class andspecification : compositespecification
{
private ispecification leftspecification;
private ispecification rightspecification;
public andspecification(ispecification leftspecification, ispecification rightspecification)
{
this。
leftspecification = leftspecification;
this。rightspecification = rightspecification;
}
public override bool issatisfiedby(t entity)
{
return leftspecification。
issatisfiedby(entity) && rightspecification。issatisfiedby(entity);
}
}
///
///否操作
///

public class notspecification : compositespecification
{
private ispecification innerspecification;
public notspecification(ispecification innerspecification)
{
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 hasreachedrentalthreshold;
private ispecification customerisactive;
private ispecification customerhaslatefees;
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 canrent = customerisactive。
and(hasreachedrentalthreshold。not())。and(customerhaslatefees。not());
return canrent。issatisfiedby(this);
}
}
}

相关推荐

网站地图