wood burning stoves 2.0*
The moose likes OO, Patterns, UML and Refactoring and the fly likes the Builder Pattern Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "the Builder Pattern" Watch "the Builder Pattern" New topic
Author

the Builder Pattern

Nichole Kim
Greenhorn

Joined: Sep 12, 2002
Posts: 12
Hi,
I am reading some tutorial re the Builder Pattern. In the example, the author declared an abstract class, and then two derived classes which are Builders, one factory class which is called Director. I have no problem with this structure. My question is: can we declare an interface instread of the abstract class for the builders? When to declare an interface, when to declare an abstract class?
Thank you!
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
You possibly can use an interface, yes.
You use an abstract class, if there is some implementation you want to reuse between subclasses (for example for the Template Method pattern).
You use an interface if you want to have the option for multiple inheritance.
If neither of the above is true for you, throw a coin...


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Nichole Kim
Greenhorn

Joined: Sep 12, 2002
Posts: 12
I see. Thank you!
Sayed Ibrahim Hashimi
Ranch Hand

Joined: May 17, 2001
Posts: 148
When you use Builder you can use interfaces but you will probably want to use Abstract classes instead that way you can give the methods a default (empty) implementation.
Here is the reason which the example in the GoF book fails to show this, because of its simplicity. When you use Builder typically the Builder class will have lots of methods. So if you just declare an interface then all of your ConcreteBuilder classes will have to implement all of those methods, some of which don't even make sense for the particular ConcreteBuilder class. Not only are you spending more time re-writing code that you shouldn't it is kinda confusing because if I'm looking at a ConcreteBuilder class and I see that you have implemented A(), B() and C() then I'm going to expect that they all make sense for that class. Where as if that was in a super-class I would pay less attention to it.
Hope this helps.


SCJP 1.4<br /><a href="http://www.cise.ufl.edu/~sih" target="_blank" rel="nofollow">www.cise.ufl.edu/~sih</a>
bparanj
Greenhorn

Joined: May 27, 2002
Posts: 27
One more deciding factor to help you decide whether to use interface or abstract class is :
Interface does not force the static structural relationship like abstract class. You could implement an interface anywhere in the hierarchy. Also note using abstract class might enforce semantic constraints whereas interfaces do not. Check out the article : http://www.javaworld.com/javaworld/jw-12-1998/jw-12-techniques_p.html
for more on this.
Bala


Moderator : <a href="http://groups.yahoo.com/group/OOAD_UML/" target="_blank" rel="nofollow">http://groups.yahoo.com/group/OOAD_UML/</a> <br />Home : <a href="http://www.zepho.com" target="_blank" rel="nofollow">http://www.zepho.com</a>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: the Builder Pattern
 
Similar Threads
how to extend a static builder
Factory Method and Builder
Eclipse build source folders / class path folders
practical use of Builder pattern?
Factory Vs Abstract Factory Pattern