C
C#2mo ago
FraznoFire

Naming factories “class”factory always a good idea?

Hi everyone, I’m just learning about design patterns, and all code examples I’ve seen highlight their implementation with obvious examples such as “DogFactory” etc. I’m wondering if this is usually done in practice, or if the class name should instead represent its purpose in the code, for example: A factory class that resolves the manufacturer of an item from its properties, and returns that manufacturer’s specific implementation of IVendorThing, would I name that factory “VendorThingFactory” or something like “VendorThingResolver”?
5 Replies
Tyrr
Tyrr2mo ago
i'd probably separate these two into VendorThingResolver using VendorThingFactory, or make VendorThingResolver implement IFactory<VendorThing> but it's not such a big deal anyways i guess, as long as class name gives a clear hint of what it does for other devs its okay to name a class SomeThingFactory if its main purpose is to create objects, but if its more complex, then go for more accurate name
FraznoFire
FraznoFireOP2mo ago
It is purely to create objects that implement that interface, but I thought it might give more of a clue as to its purpose by saying it’s specifically to resolve/identify the specific implementation required for a given device, not a general purpose way to implement IVendorThing
Tyrr
Tyrr2mo ago
"...but I thought it might give more of a clue..." personally when i have such thought about naming anything in my code, i just do it. Conventions are to help making code understandable, but they dont cover every case, sometimes can even make it harder to figure out what it does "Resolver" is very intuitive, it clearly states that it is to fetch something from some input then return result "Factory" on the other hand tells you that it will return "a product" but does not mention the input args, so it can be a Factory that will just generate something with no input provided
ero
ero2mo ago
Resolve to me means more like "find some existing data from some input". Factory means more like "create something completely new (maybe taking some input into account)" so say if you're trying to find implementations of IVendorThing, that's resolving
Tyrr
Tyrr2mo ago
thats right. simple rule of thumb: Factory is to generate things
Want results from more Discord servers?
Add your server