复制代码 代码如下:
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)