asp.net 组合模式的PHP代码

时间:2024-10-07 12:35:26 ASP 我要投稿
  • 相关推荐

asp.net 组合模式的PHP代码

  复制代码 代码如下:

  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);

  ///

  /// 与操作

  ///

  ISpecificationAnd(ISpecificationother);

  ///

  /// 否操作

  ///

  ISpecificationNot();

  }

  ///

  /// 基类

  ///

  public abstract class CompositeSpecification: ISpecification

  {

  public abstract bool IsSatisfiedBy(T candidate);

  public ISpecificationAnd(ISpecificationother)

  {

  return new AndSpecification(this, other);

  }

  public ISpecificationNot()

  {

  return new NotSpecification(this);

  }

  }

  ///

  /// 与操作

  ///

  public class AndSpecification: CompositeSpecification

  {

  private ISpecificationleftSpecification;

  private ISpecificationrightSpecification;

  public AndSpecification(ISpecificationleftSpecification, ISpecificationrightSpecification)

  {

  this.leftSpecification = leftSpecification;

  this.rightSpecification = rightSpecification;

  }

  public override bool IsSatisfiedBy(T entity)

  {

  return leftSpecification.IsSatisfiedBy(entity) && rightSpecification.IsSatisfiedBy(entity);

  }

  }

  ///

  ///否操作

  ///

  public class NotSpecification: CompositeSpecification

  {

  private ISpecificationinnerSpecification;

  public NotSpecification(ISpecificationinnerSpecification)

  {

  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 ISpecificationhasReachedRentalThreshold;

  private ISpecificationcustomerIsActive;

  private ISpecificationcustomerHasLateFees;

  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()

  {

  ISpecificationcanRent = customerIsActive.And(hasReachedRentalThreshold.Not()).And(customerHasLateFees.Not());

  return canRent.IsSatisfiedBy(this);

  }

  }

  }

【asp.net 组合模式的PHP代码】相关文章:

php数组函数序列之array-combine() - 数组合并函数的代码08-25

php如何过滤危险html代码09-21

将php实现过滤UBB代码09-11

php对图像的各种处理函数代码总结07-03

如何在cmd下面写php代码01-22

ASP.NET连SQL7接口的源代码06-06

PHP url 加密解密函数代码方法10-25

企业薪酬设计基本模式及组合模式02-26

关于PHP var-dump遍历对象属性的函数与应用代码06-21

关于ASP.NET使用JavaScript显示信息提示窗口实现原理及代码05-09