Search code examples
javagenericsinheritanceumlclass-diagram

Interfaces, inheritance and Genrics in UML Class Diagram


I'm modeling a persistence layer based on DAO pattern and I have some doubts related to the use of Genrics and interfaces inheritance.

I'd start saying that basic CRUD operations are the same for all the persistable entities so I've decided to provide a basic DAO interface using Generics. Then there would be other interfaces that inherit the basic contract plus some other specific methods related to the entity that will be managed by the concrete implementation.

It may sound complicated but it's actually very simple. In Java it would be something like this:

public interface BasicDao<T> {

    public T insert(T dataObject);

    public T update(T dataObject);

    public Boolean delete(T dataObject);

    public T getUnique(BigInteger dataObjectId);

    public List<T> getAll();
}

public interface IModulesDao extends IBasicDao<IModule> {

    public IModule getModuleByCode(String code);
}

As you can see IModulesDao adds a single method to the basic contract but concrete implementations still have to meet the whole contract, which is the idea of doing all this.

Now I've read UML Class Diagram and Generics about how to model Generics in UML and also this example. It is stated that using a buond element is not the same than subtyping and we are not allowed to add anything to the contract. So as far as I understand it, I would have to create intermediate types to finally get my IModulesDao interface.

This sounds conceptually right but I'm not sure if fits in my case since IModulesDao is just an extension or sub-interface of IBasicDao and makes no sense to have an intermediate subtype to inherit from. IMHO it will add just noise to the diagram for no benefit.

This is what I have right now but don't think is entirely correct:

Class diagram

Could you please lead me in the right direction about how to model this case?


Solution

  • You could use IModulesDao without adding the new function, so just binding the generic. So it should be named similar to your List<T> as IBasicDao<T>. Then using this bound class you can create a specialization that actually adds the new method and you can call that specialized class IModulesDao.

    But honestly nobody needs to be more christian than the Pope. UML lets you quite some freedom and if its understandable to the reader then its fine. Having said that you could also live nicely with the way you did it.