加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > PHP > 正文

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

发布时间:2020-05-24 17:01:32 所属栏目:PHP 来源:互联网
导读:asp.net 组合模式的一个例子,方便学习asp.net的朋友作为参考

<div class="codetitle"><a style="CURSOR: pointer" data="27842" class="copybut" id="copybut27842" onclick="doCopy('code27842')"> 代码如下:<div class="codebody" id="code27842">
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);
}
}
}

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读