This confuses me, in the most simplest terms what does it do? Pretend you are explaining to your mother or someone almost please.
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
- Using similar_text and strpos together
The classic approach to instantiate an object is:
PHP has the ability to dynamically create an object from variable name using the following syntax:
where variable $classname contains the name of class one wants to instantiate.
So classic object factoring would look like:
and if you call getInstance('Product') function this factory will create and return Product object. Otherwise if you call getInstance('Customer') function this factory will create and return Customer type object (created from Customer() class).
There's no need for that any more, one can send 'Product' or 'Customer' (exact names of existing classes) as a value of variable for dynamic instantiation:
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
$data
is 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.
In general a "factory" produces something: in the case of Object-Orientated-Programming, a "factory design pattern" produces objects.
It doesn't matter if it's in PHP, C# or any other Object-Orientated language.
Like a real life factory, it creates something and returns it.
Imagine something like this
or a factory method
The implementation of the factory method will create a new instance and return it.
Factory design pattern is very good when you are dealing with multiple resources and want to implement high level abstraction.
Let's break this into different section.
Suppose you have to implement abstraction and the user of your class doesn't need to care about what you've implemented in class definition.
He/She just need to worry about the use of your class methods.
e.g. You have two databases for your project
Your Factory class would take care of the creation of object for database connection.
User just need to pass the name of the database type
Output:
In future you may have different database then you don't need to change the entire code only need to pass the new database type and other code will run without making any changes.
Output:
Hope this will help.
For the record, in easy words, a factory like @Pindatjuh said, returns a object.
So, what's the difference with a constructor? (that does the same)
Constructor is called when each instance is created. Sometimes you don't want that.
For example, let's say that every time i creates an object of the class Account, i read from the database a file and use it as a template.
Using constructor:
Using factory: