Skip to content

Commit b77a05f

Browse files
committed
Update README.md
1 parent 0ee03db commit b77a05f

File tree

1 file changed

+29
-12
lines changed

1 file changed

+29
-12
lines changed

factory-method/README.md

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,37 @@ tags:
1010
---
1111

1212
## Also known as
13+
1314
Virtual Constructor
1415

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

2021
## Explanation
22+
2123
Real world example
2224

23-
> 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.
25+
> Blacksmith manufactures weapons. Elves require Elvish weapons and orcs require Orcish weapons.
26+
> Depending on the customer at hand the right type of blacksmith is summoned.
2427
2528
In plain words
2629

2730
> It provides a way to delegate the instantiation logic to child classes.
2831
2932
Wikipedia says
3033

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

35-
Taking our blacksmith example above. First of all we have a blacksmith interface and some implementations for it
42+
Taking our blacksmith example above. First of all we have a `Blacksmith` interface and some
43+
implementations for it:
3644

3745
```java
3846
public interface Blacksmith {
@@ -52,24 +60,33 @@ public class OrcBlacksmith implements Blacksmith {
5260
}
5361
```
5462

55-
Now as the customers come the correct type of blacksmith is summoned and requested weapons are manufactured
63+
When the customers come, the correct type of blacksmith is summoned and requested weapons are
64+
manufactured:
5665

5766
```java
5867
var blacksmith = new ElfBlacksmith();
5968
blacksmith.manufactureWeapon(WeaponType.SPEAR);
6069
blacksmith.manufactureWeapon(WeaponType.AXE);
61-
// Elvish weapons are created
70+
```
71+
72+
Program output:
73+
```java
74+
// Elven spear
75+
// Elven axe
6276
```
6377

6478
## Class diagram
79+
6580
![alt text](./etc/factory-method.urm.png "Factory Method pattern class diagram")
6681

6782
## Applicability
68-
Use the Factory Method pattern when
6983

70-
* a class can't anticipate the class of objects it must create
71-
* a class wants its subclasses to specify the objects it creates
72-
* classes delegate responsibility to one of several helper subclasses, and you want to localize the knowledge of which helper subclass is the delegate
84+
Use the Factory Method pattern when:
85+
86+
* Class cannot anticipate the class of objects it must create.
87+
* Class wants its subclasses to specify the objects it creates.
88+
* Classes delegate responsibility to one of several helper subclasses, and you want to localize the
89+
knowledge of which helper subclass is the delegate.
7390

7491
## Real world examples
7592

0 commit comments

Comments
 (0)