I'm defining a class structure for persisting to our cassandra database, and I'm unsure about using a combination of an abstract class and an interface. I have two concrete classes, one for persisting reports and the other for persisting configurations. They both have separate entity classes defined else where that the entityMapper
uses. I have read this post which is very similar, but I don't think their example really highlights what I want to know.
My structure looks like this:
Interface
public interface CassandraRepository<T> {
void save(T object);
void delete(T object);
}
Abstract class
import com.datastax.driver.core.Session;
import com.datastax.driver.mapping.Mapper;
import com.datastax.driver.mapping.MappingManager;
abstract class AbstractGenericRepository<T> implements CassandraRepository<T> {
private final Session sesion;
Mapper<T> entityMapper;
AbstractGenericRepository(final Session session) {
this.session = session;
final MappingManager manager = new MappingManager(this.session);
this.entityMapper = manager.mapper(getRepositoryClass());
}
protected abstract Class<T> getRepositoryClass();
}
Configuration class
import com.model.Configuration;
import com.datastax.driver.core.Session;
public class ConfigurationRepository extends AbstractGenericRepository<Configuration> {
public ConfigurationRepository(final Session session) {
super(session);
}
@Override
public void delete(final Configuration object) {
this.entityMapper.delete(object);
}
@Override
public void save(final Configuration object) {
this.entityMapper.save(object);
}
@Override
protected Class<Configuration> getRepositoryClass() {
return Configuration.class;
}
}
Report class
import com.model.Report;
import com.datastax.driver.core.Session;
public class ReportRepository extends AbstractGenericRepository<Report> {
ReportRepository(Session session) {
super(session);
}
@Override
public void save(Report object) {
this.entityMapper.save(object);
}
@Override
public void delete(Report object) {
this.entityMapper.delete(object);
}
@Override
protected Class<Report> getRepositoryClass() {
return Report.class;
}
}
My question is: is there a point of having the interface at all? Could I simply define both the save
and delete
methods as abstract methods in the abstract class itself, and eliminate the interface.
I've also realised while typing this up that I could just move both the save and delete implementations into the abstract itself since they do the same thing. But in a case where they each implemented the methods differnetly, is there any benefit to having the interface at all?
Why could I not always use an abstract class over an interface, since it allows you to provide abstract methods that must be implemented in subclasses (like an interface), but then also allows you to defining common code implementations between them (which interfaces do not allow)?
EDIT
In response to the post being tagged as a possible duplicate, the other post doesn't really clarify anything for me, specifically in the case of if an abstract class should implement an interface, and the pros/cons of doing so