Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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.

share|improve this question

4 Answers 4

up vote 3 down vote accepted

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:

createTimeline().withStartEvent().build()

or

createTimeline().withStartEvent().withAnotherDefault("def").andWithSomeAddition(42).build()

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

share|improve this answer
    
That pattern is used for JavaFx, which does use the build() call, so it's definitely possible. –  Mike G Aug 2 '13 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.

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

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

share|improve this answer

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.

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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