Video in progress

Which SOLID principle does this code break, and how would you fix the design?

interface Repository<T> {
    T find(long id);
    void save(T entity);
}

class ArchiveRepository implements Repository<Invoice> {
    public Invoice find(long id) { return archive.load(id); }
    public void save(Invoice i) {
        throw new UnsupportedOperationException("Arxiv faqat o'qiladi");
    }
}

It breaks Liskov: ReadOnlyRepository doesn't perform the save that the base contract promises and throws an exception at runtime. If we split the interface into read and write (ISP), the problem moves to compile time.

📖 Answer

Any code that accepts a Repository<Invoice> assumes save works. If you hand it an ArchiveRepository, the error only shows up in production, on a particular path. This is the classic Liskov violation: the subtype weakened the promise of the parent type.

The root cause is that the interface is too broad (ISP). We split it by capability:

interface ReadRepository<T>  { T find(long id); }
interface WriteRepository<T> { void save(T entity); }
interface Repository<T> extends ReadRepository<T>, WriteRepository<T> {}

class ArchiveRepository implements ReadRepository<Invoice> { ... }

Now you can't pass the archive to a place that needs to write: the compiler stops it.

The JDK has this problem itself: List.of() implements the List interface, but if you call add you get an UnsupportedOperationException. That was a deliberate compromise (to simplify the collections hierarchy), but avoid "optional operations" like that in your own domain.

Look for this in reviews: UnsupportedOperationException or an empty overridden method almost always means the interface was cut wrong.