Situation:
I am implementing DDD in my first project and I would like to clarify how to correctly implement strategy pattern (in my case check if API token is active). This is a business rule of ApiToken instance but there could be various implementations of this strategy so I would like to delegate this logic to external implementation.
Implementation:
ApiToken
class:
public abstract class ApiToken {
public abstract boolean isActive(TokenActiveStrategy strategy);
}
and this is token strategy contract:
public interface TokenStrategy {
boolean isActive(final ApiToken token);
}
And usage:
TokenStrategy tokenStrategy = new DatabaseTokenStrategy();
if (!token.isActive(tokenStrategy)) {
throw new AuthenticationException(ExceptionCodeType.TOKEN_NOT_ACTIVE);
}
and concrete ApiToken class:
@Override
public boolean isActive(final TokenStrategy strategy) {
return strategy.isActive(this);
}
Question:
Is it OK with respect to principles of DDD to delegate checking of domain business rule out of domain object?