asp.net组合模式的一个例子_PHP教程
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了asp.net组合模式的一个例子_PHP教程,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3192字,纯文字阅读大概需要5分钟。
内容图文
![asp.net组合模式的一个例子_PHP教程](/upload/InfoBanner/zyjiaocheng/170/f68861f39ef44aeb98dabca2691357ff.jpg)
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(ISpecification other);
///
/// 否操作
///
ISpecificationNot();
}
///
/// 基类
///
public abstract class CompositeSpecification: ISpecification
{
public abstract bool IsSatisfiedBy(T candidate);
public ISpecificationAnd(ISpecification other)
{
return new AndSpecification(this, other);
}
public ISpecificationNot()
{
return new NotSpecification(this);
}
}
///
/// 与操作
///
public class AndSpecification: CompositeSpecification
{
private ISpecificationleftSpecification;
private ISpecificationrightSpecification;
public AndSpecification(ISpecificationleftSpecification, 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 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);
}
}
}
http://www.bkjia.com/PHPjc/326154.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/326154.htmlTechArticle 代码如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Test { class Program { static void Main(string[] args) {...
内容总结
以上是互联网集市为您收集整理的asp.net组合模式的一个例子_PHP教程全部内容,希望文章能够帮你解决asp.net组合模式的一个例子_PHP教程所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。