I have a factory class, DocumentLoaderFactory
, which simply returns an instance that implements an interface, IDocumentLoader
.
All implementation resides under the following namespace
Skim.Ssms.AddIn.ActiveFileExplorer.Loader
But what I am wondering is, which namespace does DocumentLoaderFactory
belong?
I have placed the factory class under the *.Loader
namespace for now, but it is being used from a user control (ActiveFileWindow
) of the parent namespace, Skim.Ssms.AddIn.ActiveFileExplorer
as shown below.
What would be pros & cons of placing the factory method within *.Loader
or it's parent namespace? I would like to make a decision depending on pros/cons.
Here is the layout of my project
Factory method is a creational design pattern which solves the problem of creating product objects without specifying their concrete classes. The Factory Method defines a method, which should be used for creating objects instead of using a direct constructor call ( new operator).
Factory classes are often implemented because they allow the project to follow the SOLID principles more closely. In particular, the interface segregation and dependency inversion principles. Factories and interfaces allow for a lot more long term flexibility.
factory design pattern is used to instantiate objects based on another data type. Factories are often used to reduce code bloat and make it easier to modify which objects need to be created.
I would say its best to colocate your factories with the type(s) they create. A factory is a provider of something, and should be associated and proximate to the things it provides. If you follow the rules of cohesion, then you would come to the same conclusion. Related things should be close together to maintain a cohesive API.
Because the code that uses your factory needs have absolutely no knowledge of the implementation in the abstract factory pattern, I usually put the interface and the factory (plus any type information) into the root, then the implementations into their own folders (or folder if there are few).
So in your case I have something like:
Loader
- DocumentLoaderFactory
- DocumentLoadType
- IDocumentLoader
Loader\Implementation
- NameDocumentLoader
- TypeDocumentLoader
- ConnectionDocumentLoader
- DocumentLoader
I've assumed that DocumentLoader is an abstract base class that inherits your interface because of it's name, but you get the idea. I don't know what your other class "TreeViewImageIndex" is for but you could put it in either place or somewhere completely different if it's appropriate.
This keeps your code nice and cohesive, does not require your implementing class to know about the Loader\Implementation namespace and lets your document tree be easier to read.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With