I have used Factory pattern where I have multiple classes which does similar sort of work, but I don't want client to worry about class instantiating and return types. So Instantiate the necessary class dynamically depending on input and pass the values. These classes have similar methods but does slightly different work and these classes inherit the same common interface. Is this the correct way or can I improve it further?

  • 1
    But are those implementation classes doing the same thing from the perspective of the caller of the factory? If so, they're reasonable to have together. Also consider whether to use the Abstract Factory pattern… – Donal Fellows Apr 14 '14 at 8:14

It is perfectly legitimate to use a factory pattern to instantiate different classes that implement a common interface. The classes themselves could be derived from a variety of sources and could even offer a range of other services. The key point is that as long as the WhatIWantToDoFactory returns an object that implements the IWhatIWantToDo interface its job is done.

This kind of capability is particularly useful in assembling structures using Dependency Injection/IOC principles.

Having said all that, this is question and my answer to it border on being opinion-based, and are only a marginally good fit for this site. IMHO.

  • +1 - This is more of a professional opinion IMHO. – JeffO Apr 14 '14 at 12:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.