Skip to content

Commit a1da1e4

Browse files
committed
Cleanup factory
1 parent 9d21dff commit a1da1e4

2 files changed

Lines changed: 20 additions & 33 deletions

File tree

‎factory/README.md‎

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,35 +15,31 @@ tags:
1515

1616
## Intent
1717

18-
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.
18+
Providing a static method encapsulated in a class called factory, in order to hide the
19+
implementation logic and makes client code focus on usage rather then initialization new objects.
1920

2021
## Explanation
2122

2223
Real world example
2324

24-
> 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.
25+
> Lets say we have a web application connected to SQLServer, but now we want to switch to Oracle. To
26+
> do so without modifying existing source code, we need to implements Simple Factory pattern, in
27+
> which a static method can be invoked to create connection to a given database.
2528
2629
Wikipedia says
2730

28-
> 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.
31+
> Factory is an object for creating other objects – formally a factory is a function or method that
32+
> returns objects of a varying prototype or class.
2933
3034
**Programmatic Example**
3135

32-
We have an interface 'Car' and tow implementations 'Ford' and 'Ferrari'.
36+
We have an interface `Car` and two implementations `Ford` and `Ferrari`.
3337

3438
```java
35-
/**
36-
* Car interface.
37-
*/
3839
public interface Car {
39-
40-
public String getDescription();
41-
40+
String getDescription();
4241
}
4342

44-
/**
45-
* Ford implementation.
46-
*/
4743
public class Ford implements Car {
4844

4945
static final String DESCRIPTION = "This is Ford.";
@@ -54,9 +50,6 @@ public class Ford implements Car {
5450
}
5551
}
5652

57-
/**
58-
* Ferrari implementation.
59-
*/
6053
public class Ferrari implements Car {
6154

6255
static final String DESCRIPTION = "This is Ferrari.";
@@ -68,14 +61,11 @@ public class Ferrari implements Car {
6861
}
6962
```
7063

71-
Enumeration above represents types of cars that we support (Ford and Ferrari).
64+
Enumeration above represents types of cars that we support (`Ford` and `Ferrari`).
7265

7366
```java
7467
public enum CarType {
7568

76-
/**
77-
* Enumeration for different types of cars.
78-
*/
7969
FORD(Ford::new),
8070
FERRARI(Ferrari::new);
8171

@@ -90,17 +80,12 @@ public enum CarType {
9080
}
9181
}
9282
```
93-
Then we have the static method 'getCar' to create car objects encapsulated in the factory class 'CarSimpleFactory'.
83+
Then we have the static method `getCar` to create car objects encapsulated in the factory class
84+
`CarSimpleFactory`.
9485

9586
```java
96-
/**
97-
* Factory of cars.
98-
*/
9987
public class CarsFactory {
10088

101-
/**
102-
* Factory method takes as parameter a car type and initiate the appropriate class.
103-
*/
10489
public static Car getCar(CarType type) {
10590
return type.getConstructor().get();
10691
}
@@ -122,23 +107,25 @@ Program output:
122107
This is Ford.
123108
This Ferrari.
124109
```
110+
125111
## Class Diagram
112+
126113
![alt text](./etc/factory.urm.png "Factory pattern class diagram")
127114

128115
## Applicability
129-
Use the Simple Factory pattern when you only care about the creation of a object, not how to create and manage it.
130116

131-
## Pros
117+
Use the Simple Factory pattern when you only care about the creation of a object, not how to create
118+
and manage it.
119+
120+
Pros
132121
* Allows keeping all objects creation in one place and avoid of spreading 'new' key value across codebase.
133122
* Allows to writs loosely coupled code. Some of its main advantages include better testability, easy-to-understand code, swappable components, scalability and isolated features.
134123

135-
## Cons
124+
Cons
136125
* The code becomes more complicated than it should be.
137126

138127
## Related patterns
139128

140129
[Factory Method](https://java-design-patterns.com/patterns/factory-method/)
141130
[Factory Kit](https://java-design-patterns.com/patterns/factory-kit/)
142131
[Abstract Factory](https://java-design-patterns.com/patterns/abstract-factory/)
143-
144-

‎factory/src/main/java/com/iluwatar/factory/Car.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
*/
66
public interface Car {
77

8-
public String getDescription();
8+
String getDescription();
99

1010
}

0 commit comments

Comments
 (0)