主页 > 知识库 > asp.net 组合模式的一个例子

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

热门标签:团购网站 电子围栏 科大讯飞语音识别系统 银行业务 服务器配置 Mysql连接数设置 阿里云 Linux服务器
复制代码 代码如下:

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 ISpecificationT>
{
/// summary>
/// 是否可以租赁
/// /summary>
bool IsSatisfiedBy(T entity);
/// summary>
/// 与操作
/// /summary>
ISpecificationT> And(ISpecificationT> other);
/// summary>
/// 否操作
/// /summary>
ISpecificationT> Not();
}
/// summary>
/// 基类
/// /summary>
public abstract class CompositeSpecificationT> : ISpecificationT>
{
public abstract bool IsSatisfiedBy(T candidate);
public ISpecificationT> And(ISpecificationT> other)
{
return new AndSpecificationT>(this, other);
}
public ISpecificationT> Not()
{
return new NotSpecificationT>(this);
}
}
/// summary>
/// 与操作
/// /summary>
public class AndSpecificationT> : CompositeSpecificationT>
{
private ISpecificationT> leftSpecification;
private ISpecificationT> rightSpecification;
public AndSpecification(ISpecificationT> leftSpecification, ISpecificationT> rightSpecification)
{
this.leftSpecification = leftSpecification;
this.rightSpecification = rightSpecification;
}
public override bool IsSatisfiedBy(T entity)
{
return leftSpecification.IsSatisfiedBy(entity) rightSpecification.IsSatisfiedBy(entity);
}
}
///summary>
///否操作
/// /summary>
public class NotSpecificationT> : CompositeSpecificationT>
{
private ISpecificationT> innerSpecification;
public NotSpecification(ISpecificationT> innerSpecification)
{
this.innerSpecification = innerSpecification;
}
public override bool IsSatisfiedBy(T entity)
{
return !innerSpecification.IsSatisfiedBy(entity);
}
}
/// summary>
/// 是否达到最大的规定租赁数
/// /summary>
public class HasReachedMaxSpecification : CompositeSpecificationCustomer>
{
public override bool IsSatisfiedBy(Customer entity)
{
return entity.TotalRentNumber > 5;
}
}
/// summary>
/// 是否激活
/// /summary>
public class CustomerActiveSpecification : CompositeSpecificationCustomer>
{
public override bool IsSatisfiedBy(Customer entity)
{
return entity.IsActive;
}
}
/// summary>
/// 是否欠费
/// /summary>
public class CustomerHasLateFeesSpecification : CompositeSpecificationCustomer>
{
public override bool IsSatisfiedBy(Customer entity)
{
return entity.LateFees > 0;
}
}
public class Customer
{
private ISpecificationCustomer> hasReachedRentalThreshold;
private ISpecificationCustomer> customerIsActive;
private ISpecificationCustomer> 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()
{
ISpecificationCustomer> canRent = customerIsActive.And(hasReachedRentalThreshold.Not()).And(customerHasLateFees.Not());
return canRent.IsSatisfiedBy(this);
}
}
}
您可能感兴趣的文章:
  • Android源码学习之组合模式定义及应用
  • JavaScript 设计模式之组合模式解析
  • php设计模式 Composite (组合模式)
  • C++设计模式之组合模式
  • c# 组合模式
  • Java设计模式之组合模式(Composite模式)介绍
  • C#组合模式实例详解
  • Python的组合模式与责任链模式编程示例
  • iOS应用开发中运用设计模式中的组合模式的实例解析
  • java设计模式之组合模式(Composite)

标签:广元 枣庄 江苏 萍乡 衡水 蚌埠 衢州 大理

巨人网络通讯声明:本文标题《asp.net 组合模式的一个例子》,本文关键词  ;如发现本文内容存在版权问题,烦请提供相关信息告之我们,我们将及时沟通与处理。本站内容系统采集于网络,涉及言论、版权与本站无关。
  • 相关文章
  • 收缩
    • 微信客服
    • 微信二维码
    • 电话咨询

    • 400-1100-266