Specification pattern: Difference between revisions

Content deleted Content added
Added Python example
Line 224:
public override bool IsSatisfiedBy(T candidate) => !other.IsSatisfiedBy(candidate);
}
</source>
 
=== [[Python (programming language)|Python]] ===
<source>
from abc import abstractmethod
from dataclasses import dataclass
 
 
class BaseSpecification:
@abstractmethod
def is_satisfied_by(self) -> bool:
raise NotImplementedError()
 
def _and(self, other: "BaseSpecification") -> "AndSpecification":
return AndSpecification(self, other)
 
def _or(self, other: "BaseSpecification") -> "OrSpecification":
return OrSpecification(self, other)
 
def _not(self) -> "NotSpecification":
return NotSpecification(self)
 
 
@dataclass(frozen=True)
class AndSpecification(BaseSpecification):
first: BaseSpecification
second: BaseSpecification
 
def is_satisfied_by(self) -> bool:
return self.first.is_satisfied_by() and self.second.is_satisfied_by()
 
 
@dataclass(frozen=True)
class OrSpecification(BaseSpecification):
first: BaseSpecification
second: BaseSpecification
 
def is_satisfied_by(self) -> bool:
return self.first.is_satisfied_by() or self.second.is_satisfied_by()
 
 
@dataclass(frozen=True)
class NotSpecification(BaseSpecification):
subject: BaseSpecification
 
def is_satisfied_by(self) -> bool:
return not self.subject.is_satisfied_by()
 
</source>