Builder design pattern: Why do we need a Director?

后端 未结 7 1973
梦如初夏
梦如初夏 2020-12-13 18:10

Recently I\'ve come across the Builder design pattern. It seems that different authors use \"Builder pattern\" to refer to different flavours, so let me describe the pattern

7条回答
  •  悲&欢浪女
    2020-12-13 18:12

    The core portion of the Builder pattern concerns the Abstract Builder and its subclasses (concrete builders). According to GoF's Design Patterns, director simply "notifies the builder whenever a part of the product should be built", which can be perfectly done by the client.

    The StringBuilder class in the Java API is an example of a builder without the respective director -- typically the client class "directs" it.

    Also, in Effective Java and Creating and Destroying Java Objects, Joshua Bloch suggests the use of the builder pattern, and he does not include a director.

提交回复
热议问题