• 企业400电话
  • 微网小程序
  • AI电话机器人
  • 电商代运营
  • 全 部 栏 目

    企业400电话 网络优化推广 AI电话机器人 呼叫中心 网站建设 商标✡知产 微网小程序 电商运营 彩铃•短信 增值拓展业务
    asp.net 组合模式的一个例子
    复制代码 代码如下:

    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)
    上一篇:未能加载文件或程序集“XXX”或它的某一个依赖项。试图加载格式不正确的程序。
    下一篇:c# 可变数目参数params实例
  • 相关文章
  • 

    © 2016-2020 巨人网络通讯 版权所有

    《增值电信业务经营许可证》 苏ICP备15040257号-8

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