Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Design patterns 规范模式已修改(构造函数中的候选)_Design Patterns_Specification Pattern - Fatal编程技术网

Design patterns 规范模式已修改(构造函数中的候选)

Design patterns 规范模式已修改(构造函数中的候选),design-patterns,specification-pattern,Design Patterns,Specification Pattern,我知道设计模式是由设计设定的,而不是由特定的代码设定的,但有时我会担心我过度扭曲了模式,不再遵循设计 例如,规范模式如下所示: public interface ISpecification<T> { bool IsSatisfiedBy(T candidate); } _customerAccountIsActive && _customerAccountHasLateFees && _hasReachedRentalThreshold

我知道设计模式是由设计设定的,而不是由特定的代码设定的,但有时我会担心我过度扭曲了模式,不再遵循设计

例如,规范模式如下所示:

 public interface ISpecification<T>
{
    bool IsSatisfiedBy(T candidate);
}
_customerAccountIsActive 
&& _customerAccountHasLateFees
&& _hasReachedRentalThreshold
因此,我将其更改为通过构造函数中的候选项:

public abstract class Specification<TEntity> : ISpecification<TEntity>
{
    protected TEntity _candidate;

    public Specification(TEntity candidate)
    {
        _candidate = candidate;
    }

    public bool IsSatisfied()
    {
        return IsSatisfiedBy(_candidate);
    }
}

现在,我想从一位对设计模式更有经验的人那里了解一下,我是否扭曲了这一点,以及我应该注意哪些风险。

您可以在这里找到类似的问题。我和你有同样的问题,我认为有两种适应

我的问题是在一个规范中比较两种不同类型张力的两个对象

现在,我正在尝试将第二个实体添加到ISsatified(T候选)中,如下所示: 满足(T第一候选人,K第二候选人)

因此,规范将成为规范,但我认为我将失去​将规范与规范相结合的能力

到目前为止,我还没有任何关于在规范中添加第二个候选者的好主意

我认为有两种变通方法:

  • 使用参数对象并将此对象用作候选对象:

    public class ParameterObject<T,K>
    {
      public T FirstObject { get; set; }
      public K SecondObject { get; set; {
    }
    
    
    public SomeSpecification: Specification<ParameterObject<Order, Customer>>
    
    public bool IsSatisfied(ParameterObject candidate)
    {
       // do some check between order and customer candidates.
    }
    
    公共类参数对象
    {
    公共T第一对象{get;set;}
    公共K第二对象{get;set{
    }
    公共规范:规范
    公共布尔值已满足(ParameterObject候选)
    {
    //在订单和候选客户之间进行检查。
    }
    
  • 使用构造函数注入:

    public class OutOfRangeOrderSpeficiation: Specification<Order>
    {
       private ICriteria _criteria;
    
        public OutOfRangeOrderSpeficiation(ICriteria criteria)
        {
           _criteria = criteria;
        }
    
        public bool IsSatisfied(Order candidate)
        {
          // comparing between order candidate and _criteria.
          return false;
        }
    }
    
    public类OutofRangeOrderSpecification:规范
    {
    私人ICriteria_标准;
    公共范围外订单规范(ICriteria标准)
    {
    _标准=标准;
    }
    公众满意(候选订单)
    {
    //比较候选订单和_标准。
    返回false;
    }
    }
    

  • 我有同样的问题,有经验的人会来吗?
    public class OutOfRangeOrderSpeficiation: Specification<Order>
    {
       private ICriteria _criteria;
    
        public OutOfRangeOrderSpeficiation(ICriteria criteria)
        {
           _criteria = criteria;
        }
    
        public bool IsSatisfied(Order candidate)
        {
          // comparing between order candidate and _criteria.
          return false;
        }
    }