This is the business requirement:

" A Holiday Package (e.g. New York NY Holiday Package) can be offered in different ways based on the Origin city:

  • From New Delhi to NY
  • From Bombay to NY
  • NY itself ( Land package )

(Bold implies default selection)

a. and b. User can fly from either New Delhi or Bombay to NY.
c. NY is a Land package, where a user can reach NY by himself and is a standalone holidayPackage. "

Let's say I have a class that represents HolidayPackage, Destination (aka City).

public class HolidayPackage{
    Destination holidayCity;
    ArrayList<BaseHolidayPackageVariant> variants;
    BaseHolidayPackageVariant defaultVariant;

public abstract class BaseHolidayPackageVariant {
    private Integer variantId;
    private HolidayPackage holidayPackage;
    private String holidayPackageType;

public class LandHolidayPackageVariant extends BaseHolidayPackageVariant{

public class FlightHolidayPackageVariant extends BaseHolidayPackageVariant{
    private Destination originCity;

What data structure/objects should I design to support:

  • options
  • a default within those options

Sidenote: A HolidayPackage can also be offered in different ways based on Hotel selections. I'd like to follow a design which I can leverage to support that use case in the future.

This is the backend design I have in mind.


Judging from the proposed database schema, you could have a class called HolidayPackage with attributes of type Hotel, City, and PackageVersion.

PackageVersion would be a superclass of LandPackageVersion and FlightPackageVersion. It is declared as the generic PackageVersion so that polymorphism or dependency injection can be used to populate it with the appropriate subclass.

public Class HolidayPackage{

    Hotel hotel;
    City city;
    PackageVersion package;  //could be an instance of LandPackageVersion or FlightPackageVersion


OK, now that you have edited your question to show your class structure, I can see you already have it organized how I was recommending above. So this should work fine as long as you have methods that tell you information about each BaseHolidayPackageVariant in the variants ArrayList, such as whether or not there is a flight associated with it.

Oversimplified example:

public abstract class BaseHolidayPackageVariant {

    public boolean hasFlight(){}

public class LandHolidayPackageVariant extends BaseHolidayPackageVariant{

    public boolean hasFlight(){
        return false;

public class FlightHolidayPackageVariant extends BaseHolidayPackageVariant{

    public boolean hasFlight(){
        return true;

So, at runtime, you don't really need to use instanceOf() because you'll know everything you need to know about the Objects using these descriptive methods.

  • Please take a look at my edit. Apart from handling options and default, I'm faced with a concern: At runtime, how can I know if a given Object in variants[] is of Type LandPackageVariant or FlightPackageVariant ? – brainydexter Mar 22 '12 at 12:54
  • 3
    @brainydexter You can use Java's instanceOf() to know an Object's type at runtime. However, you should try to design it so it doesn't matter what type of object it is. This can be achieved by effectively overriding methods. – CFL_Jeff Mar 22 '12 at 14:06
  • I agree with that article (I had actually read the very same Scott Meyer's article against instanceOf()). For the very same reason, I am trying to come up with a better design to the problem I asked in my question. – brainydexter Mar 22 '12 at 14:15
  • @brainydexter See my edit. – CFL_Jeff Mar 22 '12 at 14:46

Based on your current implementation it sounds like the holiday package should contain a travel mechanism (air, land, from, to), a place to stay, and also what you are currently calling the base package. Your options should be a list of holiday packages that contain different combinations of the travel, hotel, and base package properties.

If the presented packages are supposed to have a default you can simply add a integer value that is the sort/rank index and then decide that the default is the one with the lowest or highest number.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.