- Interfaces should not force classes to implement what they can’t do.
- Large interfaces should be divided into small ones.
E.g. we add decoding feature to the interface.
public interface PasswordHasher
{
String hashPassword(String password);
String decodePasswordFromHash(String hash);
}
This would break this law since one of our algorithms, the SHA256 is not decryptable practically, (it’s a one-way function). Instead, we can add another interface to the applicable classes to implement their decoding algorithm.
public interface Decryptable
{
String decodePasswordFromHash(String hash);
}
public class Base64Hasher implements PasswordHasher, Decryptable
{
@Override
public String hashPassword(String password)
{
return "hashed with base64";
}
@Override
public String decodePasswordFromHash(String hash)
{
return "decoded from base64";
}
}