File tree Expand file tree Collapse file tree 5 files changed +45
-1
lines changed
src/main/java/org/codefx/demo/effective_java/_01_static_factory_methods Expand file tree Collapse file tree 5 files changed +45
-1
lines changed Original file line number Diff line number Diff line change 66 * - named constructors
77 * - instance-control
88 * - type-control
9+ * - not factory pattern
910 */
1011
1112public class Main {
Original file line number Diff line number Diff line change 11package 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+
35import 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
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 1+ package org .codefx .demo .effective_java ._01_static_factory_methods .not_factory_pattern ;
2+
3+ public interface Shape { }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments