本文介绍了在生成器模式中,方法`buildpart()`是工厂方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在构建器设计模式中,

方法buildpart()是工厂方法吗? (为了进行比较,抽象工厂是工厂方法的集合.)

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?

Gamma等人的《设计模式》中的buildpart()的示例是:

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

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

推荐答案

工厂方法通常使用new关键字创建具体的对象/产品,然后返回.通常,工厂方法的名称以前缀create开头.

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.

在Builder模式的上下文中,buildpart()方法可以创建一些对象,但是不返回该对象.从Director的角度来看,它不需要接收由buildpart()方法创建的零件.导演只是想指示建造者按照需要的顺序制造零件,并最终获得最终产品.

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.

简而言之,由于createbuild之间的差异,buildpart()方法不是工厂方法.

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

这篇关于在生成器模式中,方法`buildpart()`是工厂方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 09:15