开发者

Parametrized factory in a factory pattern - does any other way exist? [closed]

开发者 https://www.devze.com 2023-01-29 11:02 出处:网络
Closed. This question needs to be more focused. It is not currently accepting answers. 开发者_Python百科
Closed. This question needs to be more focused. It is not currently accepting answers.
开发者_Python百科

Want to improve this question? Update the question so it focuses on one problem only by editing this post.

Closed 4 years ago.

Improve this question

I am reading Go4 design patterns book and currently looking at factory method pattern. The book mentions on Pg:110

"Another variation on the factory pattern lets the factory method create multiple kinds of products. The factory method takes a parameter that identifies the kind of object to create"

Now doesn't a factory always create multiple kinds of products? Also, isn't it always necessary to send in a parameter to decide what kind of product to create? I mean, if we don't pass a parameter, how will the factory decide which object to create? What do they mean by another varation? Am I missing any point here?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号