task: Explanations and grammar fixes for all the GoF patterns (#1791)
* Grammatical fixes to command pattern * Update bridge pattern readme * Fixes to builder pattern grammar * Update chain of responsibility * Improvements to the composite example * Fixes to headings * Minor updates to decorator pattern * Update facade * Update factory example * Update factory method * Update flyweight * Interpreter explanation * Update iterator readme * Add explanation for mediator pattern * Grammatical fixes to memento * Grammar fixes for observer * Update explanation for the prototype pattern * Proxy pattern grammar fixes * Update singleton * Grammar fixes to state pattern * Grammar fixes for strategy * Grammar fixes, template method * Grammar fixes for visitor * Fix typo
This commit is contained in:
@ -7,7 +7,7 @@ categories: Creational
|
||||
language: en
|
||||
tags:
|
||||
- Extensibility
|
||||
- Gang Of Four
|
||||
- Gang of Four
|
||||
---
|
||||
|
||||
## Also known as
|
||||
@ -21,7 +21,7 @@ Factory Method lets a class defer instantiation to subclasses.
|
||||
|
||||
## Explanation
|
||||
|
||||
Real world example
|
||||
Real-world example
|
||||
|
||||
> 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.
|
||||
@ -40,7 +40,7 @@ Wikipedia says
|
||||
|
||||
**Programmatic Example**
|
||||
|
||||
Taking our blacksmith example above. First of all we have a `Blacksmith` interface and some
|
||||
Taking our blacksmith example above. First of all, we have a `Blacksmith` interface and some
|
||||
implementations for it:
|
||||
|
||||
```java
|
||||
@ -65,15 +65,25 @@ When the customers come, the correct type of blacksmith is summoned and requeste
|
||||
manufactured:
|
||||
|
||||
```java
|
||||
var blacksmith = new ElfBlacksmith();
|
||||
blacksmith.manufactureWeapon(WeaponType.SPEAR);
|
||||
blacksmith.manufactureWeapon(WeaponType.AXE);
|
||||
Blacksmith blacksmith = new OrcBlacksmith();
|
||||
Weapon weapon = blacksmith.manufactureWeapon(WeaponType.SPEAR);
|
||||
LOGGER.info("{} manufactured {}", blacksmith, weapon);
|
||||
weapon = blacksmith.manufactureWeapon(WeaponType.AXE);
|
||||
LOGGER.info("{} manufactured {}", blacksmith, weapon);
|
||||
|
||||
blacksmith = new ElfBlacksmith();
|
||||
weapon = blacksmith.manufactureWeapon(WeaponType.SPEAR);
|
||||
LOGGER.info("{} manufactured {}", blacksmith, weapon);
|
||||
weapon = blacksmith.manufactureWeapon(WeaponType.AXE);
|
||||
LOGGER.info("{} manufactured {}", blacksmith, weapon);
|
||||
```
|
||||
|
||||
Program output:
|
||||
```java
|
||||
// Elven spear
|
||||
// Elven axe
|
||||
```
|
||||
The orc blacksmith manufactured an orcish spear
|
||||
The orc blacksmith manufactured an orcish axe
|
||||
The elf blacksmith manufactured an elven spear
|
||||
The elf blacksmith manufactured an elven axe
|
||||
```
|
||||
|
||||
## Class diagram
|
||||
@ -89,7 +99,7 @@ Use the Factory Method pattern when:
|
||||
* Classes delegate responsibility to one of several helper subclasses, and you want to localize the
|
||||
knowledge of which helper subclass is the delegate.
|
||||
|
||||
## Real world examples
|
||||
## Known uses
|
||||
|
||||
* [java.util.Calendar](http://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html#getInstance--)
|
||||
* [java.util.ResourceBundle](http://docs.oracle.com/javase/8/docs/api/java/util/ResourceBundle.html#getBundle-java.lang.String-)
|
||||
|
Reference in New Issue
Block a user