I have a class called Timeline. I want to allow several defaults in my code, such as a Timeline with a Start event (The details are not needed. All that matters is that I have a class, and I want to be able to have a few different default setting). Here are the options I thought of:

1) Following a GUI/Swing inspired idea, I might want to subclass the Timeline class and, in its constructor, use the public functions to set the default. Then, I could later just instantiate those objects instead of the superclass

2) A factory class could have functions like "getTimeLineWithExplosion" and set everything up.

Which of those two is the better idea? Is there a design pattern better than both of them?

PS: This is implemented in Java, but could easily be in another language.

4 Answers 4


About your options.

I don't think subclassing here is a good idea. As I see you don't follow the "is - a" relationship here. If they have slightly different defaults - than it is different objects of the same class, but not different classes, since they don't have different behavior.

Factory option looks much better. You don't need to create new class for each new defaults you want. And factory abstracts the underlying logic nicely.

I propose you another idea. Implement something like "Builder" pattern, to achive constructions like this:




I don't know Java well, but I sure this could be implemented, and won't be significantly harder then factory, but readability would be better (as for me).

If Java allows a way to declare implicit type conversions - you could end up without that build() call

  • That pattern is used for JavaFx, which does use the build() call, so it's definitely possible.
    – sinθ
    Commented Aug 2, 2013 at 22:36

Look into the Prototype pattern.

Basically, create a base class with a virtual Clone method. For every set of defaults, create a subclass.

-- TimelineWithExplosions.Clone();
-- TimelineWithLasers.Clone();

Make the Clone method static and each child becomes its own factory.


In laymans' words:

  • As the things you want to set are already exposed with public methods, you don't need to subclass.

  • Even if those things were not exposed, changing the value of protected members is not reason enough for subclassing. You should have another reason like needing to change some behavior.

  • The factory pattern ( 2nd option ) is a better option. There are some other creational patterns that you may want to consider but Factory is OK.


Yes, 2nd approach is better. Here are other options. You can go for prototype pattern. Extra thing which will be required is that timeline class should implement Clone able, and then depending on timeline class I will do Shallow cloning or deep cloning.

If prototype option is too much, then you can go for builder Pattern. This approach will be more cleaner.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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