设计模式-规约模式C#版
设计模式-规约模式C#版
10年积累的网站建设、网站制作经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先做网站后付款的网站建设流程,更有井陉免费网站建设让你可以放心的选择与我们合作。
规约模式的使用场景就是规则,业务规则的碎片化。
业务规则的组合是不固定的,需要做成很容易组合,也很容易拆散的方式,规约模式是一个选择。
下面的例子是一个书店中,用户租书的场景。
需要判断用户的最大租书数和用户的状态,需要同时满足这两个要求,才可以继续租书。最大租书数和状态这两个规则拆散开来,在需要的时候再进行组合。不需要组合的地方,就单独使用这些规则。
针对一个实体有不同的规则,把这些规则碎片化,随意组合和拆散,这样就构成了规约模式。
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace DomainModel.Model
- {
- ///
- /// 规约模式
- ///
- ///
- public interface ISpecification
- {
- bool IsSatisfiedBy(T entity);
- ///
- /// 与规约
- ///
- ///
- ///
- ISpecification
And(ISpecification specification); - ///
- /// 或规约
- ///
- ///
- ///
- ISpecification
Or(ISpecification specification); - ///
- /// 非规约
- ///
- ///
- 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;
- }
- }
- ///
- /// 组合规约
- ///
- ///
- 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);
- }
- }
- }
参考文献
1.走向.NET架构设计—第五章—业务层模式,原则,实践(前篇)
文章标题:设计模式-规约模式C#版
转载注明:http://hbruida.cn/article/igsgdh.html