I understand the theory behind the factory design pattern now, but can’t seem to find any realistic examples of it’s use. Can someone be as kind enough as to provide one?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There are a few variants of factory designs: abstract factory, factory method, etc… Since you’re interested in a real-world example, I thought I’d share what I did.
As one example, I used a data access factory to return a concrete instance of a data access class. The logic class doesn’t know or care which database is being used; it simply asks the factory for a data class, and then uses that data class.
This is the method within my DataAccessFactory class. It is responsible for determing which data class to use, and returning it to the caller:
And this is how one of my business logic classes makes a DAL request:
The business logic is completely decoupled from the data access layer. Hope that helps.