Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 09c4e1d

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

File tree

Expand file treeCollapse file tree

5 files changed

+45
-1
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

5 files changed

+45
-1
lines changed
Open diff view settings
Collapse file

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

Copy file name to clipboardExpand all lines: src/main/java/org/codefx/demo/effective_java/_01_static_factory_methods/Main.java
+1Lines 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 {
Collapse file

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

Copy file name to clipboardExpand all lines: src/main/java/org/codefx/demo/effective_java/_01_static_factory_methods/Rectangle.java
+5-1Lines 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

Collapse file
+29Lines 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+
}
Collapse file
+3Lines 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 { }
Collapse file
+7Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.