asp.net 組合模式的一個例子
更新時間:2012年11月01日 23:21:28 作者:
asp.net 組合模式的一個例子,方便學(xué)習(xí)asp.net的朋友作為參考
復(fù)制代碼 代碼如下:
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>
/// 是否達到最大的規(guī)定租賃數(shù)
/// </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數(shù)量
/// </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);
}
}
}
相關(guān)文章
ASP.NET與ASP互通COOKIES的一點經(jīng)驗
ASP與ASP.NET互相整合時,其中文COOKIES信息無法被互通共享,當使用ASP.NET寫入中文COOKIES信息后,使用ASP進行讀取,讀出來的卻是亂碼,而非中文。2010-03-03
VB.net 查詢獲取數(shù)據(jù)庫數(shù)據(jù)信息
VB.net 查詢獲取數(shù)據(jù)庫數(shù)據(jù)信息實現(xiàn)函數(shù),需要的朋友可以參考下,代碼比較簡單。2009-07-07
asp.net?core?+?jenkins?實現(xiàn)自動化發(fā)布功能
這篇文章主要介紹了asp.net?core?+?jenkins?實現(xiàn)自動化發(fā)布功能,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-04-04
asp.net mvc webapi 實用的接口加密方法示例
本篇文章主要介紹了asp.net mvc webapi 實用的接口加密方法示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10
Asp.Net Couchbase Memcached圖文安裝調(diào)用開發(fā)
本文主要是是如何安裝CouchBase服務(wù)端,以及客戶端如何進行調(diào)用。圖文詳解,大家參考吧2013-11-11

