In builder pattern, is method `buildpart()` a fact

2019-08-30 04:15发布

In builder design pattern,

enter image description here

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条回答
▲ chillily
2楼-- · 2019-08-30 05:07

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.

查看更多
登录 后发表回答