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

2015-01-24信息快讯网

asp.net 组合模式的一个例子,方便学习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 ISpecification<T> 
{ 
/// <summary> 
/// 是否可以租赁 
/// </summary> 
bool IsSatisfiedBy(T entity); 
/// <summary> 
/// 与操作 
/// </summary> 
ISpecification<T> And(ISpecification<T> other); 
/// <summary> 
/// 否操作 
/// </summary> 
ISpecification<T> Not(); 
} 
/// <summary> 
/// 基类 
/// </summary> 
public abstract class CompositeSpecification<T> : ISpecification<T> 
{ 
public abstract bool IsSatisfiedBy(T candidate); 
public ISpecification<T> And(ISpecification<T> other) 
{ 
return new AndSpecification<T>(this, other); 
} 
public ISpecification<T> Not() 
{ 
return new NotSpecification<T>(this); 
} 
} 
/// <summary> 
/// 与操作 
/// </summary> 
public class AndSpecification<T> : CompositeSpecification<T> 
{ 
private ISpecification<T> leftSpecification; 
private ISpecification<T> rightSpecification; 
public AndSpecification(ISpecification<T> leftSpecification, ISpecification<T> rightSpecification) 
{ 
this.leftSpecification = leftSpecification; 
this.rightSpecification = rightSpecification; 
} 
public override bool IsSatisfiedBy(T entity) 
{ 
return leftSpecification.IsSatisfiedBy(entity) && rightSpecification.IsSatisfiedBy(entity); 
} 
} 
///<summary> 
///否操作 
/// </summary> 
public class NotSpecification<T> : CompositeSpecification<T> 
{ 
private ISpecification<T> innerSpecification; 
public NotSpecification(ISpecification<T> innerSpecification) 
{ 
this.innerSpecification = innerSpecification; 
} 
public override bool IsSatisfiedBy(T entity) 
{ 
return !innerSpecification.IsSatisfiedBy(entity); 
} 
} 
/// <summary> 
/// 是否达到最大的规定租赁数 
/// </summary> 
public class HasReachedMaxSpecification : CompositeSpecification<Customer> 
{ 
public override bool IsSatisfiedBy(Customer entity) 
{ 
return entity.TotalRentNumber > 5; 
} 
} 
/// <summary> 
/// 是否激活 
/// </summary> 
public class CustomerActiveSpecification : CompositeSpecification<Customer> 
{ 
public override bool IsSatisfiedBy(Customer entity) 
{ 
return entity.IsActive; 
} 
} 
/// <summary> 
/// 是否欠费 
/// </summary> 
public class CustomerHasLateFeesSpecification : CompositeSpecification<Customer> 
{ 
public override bool IsSatisfiedBy(Customer entity) 
{ 
return entity.LateFees > 0; 
} 
} 
public class Customer 
{ 
private ISpecification<Customer> hasReachedRentalThreshold; 
private ISpecification<Customer> customerIsActive; 
private ISpecification<Customer> 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() 
{ 
ISpecification<Customer> canRent = customerIsActive.And(hasReachedRentalThreshold.Not()).And(customerHasLateFees.Not()); 
return canRent.IsSatisfiedBy(this); 
} 
} 
} 
file_get_contents("php://input", "r")实例介绍
解析PHP中empty is_null和isset的测试
解析curl提交GET,POST,Cookie的简单方法
解析php session_set_save_handler 函数的用法(mysql)
如何使用PHP实现javascript的escape和unescape函数
php setcookie(name, value, expires, path, domain, secure) 参数详解
浅析memcache启动以及telnet命令详解
php 去除html标记--strip_tags与htmlspecialchars的区别详解
关于Sphinx创建全文检索的索引介绍
使用Sphinx对索引进行搜索
PHP Class&Object -- 解析PHP实现二叉树
PHP Class&Object -- PHP 自排序二叉树的深入解析
解析php addslashes()与addclashes()函数的区别和比较
探讨:php中在foreach中使用foreach ($arr as &$value) 这种类型的解释
解析coreseek for sphinx的使用
php输出echo、print、print_r、printf、sprintf、var_dump的区别比较
解析:通过php socket并借助telnet实现简单的聊天程序
解析php中static,const与define的使用区别
解析php中const与define的应用区别
php教程 插件机制在PHP中实现方案
PHP类与对象中的private访问控制的疑问
php daddslashes()和 saddslashes()有哪些区别分析
PHP daddslashes 使用方法介绍
php模拟js函数unescape的函数代码
记录PHP错误日志 display_errors与log_errors的区别
仿Aspnetpager的一个PHP分页类代码 附源码下载
PHP 正则表达式之正则处理函数小结(preg_match,preg_match_all,preg_replace,preg_split)
php中explode与split的区别介绍
PHP HTML JavaScript MySQL代码如何互相传值的方法分享
如何获知PHP程序占用多少内存(memory_get_usage)
PHP管理内存函数 memory_get_usage()使用介绍
详解php的魔术方法__get()和__set()使用介绍
用来解析.htpasswd文件的PHP类
PHP setTime 设置当前时间的代码
php中3des加密代码(完全与.net中的兼容)
PHP sprintf() 函数的应用(定义和用法)
PHP Parse Error: syntax error, unexpected $end 错误的解决办法
php提示undefined index的几种解决方法
©2014-2024 dbsqp.com