2

From Design Pattern by Gang of Four, enter image description here

Example:

enter image description here

Why doesn't the interface Builder have a method GetResult(), which is overridden in the concrete class ConcreteBuilder?

In the example, the concrete builders have GetXXX() methods, where XXX is different for different concrete builders, which doesn't promote "programming to interface" at all.

Is this "omission" deliberate in the builder pattern?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Tim
  • 1
  • 141
  • 372
  • 590
  • Only thing that comes to mind immediately is that since there weren't generics back then the thing that came back would always have to be cast anyway, which makes an interface method not as useful. A `BuiltThing` interface on its own probably isn't super-useful – Dave Newton Sep 29 '17 at 19:31
  • The book was written in C++ not Java. C++ had template then. – Tim Sep 29 '17 at 19:32
  • `GetResult` is the function related to ConcreteBuilder. The name here is deceiving. There can be another builder `WoodBuilder` that may have its own function `Manufacture()`. – Arun Sep 29 '17 at 19:32
  • @Tim Okay, but as a general pattern, I'm still not sure an "interface" method would be helpful. ¯\(°_o)/¯ – Dave Newton Sep 29 '17 at 19:33
  • @Arun The builder should return the result in some way, so it's not unreasonable to expect a method which would return the result in the `Builder` interface. – lexicore Sep 29 '17 at 19:36
  • @DaveNewton Even without generics all "things" could implement some common interface. If "parts" are abstract enough to be represented via obviously useful `BuildPart()` method in the interface, I don't see why a common `BuildThing` can't be useful to represent "things". But, still, this is the best explanating so far. – lexicore Sep 29 '17 at 19:40
  • @lexicore Thanks. Besides "omission", the book sometimes has some unexpected "addition". See https://stackoverflow.com/questions/46480156/what-are-the-purposes-of-other-members-of-a-singleton-class-besides-the-instance – Tim Sep 29 '17 at 19:45
  • @lexicore I think what I mean is that in a specific system you'd probably want a `BuiltThing` interface, e.g., if you're returning UI elements that have some similarity. Then you'd have a class living between the interface and the system that implements that system-/usage-specific interface. I'm not sure, really. – Dave Newton Sep 29 '17 at 19:46

1 Answers1

2

Yes, the omission is deliberate. The book addresses it directly.

Why no abstract class for products? In the common case, the products produced by the concrete builders differ so greatly in their representation that there is little to gain from giving different products a common parent class. Because the client usually configures the director with the proper concrete builder, the client is in a position to know which concrete subclass of Builder is in use and can handle its products accordingly.

Regarding the comments to the OP, generics could assist the client in handling products by enabling a GetResult() method with a generic return type in the Builder interface.

jaco0646
  • 15,303
  • 7
  • 59
  • 83