【发布时间】:2014-03-20 05:53:50
【问题描述】:
我的问题是:人们为什么这样做:
界面:
public interface CarDAO {
void addCar(Car car);
List<Car> readAll();
void deleteCar(Long id);
}
创建一个实现 carDAO 的类
public class CarDAOImpl implements CarDAO {
private SessionFactory sessionFactory;
private Session getCurrentSession(){
return sessionFactory.getCurrentSession();
}
public void addCar(Car car) {
all the code to add the car
}
public void deleteCar(Car car) {
all the code to delete the car
}
为什么不直接创建一个不带接口的 carCRUDclass?
【问题讨论】:
-
接口的一个被低估/经常被忽视的特性是,使用它们最大限度地减少对暴露子类型关系的依赖 :)