lundi 23 août 2021

Why Parameterless factory methods are Leaky Abstraction?

I'm reading a book which says:

// code smell
public interface IProductRepositoryFactory {
   IProductRepository Create();
}

The Dependencies created by an Abstract Factory should conceptually require a runtime value, and the translation from a runtime value into an Abstraction should make sense. By specifying an IProductRepositoryFactory Abstraction with a parameterless Create method, you let the consumer know that there are more instances of the given service, and that it has to deal with this. Because another implementation of IProductRepository might not require multiple instances or deterministic disposal at all, you’re therefore leaking implementation details through the Abstract Factory with its parameterless Create method.

I'm a little bit confused here, what does "more instances of the given service" mean, does it mean that you call a concrete Factory's Create method multiple times? what's wrong with that? even if you have factory methods that does have parameters as:

public interface IProductRepositoryFactory {
   IProductRepository Create(string type);
}

and if you call a concrete Factory's Create method multiple times there will be multiple instances too. so what's wrong with parameterless factory methods? what does it leak?

Aucun commentaire:

Enregistrer un commentaire