so I have the following design:

for the models:

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
}

For the ticket manager I have the following

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 
   }

}

I'm confused how I can improve the design, because as it is now it's going to be hard to add in more ticket managers/other types of tickets(I think so at least)

I tried to make an interface to be implemented by the ticket managers:

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

but I can't seem to implement it properly because the Ticket parameter not accepting the child tickets.

also, I'm going for MVC approach here so I'm not even sure if I should be making a ticket manager class and just add those methods to the model. I'm confused as to when you should add methods in a model or put them into their own class.

2 Answers

0
Sathiraumesh On

create an interface for Tickets

public interface Tickets {

}

To have Different Tickets implement the interface

public class ChildTicket1 implemnets ParentTicket{


  //getters and setters here
}


public class ChildTicket2 implemnets ParentTicket{


  //getters and setters here
}

This is the ticket manager class pass the type as the interface type of Tickets

public class TicketManager{

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

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

    }

if you want your attributes you can do

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 
       }

    }

this pattern is called a strategy pattern. If you want to learn more about how to design your applications more learn about design patterns. this is a good reference find it and read it if you want http://shop.oreilly.com/product/9780596007126.do

0
forrert On

You might want to use generics in the ticket manager interface:

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

The key here is to restrict the generic parameter T to be a subclass of ParentTicket. An implementation of this for ChildTicket1 would look like this:

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
    }
}

This approach can be particularly helpful if you need to build an API that gives access to all ticket types, e.g.:

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;
}