抽象类与成员也是抽象的

时间:2015-05-15 17:41:04

标签: java oop inheritance abstract-class

我有一个抽象类,用于定义一些将在子类之间共享的抽象方法(这是我理解为抽象类的正确用法。因为这对于一组非常相似的类我决定使用抽象类而不是接口。)

public abstract class DomainService<T extends Domain> {

    abstract public void insert(T object) throws ValidationException;

    abstract public void delete(T object) throws EntityNotFoundException;

    abstract public List<T> fetchAll();
}

T是与之相关的域类,并且所有域类都有一个名为Domain

的公共超类
public abstract class Domain implements Serializable {

    private static final long serialVersionUID = 8868922239517301371L;

    private static Logger log = LoggerFactory.getLogger(Domain.class);

    public Domain() {
    }

    public List<String> getColumnMembers() {
        Field[] declaredFields = this.getClass().getDeclaredFields();

        List<String> declaredFieldsNames = new ArrayList<String>(declaredFields.length);
        for (Field field : declaredFields) {
            if (Modifier.isStatic(field.getModifiers()) || !field.isAnnotationPresent(Column.class)) {
                continue;
            }
            declaredFieldsNames.add(field.getName());
        }

        return declaredFieldsNames;
    }

    public abstract Object getPrimaryKey();
}

每个Domain都有一个DomainAccess类,可以访问数据库中的表,所有这些类都有一个共同方法的名为DomainAccess的公共超类

public abstract class DomainAccess<T extends Domain> {

    protected abstract Logger getLogger();

    protected DatabaseFacade db;

    protected Class<T> domainClass;

    @Inject
    public DomainAccess(DatabaseFacade databaseFacade, Class<T> domainClass) {
        this.db = databaseFacade;
        this.domainClass = domainClass;
    }

    @SuppressWarnings("unchecked")
    public T fetchByPrimaryKey(Object primaryKey) {
        return (T) db.find(domainClass, primaryKey);
    }

    public boolean exists(T object) {
        return fetchByPrimaryKey(object.getPrimaryKey()) == null ? false : true;
    }

    public void save(T object) {
        db.save(object);
    }

    public void merge(T object) {
        db.merge(object);
    }

    public void delete(T object) {
        db.remove(object);
    }

    public void saveOrUpdate(T object) {
        if (exists(object)) {
            merge(object);
        } else {
            save(object);
        }
    }

    public void deleteByPrimaryKey(T object) throws EntityNotFoundException {
        Object primaryKey = object.getPrimaryKey();
        T objectToDelete = fetchByPrimaryKey(primaryKey);

        if (objectToDelete != null) {
            getLogger().debug("There was no entry found with primary key: " + primaryKey);
            throw new EntityNotFoundException("No entry was found with specified primary key [" + primaryKey + "]");
        } else {
            getLogger().debug("Deleting entry with id: " + primaryKey);
            delete(objectToDelete);
        }
    }

    @SuppressWarnings("unchecked")
    public List<T> getResultList(String hql, String... parameters) {
        TypedQuery<T> query = db.createTypedQuery(hql, domainClass);
        for (int i = 0; i < parameters.length; i++) {
            query.setParameter(i + 1, parameters[i]);
        }
        return query.getResultList();

    }

    @SuppressWarnings("unchecked")
    public T getSingleResult(String hql, String... parameters) {
        TypedQuery<T> query = db.createTypedQuery(hql, domainClass);
        for (int i = 1; i <= parameters.length; i++) {
            query.setParameter(i, parameters[i - 1]);
        }

        return query.getSingleResult();
    }
}

除了您对此处所看到的任何评论之外,我的问题如下:

DomainService中的delete方法对于每个子类都是相同的,即

UserService:
    @Transactional
    @Override
    public void delete(User user) throws EntityNotFoundException {
        userAccess.deleteByPrimaryKey(user);
    }
DepartmentService:
    @Transactional
    @Override
    public void delete(Department department) throws EntityNotFoundException {
        departmentAccess.deleteByPrimaryKey(department);
    }

所以我想把它移到抽象的父类。但要做到这一点,我必须在那里定义DomainAccess,这是猜测

public abstract class DomainService<T extends Domain> {

    protected DomainAccess<T> domainAccess;

    protected DomainService(DomainAccess<T> domainAccess) {
        this.domainAccess = domainAccess;
    }

    abstract public void insert(T object) throws ValidationException;

    public void delete(T object) throws ValidationException {
        domainAccess.deleteByPrimaryKey(user);
    }       
    abstract public List<T> fetchAll();
}

在子类构造函数

中传递DomainService
@Inject
public UserService(UserAccess userAccess) {
    super(userAccess);
    ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
    this.validator = factory.getValidator();
}

但如果我这样做,那么在子类中,让我们说UserService是我的方法

@Override
public List<User> fetchAll() {
    return userAccess.fetchAllUsers();
}

必须变得像这样

@Override
public List<User> fetchAll() {
    return ((UserAccess)domainAccess).fetchAllUsers();
}

每当我使用特定方法到子类时,都必须这样做。

这似乎不对。是否有任何指针或思考方式可以使用泛型或抽象多少?

0 个答案:

没有答案