Skip to content

Latest commit

 

History

History
42 lines (34 loc) · 1015 Bytes

04-i.md

File metadata and controls

42 lines (34 loc) · 1015 Bytes

I - Interface Segregation

  1. Interfaces should not force classes to implement what they can’t do.
  2. 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";
    }
}