This confuses me, in the most simplest terms what does it do? Pretend you are explaining to your mother or someone almost please.
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.
A factory creates an object. So, if you wanted to build
You wouldn’t want to rely on having to do the following code everytime you create the object
That is where the factory would come in. We define a factory to take care of that for us:
Now all we have to do is
The real advantage is when you want to change the class. Lets say we wanted to pass in a different ClassC:
or a new ClassB:
Now we can use inheritance to easily modify how the class is created, to put in a different set of classes.
A good example might be this user class:
In this class
$datais the class we use to store our data. Now for this class, lets say we use a Session to store our data. The factory would look like this:Now, lets say instead we want to store all of our data in the database, it is really simple to change it:
Factories are a design pattern we use to control how we put objects together, and using correct factory patterns allows us to create the customized objects we need.