Skip to content

Commit 09c4e1d

Browse files
author
Nicolai Parlog
committed
[01] Show orthoginality to factory patterns
1 parent 2487e3d commit 09c4e1d

File tree

5 files changed

+45
-1
lines changed

5 files changed

+45
-1
lines changed

src/main/java/org/codefx/demo/effective_java/_01_static_factory_methods/Main.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* - named constructors
77
* - instance-control
88
* - type-control
9+
* - not factory pattern
910
*/
1011

1112
public class Main {

src/main/java/org/codefx/demo/effective_java/_01_static_factory_methods/Rectangle.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package org.codefx.demo.effective_java._01_static_factory_methods;
22

3+
import org.codefx.demo.effective_java._01_static_factory_methods.not_factory_pattern.Shape;
4+
35
import static org.codefx.demo.effective_java._01_static_factory_methods.Point.ofXY;
46

5-
public class Rectangle {
7+
// NOTE not factory pattern:
8+
// It only implements `Shape`, so the code in the package not_factory_pattern compiles
9+
public class Rectangle implements Shape {
610

711
protected final Point lowerLeft, upperRight;
812

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.codefx.demo.effective_java._01_static_factory_methods.not_factory_pattern;
2+
3+
import org.codefx.demo.effective_java._01_static_factory_methods.Point;
4+
import org.codefx.demo.effective_java._01_static_factory_methods.Rectangle;
5+
6+
// NOTE not factory pattern:
7+
// This is an implementation of the abstract factory pattern
8+
// (https://en.wikipedia.org/wiki/Abstract_factory_pattern)
9+
public class GrowingRectangleFactory implements ShapeFactory {
10+
11+
private Point lowerLeft;
12+
private Point upperRight;
13+
14+
public GrowingRectangleFactory(Point lowerLeft, Point upperRight) {
15+
this.lowerLeft = lowerLeft;
16+
this.upperRight = upperRight;
17+
}
18+
19+
@Override
20+
public Shape createShape() {
21+
upperRight = Point.ofXY(upperRight.x() + 1, upperRight.y() + 1);
22+
// NOTE not factory pattern:
23+
// Static factory methods are no replacement for abstract factory pattern or factory method pattern.
24+
// In fact, they don't interact with it at all except that you call a static factory method instead
25+
// of a constructor.
26+
return Rectangle.fromLowerLeftToUpperRight(lowerLeft, upperRight);
27+
}
28+
29+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package org.codefx.demo.effective_java._01_static_factory_methods.not_factory_pattern;
2+
3+
public interface Shape { }
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.codefx.demo.effective_java._01_static_factory_methods.not_factory_pattern;
2+
3+
public interface ShapeFactory {
4+
5+
public Shape createShape();
6+
7+
}

0 commit comments

Comments
 (0)