Skip to content

Commit a251032

Browse files
committed
docs: update factory method
1 parent 4a558bb commit a251032

File tree

6 files changed

+37
-37
lines changed

6 files changed

+37
-37
lines changed

factory-method/README.md

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ title: Factory Method
33
category: Creational
44
language: en
55
tag:
6-
- Extensibility
7-
- Gang of Four
6+
- Encapsulation
7+
- Gang of Four
8+
- Instantiation
9+
- Object composition
10+
- Polymorphism
811
---
912

1013
## Also known as
@@ -13,32 +16,25 @@ Virtual Constructor
1316

1417
## Intent
1518

16-
Define an interface for creating an object, but let subclasses decide which class to instantiate.
17-
Factory Method lets a class defer instantiation to subclasses.
19+
Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.
1820

1921
## Explanation
2022

2123
Real-world example
2224

23-
> Blacksmith manufactures weapons. Elves require Elvish weapons and orcs require Orcish weapons.
24-
> Depending on the customer at hand the right type of blacksmith is summoned.
25+
> Blacksmith manufactures weapons. Elves require Elvish weapons and orcs require Orcish weapons. Depending on the customer at hand the right type of blacksmith is summoned.
2526
2627
In plain words
2728

2829
> It provides a way to delegate the instantiation logic to child classes.
2930
3031
Wikipedia says
3132

32-
> In class-based programming, the factory method pattern is a creational pattern that uses factory
33-
> methods to deal with the problem of creating objects without having to specify the exact class of
34-
> the object that will be created. This is done by creating objects by calling a factory method
35-
> — either specified in an interface and implemented by child classes, or implemented in a base
36-
> class and optionally overridden by derived classes—rather than by calling a constructor.
33+
> In class-based programming, the factory method pattern is a creational pattern that uses factory methods to deal with the problem of creating objects without having to specify the exact class of the object that will be created. This is done by creating objects by calling a factory method — either specified in an interface and implemented by child classes, or implemented in a base class and optionally overridden by derived classes—rather than by calling a constructor.
3734
38-
**Programmatic Example**
35+
**Programmatic Example**
3936

40-
Taking our blacksmith example above. First of all, we have a `Blacksmith` interface and some
41-
implementations for it:
37+
Taking our blacksmith example above. First of all, we have a `Blacksmith` interface and some implementations for it:
4238

4339
```java
4440
public interface Blacksmith {
@@ -58,8 +54,7 @@ public class OrcBlacksmith implements Blacksmith {
5854
}
5955
```
6056

61-
When the customers come, the correct type of blacksmith is summoned and requested weapons are
62-
manufactured:
57+
When the customers come, the correct type of blacksmith is summoned and requested weapons are manufactured:
6358

