java 接口中如何声明类似于重载的方法?
大家讲道理
大家讲道理 2017-04-17 17:49:13
0
1
498

我们的作业要写一个web宠物医院管理系统,因为医生、客户等都包含浏览、添加、删除操作,所以对于service层,我希望有个统一的接口以供servlet中的类使用。

开始我是这样定义的:

public interface ServiceManager<T extends PetHospital> {
    boolean addOne(T arg);

    boolean deleteOne(int id);

    List<T> list(int page);
}

这样删除和浏览都没有问题,但是addOne(T arg)方法会造成在servlet层的类需要创建Hippiaterbean层里面声明的类,感觉这样做好像不太符合分层的设计,但是对于医生、客户、宠物等不同对象需要的参数是不一样的,我该怎么设计addOne方法来保证servlet层调用时只需要传这些对象需要的参数,而不是这些对象。


可能描述的不清楚,再解释一下:

我希望在接口里还是要声明addOne方法,但是能够接收不同参数(个数、类型都不一定),这样在service层还是可以优雅的实现这个接口。

对象及其属性包含的内容如下:

package bean;
//兽医
class Hippiater {
  private int id;//primary key
  private String name;//姓名
  private int workAge;//医龄
  private String speciality;//技能专长
}
//客户
class Customer {
  private int id;//primary key
  private String name;//姓名
}
//宠物
class Pet {
  private int id; //primary key
  private int masterId; //所属客户id
  private String breed; //品种
  private String name; //名字
}
//病历
class Record {
  private int id; //primary key
  private int petId; //宠物ID 
  private long recordTime; //病历创建日期
  private String diseaseDescribe; //病情描述
}

目录结构如下:

root
+- bean
+- DAO
+- service
`- web

上面的几个类在bean目录下,ServiceManager接口位于service目录下,为web目录下的类提供服务。
但我觉得应该不能让web下的类去操作bean目录下的类的实例,而应该提供其所需参数,交给service去处理
所有我的问题就是怎么设计我的ServiceManager接口来满足这个需求

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all(1)
小葫芦

Let me tell you a few questions first

  1. You use a type parameter T to inherit PetHospital. The type of T may be Hippiater, Customer, Pet, etc. This inheritance method violates the "is-A" semantics of inheritance. Imagine that a customer or a pet is a PetHospital?

  2. Web Zeng can certainly understand the objects in the bean layer, because the parameters of your web layer generally come from the form, so naturally you must know what object the form is for. Controllers in struts or spring are used directly and encapsulate form parameters into bean layer objects.

  3. The delete method only accepts an id, so how do you know what object needs to be deleted? Is it a customer? Or a pet? What if these two have the same id?

If you must use a generic type, refer to the following code

public class Entity {
    private String id;
    //omit getter and setter
}

public class Pet extends Entity{
    private String name;
}

public interface EntitiyService<T extends Entity> {
    boolean addOne(T arg);

    boolean deleteOne(int id);

    List<T> list(int page);
}

public class EntityServiceImpl<T extends Entity> implements EntitiyService<T> {
    @Override
    public boolean addOne(T arg) {
        System.out.println("save entity " + arg.toString());
        return true;
    }

    @Override
    public boolean deleteOne(int id) {
        System.out.println("delete entity which type is T");
        return false;
    }

    @Override
    public List<T> list(int page) {
        System.out.println("query first " + page + " pages result");
        return null;
    }
}

public class Client {
    public static void main(String[] args) {
        Pet petFromForm = new Pet();
        EntityServiceImpl<Pet> entityService = new EntityServiceImpl<>();
        entityService.addOne(petFromForm);
        entityService.deleteOne(1);
        entityService.list(3);
    }
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!