Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which namespace does a factory class belong?

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 alt text

like image 599
dance2die Avatar asked Aug 24 '09 00:08

dance2die


People also ask

What is a factory in C#?

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).

Why are factory classes used?

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.

Why use factory pattern c#?

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.


2 Answers

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.

like image 97
jrista Avatar answered Nov 09 '22 13:11

jrista


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.

like image 32
Odd Avatar answered Nov 09 '22 12:11

Odd