GOF can be a bit confusing as you have factory and factory method. Never sure which one is which. Without the full text it's hard to tell which you're talking about.
Normally only the type of the factory you create determines what is returned by the methods, e.g. if you have a pizza factory the different methods return different pizzas. But the factory determines which ones to create: a specific type for each method. This is of course limited in one way since you don't have any influence over what is created apart from choosing which factory to use, but in the frameworks where you use this variant that's the whole idea. The factory takes the options out of your hands so you don't have the responsibility to choose. Basically you say 'give me whatever you have'. E.g. give me any pepperoni pizza. But the factory can return a New York pepperoni pizza, or a extra thin pepperoni pizza, depending on which factory you're using.
There are situations where the factory will work more like a strategy. You don't want to know what it does, but you still want to be able to tell it which type you'd like to receive. The type should either have an interface the factory can work on, or the factory itself should be so simple it doesn't need an interface. Here you say 'give me an instance of this type, do whatever you normally do with an instance'. E.g. I want this here pizza, with extra cheese.
I think that they are talking about inherited types. So if you have product as a parent class and 2 child classes BetaProduct and FinishedProduct. You can tell the factory what kind of object to create, a BetaProduct or a FinishedProduct by simple passing in some sort of parameter that changes the control flow within the factory. It will always set the values on the object based on the information the factory has...but when it talks about KIND of product...that is the name/type of the child class.
Your factory could look up a value from the database or config file itself, however this is more likely to be done outside of the factory then passed in.
发布评论
评论(3)
GOF 可能有点令人困惑,因为你有工厂和工厂方法。永远不确定哪个是哪个。没有全文,很难分辨你在说什么。
通常,只有您创建的工厂的类型决定了方法返回的内容,例如,如果您有一个披萨工厂,则不同的方法返回不同的披萨。但工厂决定创建哪些:每个方法的特定类型。这当然在某种程度上受到限制,因为除了选择使用哪个工厂之外,您对创建的内容没有任何影响,但在使用此变体的框架中,这就是整个想法。工厂把选择权从你手中夺走了,所以你没有选择的责任。基本上你会说“给我你拥有的一切”。例如,给我任何意大利辣香肠披萨。但工厂可以退回纽约意大利辣香肠披萨或超薄意大利辣香肠披萨,具体取决于您使用的工厂。
在某些情况下,工厂的运作更像是一种策略。您不想知道它的作用,但您仍然希望能够告诉它您想要接收哪种类型。该类型应该具有工厂可以使用的接口,或者工厂本身应该非常简单,不需要接口。这里你说“给我一个这种类型的实例,做你通常对实例做的事情”。例如,我想要这个披萨,加额外的奶酪。
希望这有帮助。
GOF can be a bit confusing as you have factory and factory method. Never sure which one is which. Without the full text it's hard to tell which you're talking about.
Normally only the type of the factory you create determines what is returned by the methods, e.g. if you have a pizza factory the different methods return different pizzas. But the factory determines which ones to create: a specific type for each method. This is of course limited in one way since you don't have any influence over what is created apart from choosing which factory to use, but in the frameworks where you use this variant that's the whole idea. The factory takes the options out of your hands so you don't have the responsibility to choose. Basically you say 'give me whatever you have'. E.g. give me any pepperoni pizza. But the factory can return a New York pepperoni pizza, or a extra thin pepperoni pizza, depending on which factory you're using.
There are situations where the factory will work more like a strategy. You don't want to know what it does, but you still want to be able to tell it which type you'd like to receive. The type should either have an interface the factory can work on, or the factory itself should be so simple it doesn't need an interface. Here you say 'give me an instance of this type, do whatever you normally do with an instance'. E.g. I want this here pizza, with extra cheese.
Hope this helps.
我认为他们正在谈论继承类型。因此,如果您有产品作为父类和 2 个子类
BetaProduct
和FinishedProduct
。您可以通过简单地传递某种改变工厂内控制流的参数来告诉工厂要创建哪种类型的对象,BetaProduct 或FinishedProduct。它始终会根据工厂拥有的信息设置对象的值...但是当它谈论产品的种类时...那就是子类的名称/类型。I think that they are talking about inherited types. So if you have product as a parent class and 2 child classes
BetaProduct
andFinishedProduct
. You can tell the factory what kind of object to create, aBetaProduct
or aFinishedProduct
by simple passing in some sort of parameter that changes the control flow within the factory. It will always set the values on the object based on the information the factory has...but when it talks about KIND of product...that is the name/type of the child class.您的工厂可以从数据库或配置文件本身查找值,但是这更有可能在工厂外部完成然后传入。
Your factory could look up a value from the database or config file itself, however this is more likely to be done outside of the factory then passed in.