Cleanup factory
This commit is contained in:
parent
9d21dff855
commit
a1da1e4973
@ -15,35 +15,31 @@ tags:
|
|||||||
|
|
||||||
## Intent
|
## Intent
|
||||||
|
|
||||||
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.
|
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
|
## Explanation
|
||||||
|
|
||||||
Real world example
|
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.
|
> 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
|
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.
|
> 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**
|
**Programmatic Example**
|
||||||
|
|
||||||
We have an interface 'Car' and tow implementations 'Ford' and 'Ferrari'.
|
We have an interface `Car` and two implementations `Ford` and `Ferrari`.
|
||||||
|
|
||||||
```java
|
```java
|
||||||
/**
|
|
||||||
* Car interface.
|
|
||||||
*/
|
|
||||||
public interface Car {
|
public interface Car {
|
||||||
|
String getDescription();
|
||||||
public String getDescription();
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Ford implementation.
|
|
||||||
*/
|
|
||||||
public class Ford implements Car {
|
public class Ford implements Car {
|
||||||
|
|
||||||
static final String DESCRIPTION = "This is Ford.";
|
static final String DESCRIPTION = "This is Ford.";
|
||||||
@ -54,9 +50,6 @@ public class Ford implements Car {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Ferrari implementation.
|
|
||||||
*/
|
|
||||||
public class Ferrari implements Car {
|
public class Ferrari implements Car {
|
||||||
|
|
||||||
static final String DESCRIPTION = "This is Ferrari.";
|
static final String DESCRIPTION = "This is Ferrari.";
|
||||||
@ -68,14 +61,11 @@ public class Ferrari implements Car {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Enumeration above represents types of cars that we support (Ford and Ferrari).
|
Enumeration above represents types of cars that we support (`Ford` and `Ferrari`).
|
||||||
|
|
||||||
```java
|
```java
|
||||||
public enum CarType {
|
public enum CarType {
|
||||||
|
|
||||||
/**
|
|
||||||
* Enumeration for different types of cars.
|
|
||||||
*/
|
|
||||||
FORD(Ford::new),
|
FORD(Ford::new),
|
||||||
FERRARI(Ferrari::new);
|
FERRARI(Ferrari::new);
|
||||||
|
|
||||||
@ -90,17 +80,12 @@ public enum CarType {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
Then we have the static method 'getCar' to create car objects encapsulated in the factory class 'CarSimpleFactory'.
|
Then we have the static method `getCar` to create car objects encapsulated in the factory class
|
||||||
|
`CarSimpleFactory`.
|
||||||
|
|
||||||
```java
|
```java
|
||||||
/**
|
|
||||||
* Factory of cars.
|
|
||||||
*/
|
|
||||||
public class CarsFactory {
|
public class CarsFactory {
|
||||||
|
|
||||||
/**
|
|
||||||
* Factory method takes as parameter a car type and initiate the appropriate class.
|
|
||||||
*/
|
|
||||||
public static Car getCar(CarType type) {
|
public static Car getCar(CarType type) {
|
||||||
return type.getConstructor().get();
|
return type.getConstructor().get();
|
||||||
}
|
}
|
||||||
@ -122,17 +107,21 @@ Program output:
|
|||||||
This is Ford.
|
This is Ford.
|
||||||
This Ferrari.
|
This Ferrari.
|
||||||
```
|
```
|
||||||
|
|
||||||
## Class Diagram
|
## Class Diagram
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
## Applicability
|
## Applicability
|
||||||
Use the Simple Factory pattern when you only care about the creation of a object, not how to create and manage it.
|
|
||||||
|
|
||||||
## Pros
|
Use the Simple Factory pattern when you only care about the creation of a object, not how to create
|
||||||
|
and manage it.
|
||||||
|
|
||||||
|
Pros
|
||||||
* Allows keeping all objects creation in one place and avoid of spreading 'new' key value across codebase.
|
* Allows keeping all objects creation in one place and avoid of spreading 'new' key value across codebase.
|
||||||
* Allows to writs loosely coupled code. Some of its main advantages include better testability, easy-to-understand code, swappable components, scalability and isolated features.
|
* Allows to writs loosely coupled code. Some of its main advantages include better testability, easy-to-understand code, swappable components, scalability and isolated features.
|
||||||
|
|
||||||
## Cons
|
Cons
|
||||||
* The code becomes more complicated than it should be.
|
* The code becomes more complicated than it should be.
|
||||||
|
|
||||||
## Related patterns
|
## Related patterns
|
||||||
@ -140,5 +129,3 @@ Use the Simple Factory pattern when you only care about the creation of a object
|
|||||||
[Factory Method](https://java-design-patterns.com/patterns/factory-method/)
|
[Factory Method](https://java-design-patterns.com/patterns/factory-method/)
|
||||||
[Factory Kit](https://java-design-patterns.com/patterns/factory-kit/)
|
[Factory Kit](https://java-design-patterns.com/patterns/factory-kit/)
|
||||||
[Abstract Factory](https://java-design-patterns.com/patterns/abstract-factory/)
|
[Abstract Factory](https://java-design-patterns.com/patterns/abstract-factory/)
|
||||||
|
|
||||||
|
|
||||||
|
@ -5,6 +5,6 @@ package com.iluwatar.factory;
|
|||||||
*/
|
*/
|
||||||
public interface Car {
|
public interface Car {
|
||||||
|
|
||||||
public String getDescription();
|
String getDescription();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user