You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit a1da1e4
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: factory/README.md
+19-32Lines changed: 19 additions & 32 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -15,35 +15,31 @@ tags:
15
15
16
16
## Intent
17
17
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.
19
20
20
21
## Explanation
21
22
22
23
Real world example
23
24
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.
25
28
26
29
Wikipedia says
27
30
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.
29
33
30
34
**Programmatic Example**
31
35
32
-
We have an interface 'Car' and tow implementations 'Ford' and 'Ferrari'.
36
+
We have an interface `Car` and two implementations `Ford` and `Ferrari`.
33
37
34
38
```java
35
-
/**
36
-
* Car interface.
37
-
*/
38
39
publicinterfaceCar {
39
-
40
-
publicStringgetDescription();
41
-
40
+
StringgetDescription();
42
41
}
43
42
44
-
/**
45
-
* Ford implementation.
46
-
*/
47
43
publicclassFordimplementsCar {
48
44
49
45
staticfinalStringDESCRIPTION="This is Ford.";
@@ -54,9 +50,6 @@ public class Ford implements Car {
54
50
}
55
51
}
56
52
57
-
/**
58
-
* Ferrari implementation.
59
-
*/
60
53
publicclassFerrariimplementsCar {
61
54
62
55
staticfinalStringDESCRIPTION="This is Ferrari.";
@@ -68,14 +61,11 @@ public class Ferrari implements Car {
68
61
}
69
62
```
70
63
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`).
72
65
73
66
```java
74
67
publicenumCarType {
75
68
76
-
/**
77
-
* Enumeration for different types of cars.
78
-
*/
79
69
FORD(Ford::new),
80
70
FERRARI(Ferrari::new);
81
71
@@ -90,17 +80,12 @@ public enum CarType {
90
80
}
91
81
}
92
82
```
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`.
94
85
95
86
```java
96
-
/**
97
-
* Factory of cars.
98
-
*/
99
87
publicclassCarsFactory {
100
88
101
-
/**
102
-
* Factory method takes as parameter a car type and initiate the appropriate class.
103
-
*/
104
89
publicstaticCargetCar(CarTypetype) {
105
90
return type.getConstructor().get();
106
91
}
@@ -122,23 +107,25 @@ Program output:
122
107
This is Ford.
123
108
ThisFerrari.
124
109
```
110
+
125
111
## ClassDiagram
112
+
126
113

127
114
128
115
## Applicability
129
-
Use the SimpleFactory pattern when you only care about the creation of a object, not how to create and manage it.
130
116
131
-
## Pros
117
+
Use the SimpleFactory pattern when you only care about the creation of a object, not how to create
118
+
and manage it.
119
+
120
+
Pros
132
121
*Allows keeping all objects creation in one place and avoid of spreading 'new' key value across codebase.
133
122
*Allows to writs loosely coupled code. Some of its main advantages include better testability, easy-to-understand code, swappable components, scalability and isolated features.
134
123
135
-
## Cons
124
+
Cons
136
125
*The code becomes more complicated than it should be.
0 commit comments