2

設計パターンは特定のコードではなく、設計によって設定されることを知っていますが、パターンを曲げすぎて設計に従わなくなったのではないかと心配することがあります。

たとえば、仕様パターンは次のようになります。

 public interface ISpecification<T>
{
    bool IsSatisfiedBy(T candidate);
}

しかし、私にはこれはあまり読みにくいです:

 _customerAccountIsActive
.And(_hasReachedRentalThreshold)
.And(_customerAccountHasLateFees)
.IsSatisfiedBy(this); 

そこで、コンストラクター内で候補を渡すように変更しました。

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

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

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

さらに、bool 演算子をオーバーロードしたので、次のように記述できます。

_customerAccountIsActive 
&& _customerAccountHasLateFees
&& _hasReachedRentalThreshold

ここで、設計パターンの経験が豊富な人から、これをひねりすぎていないかどうか、またどのようなリスクに注意する必要があるかを知りたいと思います。

4

1 に答える 1