✅ Confused when try to implement Factory Method
I'm learning design patterns starting from the factory method. what I'm confused about is the first initialization to be used as a reference to be manipulated, for example as follows:
https://hastebin.com/share/kohugizayu.csharp
These 3 variables are needed for the movement (and maybe initialization) of the player later and it looks like it should be done in each concrete product, so where should I declare these 3 variables in the factory method implementation? if I declare it in each concrete product it will look like duplication code.
Any suggestions?
Thanks
Hastebin
Hastebin is a free web-based pastebin service for storing and sharing text and code snippets with anyone. Get started now.
9 Replies
You could abstract position, size and speed out of your concrete classes into an abstract base class:
Have your concrete cores implement that by inheriting from
CoreBase
.
Also, you should rename movement
to Move
because
- methods should be verbs as they do things
- methods should be pascal case as that is the convention in C#
it's less of a problem concerning the factory and more of a concern of your type hierarchy
You are implementing the Abstract Factory pattern, whose purpose is two-fold:
- hide the implementation type
- hide the initialization complexity
Your current initialization effort is nil, but that might change in the future if you want to, say, let users provide the position, size and/or speed of a produced ICore
. In that case, you could add constructors to your concrete cores and modify the factory interface (ShapesCreator
) to take those constructor parameters.thanks for the advice!
$close
If you have no further questions, please use /close to mark the forum thread as answered
Sorry, I have another question. My first question: after the changes you suggested, can my code still be called a factory method? because it changes the structure of the UML diagram.
Second: what does initialization mean when you say my code implements abstract factory?
After you mentioned abstract factory I tried to understand and compare the two, but it raised a third question.
Third: can you give an example and explain when to use one over the other in an easy to understand sentence?
Thank you very much and sorry for responding so late, I thought no one would respond.
1. You can call anything that is concerned with creating an instance of another type a factory; methods or types are generally called factories. A "dumb" factory that just does
Create() => new Foo()
is still a factory.
2. Initialization is any operation you do as a prerequisite to or in service of the process of creating an instance. Fetching constructor arguments, calculating expensive values etc. account for the initialization effort. In your case, getting the initial Position
, Size
and Speed
values for created cores would be initialization effort in the factory. You could have users provide these values via the factory methods parameters. Initialization and Abstract Factory related only in that a factory performs initialization, and Abstract Factory is a factory.
3. On reading further into the subject, it seems you are indeed implementing a regular factory. The difference being that a regular factory produces one kind of object, whereas an abstract factory produces a family.
You can read more about factories here, here and here.Abstract Factory
Abstract Factory is a creational design pattern that lets you produce families of related objects without specifying their concrete classes.
Baeldung on Computer Science
Factory Method vs. Factory vs. Abstract Factory | Baeldung on Compu...
Learn about the Factory pattern and all its flavors.
I'll take that as a reference, thank you very much!
can i send you a friend request?
sure
I can't send it, I think because the request is disabled.
well, i will read those first. again, thank you!