Can I use Attributes so my Factory knows what it can/should instantiate without breaking the “Loosely-Coupled” rule?

I don't think that using attributes would be increasing the coupling between the factory and the class it creates, in fact, it would decrease the coupling here because the factory would be discovering the information at runtime via the attributes. If anything you're simply trading the coupling between the class being created for the coupling to the attribute. That said, I'm not sure exactly what that buys you.

The point of the factory is that you localize the creational logic in a single place. By putting it into attributes you've spread it all over your code again, partially defeating the purpose of the factory: now you have to look at both the factory and the attribute to understand how the object is being created.

Decorate product classes of a factory can make development much easier and it is something I sometime do. This is especially useful when products must be created based on a unique identifier stored in a database for instance. There must be a mapping between that unique id and the product class and using an attribute makes this very clear and reabable.

Besides this, it allows you to add product classes, without changing the factory.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions