In builder pattern, is method `buildpart()` a factory method?

烂漫一生 提交于 2019-12-14 03:16:22

问题


In builder design pattern,

is method buildpart() a factory method? (For comparison, an abstract factory is a collection of factory methods.)

Why are they (not) factory methods? For clarification, could you also provide the definition of a factory method?

An example for buildpart() from Design Patterns by Gamma et al is:

void StandardMazeBuilder::BuildMaze  () {
_currentMaze  = new Maze;
}

回答1:


A factory method creates a concrete object/product, usually with the new keyword, and returns that object. Typicall the name of a factory method starts with prefix create.

The buildpart() method in the context of Builder Pattern may create some object, but does not return that object. From the perspective of the Director, it does not need to receive the part created by the buildpart() method. The Director just wants to instruct the Builder to build parts in a wanted order, and finally receive the final product.

In short, due to the difference between create and build, a buildpart() method is not a factory method.



来源:https://stackoverflow.com/questions/56509460/in-builder-pattern-is-method-buildpart-a-factory-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!