2

Lets look on a simple example: assume that I have three classes implemeting IPersonRepository: SQLPersonRepository, WebPersonRepository, InMemoryPersonRepository. I also have PersonRepositoryFactory class which is simple Factory method pattern implementation - contains GetPersonRepository methods which have enum/string as parameter. How could I create Factory pattern (or in other words centralize my object creation) when SQLPersonRepository needs dependency for: SQLConnection or string to database path, WebPersonRepository needs some http settings object and InMemoryPersonRepository has no dependencies? I could pass it in Factory constructor or as method parameter but it'd be ugly, unclean and unmaintable especially if I had more IPersonRepository implementations.

asked Jan 23, 2014 at 22:41

1 Answer 1

3

I suggest to implement PersonRepositoryFactory as an abstract factory, with subclasses SQLPersonRepositoryFactory, WebPersonRepositoryFactory and InMemoryPersonRepositoryFactory. Those subclasses can have different constructor parameters (like SQLConnection, HttpSetting or no parameters). Once you have initialized those three factory objects, you can use them through an ordinary factory method for producing of many IPersonRepository objects, all with the same SQL connection or http settings.

answered Jan 23, 2014 at 23:11

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.