继承和接口设计改进

时间:2019-05-11 13:42:29

标签: java inheritance interface

所以我有以下设计:

对于型号:

public class ParentTicket{
  protected ticketAttribute1
  protected ticketAttribute2
  protected ticketAttribute3

  //getters and setters here
}

public class ChildTicket1 extends ParentTicket{
  private childTicket1Attr1
  private childTicket1Attr2
  private childTicket1Attr3

  //getters and setters here
}

public class ChildTicket2 extends ParentTicket{
  private childTicket2Attr1
  private childTicket2Attr2
  private childTicket2Attr3

  //getters and setters here
}

对于票务经理,我有以下内容

public class TicketManager1{

   public ArrayList<ChildTicket1> getAlltickets(){ 
       //some implementation 
   }

   public void saveTicketToDB(ChildTicket1 ticket){ 
       //some implementation 
   }

}

public class TicketManager2{

   public ArrayList<ChildTicket2> getAlltickets(){ 
       //some implementation 
   }

   public void saveTicketToDB(ChildTicket2 ticket){ 
       //some implementation 
   }

}

我对如何改进设计感到困惑,因为从现在开始,很难添加更多的票务经理/其他类型的票证(至少我是这么认为的)

我试图创建一个由票务管理者实现的接口:

public interface IManageTickets{
  public Collection getAllTickets();
  public void saveTicketToDB(Ticket ticket);
  //some other methods
}

但是我似乎无法正确实现它,因为Ticket参数不接受子票证。

此外,我将在这里使用MVC方法,因此甚至不确定我是否应该制作票务管理器类并将这些方法添加到模型中。对于何时应该在模型中添加方法或将其放入自己的类中,我感到困惑。

2 个答案:

答案 0 :(得分:0)

为票证创建界面

public interface Tickets {

}

要让不同的票证实现该接口

public class ChildTicket1 implemnets ParentTicket{


  //getters and setters here
}


public class ChildTicket2 implemnets ParentTicket{


  //getters and setters here
}

这是票证管理器类,将类型作为票证的接口类型

public class TicketManager{

       public ArrayList<Tickets> getAlltickets(){ 
           //some implementation 
       }

       public void saveTicketToDB(Tickets ticket){ 
           //some implementation 
       }

    }

如果您希望自己的属性可以做

public class Tickets {
 protected ticketAttribute1
 protected ticketAttribute2
 protected ticketAttribute3
}

public class ChildTicket1 extends ParentTicket{


  //getters and setters here
}


public class ChildTicket2 extends ParentTicket{


  //getters and setters here
}





public class TicketManager{

       public ArrayList<Tickets> getAlltickets(){ 
           //some implementation 
       }

       public void saveTicketToDB(Tickets ticket){ 
           //some implementation 
       }

    }

此模式称为策略模式。如果您想了解更多有关如何设计应用程序的信息,请更多地了解设计模式。 如果您想http://shop.oreilly.com/product/9780596007126.do

,可以找到并阅读它是一个很好的参考

答案 1 :(得分:0)

您可能想在票务管理器界面中使用泛型:

public interface TicketManager<T extends ParentTicket> {
    Collection<T> getTickets();
    void saveTicketToDb(T ticket);
}

此处的关键是将通用参数T限制为ParentTicket的子类。 ChildTicket1的此实现如下所示:

public class TicketManager1 implements TicketManager<ChildTicket1> {
    @Override
    public Collection<ChildTicket1> getTickets() {
        // implementation goes here
        return null;
    }

    @Override
    public void saveTicketToDb(ChildTicket1 ticket) {
        // implementation goes here
    }
}

如果您需要构建可访问所有票证类型的API,例如:

,此方法特别有用。
public Collection<ParentTicket> getAllTickets() {
    List<TicketManager<? extends ParentTicket>> managers; // contains a list of all TicketManager implementations
    List<ParentTicket> tickets = new ArrayList<>();
    for (TicketManager<? extends ParentTicket> manager : managers) {
        tickets.addAll(manager.getTickets());
    }
    return tickets;
}