Specification pattern: Difference between revisions

Content deleted Content added
Python: added python language tag
Python: cleaner source code, matches other examples in is_satisfied_by
Line 230:
from abc import abstractmethod
from dataclasses import dataclass
from typing import Any
 
 
class BaseSpecification:
@abstractmethod
def is_satisfied_by(self, candidate: Any) -> bool:
raise NotImplementedError()
 
def _andand_(self, other: "BaseSpecification") -> "AndSpecification":
return AndSpecification(self, other)
 
def _oror_(self, other: "BaseSpecification") -> "OrSpecification":
return OrSpecification(self, other)
 
def _notnot_(self) -> "NotSpecification":
return NotSpecification(self)
 
Line 252 ⟶ 253:
second: BaseSpecification
 
def is_satisfied_by(self, candidate: Any) -> bool:
return self.first.is_satisfied_by(candidate) and self.second.is_satisfied_by(candidate)
 
 
Line 261 ⟶ 262:
second: BaseSpecification
 
def is_satisfied_by(self, candidate: Any) -> bool:
return self.first.is_satisfied_by(candidate) or self.second.is_satisfied_by(candidate)
 
 
Line 269 ⟶ 270:
subject: BaseSpecification
 
def is_satisfied_by(self, candidate: Any) -> bool:
return not self.subject.is_satisfied_by(candidate)
 
 
</source>