Providing a static method encapsulated in a class called factory, in order to hide the implementation logic and makes client code focus on usage rather then initialization new objects.
## Explanation
Real world example
> Lets say we have a web application connected to SQLServer, but now we want to switch to Oracle. To do so without modifying existing source code, we need to implements Simple Factory pattern, in which a static method can be invoked to create connection to a given database.
Wikipedia says
> Factory is an object for creating other objects – formally a factory is a function or method that returns objects of a varying prototype or class.
**Programmatic Example**
We have an interface "Car" and tow implementations "Ford" and "Ferrari".
```java
/**
* Car interface.
*/
publicinterfaceCar{
publicStringgetDescription();
}
/**
* Ford implementation.
*/
publicclassFordimplementsCar{
staticfinalStringDESCRIPTION="This is Ford.";
@Override
publicStringgetDescription(){
returnDESCRIPTION;
}
}
/**
* Ferrari implementation.
*/
publicclassFerrariimplementsCar{
staticfinalStringDESCRIPTION="This is Ferrari.";
@Override
publicStringgetDescription(){
returnDESCRIPTION;
}
}
```
Then we have the static method "getCar" to create car objects encapsulated in the factory class "CarSimpleFactory".
```java
/**
* Factory of cars.
*/
publicclassCarSimpleFactory{
/**
* Enumeration for different types of cars.
*/
staticenumCarType{
FORD,FERRARI
}
/**
* Factory method takes as parameter a car type and initiate the appropriate class.
*/
publicstaticCargetCar(CarTypetype){
switch(type){
caseFORD:returnnewFord();
caseFERRARI:returnnewFerrari();
default:thrownewIllegalArgumentException("Model not supported.");
}
}
}
```
Now on the client code we can create differentes types of cars(Ford or Ferrari) using the factory class.