Skip to content

Commit

Permalink
#590 Alter Factory Method presentation
Browse files Browse the repository at this point in the history
  • Loading branch information
iluwatar committed Aug 12, 2017
1 parent 002774b commit 2d750dc
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 172 deletions.
41 changes: 40 additions & 1 deletion factory-method/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,46 @@ Define an interface for creating an object, but let subclasses
decide which class to instantiate. Factory Method lets a class defer
instantiation to subclasses.

![alt text](./etc/factory-method_1.png "Factory Method")
## Explanation

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.
In plain words
> It provides a way to delegate the instantiation logic to child classes.
Wikipedia says
> 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.
**Programmatic Example**

Taking our blacksmith example above. First of all we have a blacksmith interface and some implementations for it

```
public interface Blacksmith {
Weapon manufactureWeapon(WeaponType weaponType);
}
public class ElfBlacksmith implements Blacksmith {
public Weapon manufactureWeapon(WeaponType weaponType) {
return new ElfWeapon(weaponType);
}
}
public class OrcBlacksmith implements Blacksmith {
public Weapon manufactureWeapon(WeaponType weaponType) {
return new OrcWeapon(weaponType);
}
}
```

Now as the customers come the correct type of blacksmith is summoned and requested weapons are manufactured
```
Blacksmith blacksmith = new ElfBlacksmith();
blacksmith.manufactureWeapon(WeaponType.SPEAR);
blacksmith.manufactureWeapon(WeaponType.AXE);
// Elvish weapons are created
```

## Applicability
Use the Factory Method pattern when
Expand Down
Binary file removed factory-method/etc/factory-method.png
Binary file not shown.
117 changes: 0 additions & 117 deletions factory-method/etc/factory-method.ucls

This file was deleted.

54 changes: 0 additions & 54 deletions factory-method/etc/factory-method.urm.puml

This file was deleted.

Binary file removed factory-method/etc/factory-method_1.png
Binary file not shown.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,7 @@
<!-- skip for parent project -->
<param>java-design-patterns</param>
<param>singleton</param>
<param>factory-method</param>
</skipForProjects>
</configuration>
</plugin>
Expand Down

0 comments on commit 2d750dc

Please sign in to comment.