6459
```java
6560
Blacksmith blacksmith = new OrcBlacksmith();
@@ -93,8 +88,7 @@ Use the Factory Method pattern when:
9388

9489
* Class cannot anticipate the class of objects it must create.
9590
* Class wants its subclasses to specify the objects it creates.
96-
* Classes delegate responsibility to one of several helper subclasses, and you want to localize the
97-
knowledge of which helper subclass is the delegate.
91+
* Classes delegate responsibility to one of several helper subclasses, and you want to localize the knowledge of which helper subclass is the delegate.
9892

9993
## Known uses
10094

@@ -105,9 +99,27 @@ knowledge of which helper subclass is the delegate.
10599
* [java.net.URLStreamHandlerFactory](http://docs.oracle.com/javase/8/docs/api/java/net/URLStreamHandlerFactory.html#createURLStreamHandler-java.lang.String-)
106100
* [java.util.EnumSet](https://docs.oracle.com/javase/8/docs/api/java/util/EnumSet.html#of-E-)
107101
* [javax.xml.bind.JAXBContext](https://docs.oracle.com/javase/8/docs/api/javax/xml/bind/JAXBContext.html#createMarshaller--)
102+
* Frameworks that run application components, configured dynamically at runtime.
103+
104+
## Consequences
105+
106+
Benefits:
107+
108+
* Provides hooks for subclasses, creating flexibility in code.
109+
* Connects parallel class hierarchies.
110+
* Eliminates the need to bind application-specific classes into the code. The code only deals with the product interface; hence it can work with any user-defined concrete product classes.
111+
112+
Trade-offs:
113+
114+
* Can complicate the code by requiring the addition of new subclasses to implement the extended factory methods.
115+
116+
## Related Patterns
117+
118+
* [Abstract Factory](https://java-design-patterns.com/patterns/abstract-factory/): Factory methods are often called within Abstract Factory patterns.
119+
* [Prototype](https://java-design-patterns.com/patterns/prototype/): A factory method that returns a new instance of a class that is a clone of a prototype class.
108120

109121
## Credits
110122

111-
* [Design Patterns: Elements of Reusable Object-Oriented Software](https://www.amazon.com/gp/product/0201633612/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0201633612&linkCode=as2&tag=javadesignpat-20&linkId=675d49790ce11db99d90bde47f1aeb59)
112-
* [Head First Design Patterns: A Brain-Friendly Guide](https://www.amazon.com/gp/product/0596007124/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596007124&linkCode=as2&tag=javadesignpat-20&linkId=6b8b6eea86021af6c8e3cd3fc382cb5b)
113-
* [Refactoring to Patterns](https://www.amazon.com/gp/product/0321213351/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0321213351&linkCode=as2&tag=javadesignpat-20&linkId=2a76fcb387234bc71b1c61150b3cc3a7)
123+
* [Design Patterns: Elements of Reusable Object-Oriented Software](https://amzn.to/3w0Rk5y)
124+
* [Head First Design Patterns: Building Extensible and Maintainable Object-Oriented Software](https://amzn.to/3UpTLrG)
125+
* [Patterns of Enterprise Application Architecture](https://amzn.to/4b2ZxoM)

factory-method/src/main/java/com/iluwatar/factory/method/ElfWeapon.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,7 @@
3030
/**
3131
* ElfWeapon.
3232
*/
33-
@RequiredArgsConstructor
34-
@Getter
35-
public class ElfWeapon implements Weapon {
36-
37-
private final WeaponType weaponType;
33+
public record ElfWeapon(WeaponType weaponType) implements Weapon {
3834

3935
@Override
4036
public String toString() {

factory-method/src/main/java/com/iluwatar/factory/method/OrcWeapon.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,7 @@
3030
/**
3131
* OrcWeapon.
3232
*/
33-
@RequiredArgsConstructor
34-
@Getter
35-
public class OrcWeapon implements Weapon {
36-
37-
private final WeaponType weaponType;
33+
public record OrcWeapon(WeaponType weaponType) implements Weapon {
3834

3935
@Override
4036
public String toString() {

factory-method/src/main/java/com/iluwatar/factory/method/Weapon.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@
2929
*/
3030
public interface Weapon {
3131

32-
WeaponType getWeaponType();
32+
WeaponType weaponType();
3333

3434
}

factory-method/src/test/java/com/iluwatar/factory/method/FactoryMethodTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,6 @@ void testElfBlacksmithWithSpear() {
9898
private void verifyWeapon(Weapon weapon, WeaponType expectedWeaponType, Class<?> clazz) {
9999
assertTrue(clazz.isInstance(weapon), "Weapon must be an object of: " + clazz.getName());
100100
assertEquals(expectedWeaponType, weapon
101-
.getWeaponType(), "Weapon must be of weaponType: " + expectedWeaponType);
101+
.weaponType(), "Weapon must be of weaponType: " + expectedWeaponType);
102102
}
103103
}

factory/README.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ tag:
77
- Instantiation
88
---
99

10-
## Also known as
11-
12-
* Factory Method
13-
1410
## Intent
1511

1612
The Factory design pattern is intended to define an interface for creating an object, but allows subclasses to alter the type of objects that will be created. This pattern is particularly useful when the creation process involves complexity.

0 commit comments

Comments
 (0)