設(shè)計(jì)模式-規(guī)約模式C#版
10年積累的網(wǎng)站建設(shè)、網(wǎng)站制作經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先做網(wǎng)站后付款的網(wǎng)站建設(shè)流程,更有井陘免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
規(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
- {
- ///
- /// 規(guī)約模式
- ///
- ///
- public interface ISpecification
- {
- bool IsSatisfiedBy(T entity);
- ///
- /// 與規(guī)約
- ///
- ///
- ///
- ISpecification
And(ISpecification specification); - ///
- /// 或規(guī)約
- ///
- ///
- ///
- ISpecification
Or(ISpecification specification); - ///
- /// 非規(guī)約
- ///
- ///
- ISpecification
Not(); - }
- public class Customer
- {
- private ISpecification
_hasReachedMax; - private ISpecification
_active; - public Customer(ISpecification
hasReachedMax, ISpecification 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
- {
- public override bool IsSatisfiedBy(Customer entity)
- {
- return entity.TotalRentNumber >= 6;
- }
- }
- public class CustomerActiveSpecification : CompositeSpecification
- {
- public override bool IsSatisfiedBy(Customer entity)
- {
- return entity.Active;
- }
- }
- ///
- /// 組合規(guī)約
- ///
- ///
- public abstract class CompositeSpecification
: ISpecification - {
- public abstract bool IsSatisfiedBy(T entity);
- public ISpecification
And(ISpecification specification) - {
- return new AndSpecification
(this, specification); - }
- public ISpecification
Not() - {
- return new NotSpecification
(this); - }
- public ISpecification
Or(ISpecification specification) - {
- throw new NotImplementedException();
- }
- }
- public class AndSpecification
: CompositeSpecification - {
- private ISpecification
_left; - private ISpecification
_right; - public AndSpecification(ISpecification
left, ISpecification 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
: CompositeSpecification - {
- private ISpecification
_left; - private ISpecification
_right; - public OrSpecification(ISpecification
left, ISpecification 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
: CompositeSpecification - {
- private ISpecification
_inner; - public NotSpecification(ISpecification
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í)踐(前篇)