您好,登錄后才能下訂單哦!
設(shè)計(jì)模式-規(guī)約模式C#版
規(guī)約模式的使用場(chǎng)景就是規(guī)則,業(yè)務(wù)規(guī)則的碎片化。
業(yè)務(wù)規(guī)則的組合是不固定的,需要做成很容易組合,也很容易拆散的方式,規(guī)約模式是一個(gè)選擇。
下面的例子是一個(gè)書店中,用戶租書的場(chǎng)景。
需要判斷用戶的最大租書數(shù)和用戶的狀態(tài),需要同時(shí)滿足這兩個(gè)要求,才可以繼續(xù)租書。最大租書數(shù)和狀態(tài)這兩個(gè)規(guī)則拆散開來(lái),在需要的時(shí)候再進(jìn)行組合。不需要組合的地方,就單獨(dú)使用這些規(guī)則。
針對(duì)一個(gè)實(shí)體有不同的規(guī)則,把這些規(guī)則碎片化,隨意組合和拆散,這樣就構(gòu)成了規(guī)約模式。
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace DomainModel.Model
- {
- /// <summary>
- /// 規(guī)約模式
- /// </summary>
- /// <typeparam name="T"></typeparam>
- public interface ISpecification<T>
- {
- bool IsSatisfiedBy(T entity);
- /// <summary>
- /// 與規(guī)約
- /// </summary>
- /// <param name="specification"></param>
- /// <returns></returns>
- ISpecification<T> And(ISpecification<T> specification);
- /// <summary>
- /// 或規(guī)約
- /// </summary>
- /// <param name="specification"></param>
- /// <returns></returns>
- ISpecification<T> Or(ISpecification<T> specification);
- /// <summary>
- /// 非規(guī)約
- /// </summary>
- /// <returns></returns>
- ISpecification<T> Not();
- }
- public class Customer
- {
- private ISpecification<Customer> _hasReachedMax;
- private ISpecification<Customer> _active;
- public Customer(ISpecification<Customer> hasReachedMax, ISpecification<Customer> active)
- {
- this._hasReachedMax = hasReachedMax;
- this._active = active;
- }
- public int TotalRentNumber { get; set; }
- public bool Active
- {
- get { return true; }
- }
- public bool CanRent()
- {
- var specification = this._hasReachedMax.Not().And(this._active.Not());
- return specification.IsSatisfiedBy(this);
- }
- }
- public class HasReachedMaxSpecification : CompositeSpecification<Customer>
- {
- public override bool IsSatisfiedBy(Customer entity)
- {
- return entity.TotalRentNumber >= 6;
- }
- }
- public class CustomerActiveSpecification : CompositeSpecification<Customer>
- {
- public override bool IsSatisfiedBy(Customer entity)
- {
- return entity.Active;
- }
- }
- /// <summary>
- /// 組合規(guī)約
- /// </summary>
- /// <typeparam name="T"></typeparam>
- public abstract class CompositeSpecification<T> : ISpecification<T>
- {
- public abstract bool IsSatisfiedBy(T entity);
- public ISpecification<T> And(ISpecification<T> specification)
- {
- return new AndSpecification<T>(this, specification);
- }
- public ISpecification<T> Not()
- {
- return new NotSpecification<T>(this);
- }
- public ISpecification<T> Or(ISpecification<T> specification)
- {
- throw new NotImplementedException();
- }
- }
- public class AndSpecification<T> : CompositeSpecification<T>
- {
- private ISpecification<T> _left;
- private ISpecification<T> _right;
- public AndSpecification(ISpecification<T> left, ISpecification<T> right)
- {
- this._left = left;
- this._right = right;
- }
- public override bool IsSatisfiedBy(T entity)
- {
- return this._left.IsSatisfiedBy(entity) && this._right.IsSatisfiedBy(entity);
- }
- }
- public class OrSpecification<T> : CompositeSpecification<T>
- {
- private ISpecification<T> _left;
- private ISpecification<T> _right;
- public OrSpecification(ISpecification<T> left, ISpecification<T> right)
- {
- this._left = left;
- this._right = right;
- }
- public override bool IsSatisfiedBy(T entity)
- {
- return this._left.IsSatisfiedBy(entity) || this._right.IsSatisfiedBy(entity);
- }
- }
- public class NotSpecification<T> : CompositeSpecification<T>
- {
- private ISpecification<T> _inner;
- public NotSpecification(ISpecification<T> inner)
- {
- this._inner = inner;
- }
- public override bool IsSatisfiedBy(T entity)
- {
- return !this._inner.IsSatisfiedBy(entity);
- }
- }
- }
參考文獻(xiàn)
1.走向.NET架構(gòu)設(shè)計(jì)—第五章—業(yè)務(wù)層模式,原則,實(shí)踐(前篇)
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。