Skip to content

Commit 506d316

Browse files
committed
Add Factory Method presentation
1 parent 4b1650f commit 506d316

File tree

3 files changed

+190
-1
lines changed

3 files changed

+190
-1
lines changed

factory-method/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,11 @@ Use the Factory Method pattern when
6969
* a class wants its subclasses to specify the objects it creates
7070
* classes delegate responsibility to one of several helper subclasses, and you want to localize the knowledge of which helper subclass is the delegate
7171

72-
## Known uses
72+
## Presentations
73+
74+
* [Factory Method Pattern](etc/presentation.html)
75+
76+
## Real world examples
7377

7478
* [java.util.Calendar](http://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html#getInstance--)
7579
* [java.util.ResourceBundle](http://docs.oracle.com/javase/8/docs/api/java/util/ResourceBundle.html#getBundle-java.lang.String-)

factory-method/etc/diagram1.png

58.5 KB
Loading

factory-method/etc/presentation.html

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
<!--
2+
3+
The MIT License
4+
Copyright (c) 2017 Rodolfo Forte
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in
14+
all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
THE SOFTWARE.
23+
24+
-->
25+
<!DOCTYPE html>
26+
<html>
27+
<head>
28+
<title>Design Patterns - Factory Method Presentation</title>
29+
<meta charset="utf-8">
30+
<style>
31+
@import url(https://fonts.googleapis.com/css?family=Yanone+Kaffeesatz);
32+
@import url(https://fonts.googleapis.com/css?family=Droid+Serif:400,700,400italic);
33+
@import url(https://fonts.googleapis.com/css?family=Ubuntu+Mono:400,700,400italic);
34+
35+
body { font-family: 'Droid Serif'; }
36+
h1, h2, h3 {
37+
font-family: 'Yanone Kaffeesatz';
38+
font-weight: normal;
39+
}
40+
.remark-code, .remark-inline-code { font-family: 'Ubuntu Mono'; }
41+
42+
blockquote {
43+
border-left: 0.3em solid rgba(0,0,0,0.5);
44+
padding: 0 15px;
45+
font-style: italic;
46+
}
47+
48+
img {
49+
max-width:100%;
50+
}
51+
</style>
52+
</head>
53+
<body>
54+
<textarea id="source">
55+
56+
class: center, middle
57+
58+
# Factory Method
59+
60+
---
61+
62+
# Also known as
63+
64+
* Virtual Constructor
65+
66+
---
67+
68+
# Intent
69+
70+
* Define an interface for creating an object, but let subclasses decide which class to instantiate;
71+
* Factory Method lets a class defer instantiation to subclasses;
72+
73+
---
74+
75+
# Explanation
76+
77+
* [Wikipedia](https://en.wikipedia.org/wiki/Factory_method_pattern) says:
78+
> "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."
79+
80+
* In plain words:
81+
* It provides a way to delegate the instantiation logic to child classes;
82+
83+
---
84+
85+
# Example
86+
87+
* Blacksmith manufactures weapons;
88+
89+
* Elves require Elvish weapons and Orcs require Orcish weapons;
90+
91+
* Depending on the customer at hand, the right type of blacksmith is summoned and, consequently, the right type of weapon;
92+
93+
---
94+
95+
# Programmatic Example
96+
97+
Considering the blacksmith example, we have a blacksmith interface and some implementations for it:
98+
99+
<br />
100+
101+
```
102+
public interface Blacksmith {
103+
Weapon manufactureWeapon(WeaponType weaponType);
104+
}
105+
106+
public class ElfBlacksmith implements Blacksmith {
107+
public Weapon manufactureWeapon(WeaponType weaponType) {
108+
return new ElfWeapon(weaponType);
109+
}
110+
}
111+
112+
public class OrcBlacksmith implements Blacksmith {
113+
public Weapon manufactureWeapon(WeaponType weaponType) {
114+
return new OrcWeapon(weaponType);
115+
}
116+
}
117+
```
118+
119+
---
120+
121+
# Programmatic Example
122+
123+
Now, as the customers come, the correct type of blacksmith is summoned and weapons are instantiated via the factory method `manufactureWeapon()`:
124+
125+
<br />
126+
127+
```
128+
Blacksmith blacksmith = new ElfBlacksmith();
129+
blacksmith.manufactureWeapon(WeaponType.SPEAR);
130+
blacksmith.manufactureWeapon(WeaponType.AXE);
131+
// Elvish weapons are created
132+
```
133+
134+
---
135+
136+
# Diagram
137+
138+
* The diagram below showcases the example:
139+
140+
.center[![Diagram](diagram1.png)]
141+
142+
---
143+
144+
# Applicability
145+
146+
Use the Factory Method pattern when:
147+
148+
* A class can't anticipate the class of objects it must create;
149+
150+
* A class wants its subclasses to specify the objects it creates;
151+
152+
* Classes delegate responsibility to one of several helper subclasses, and you want to localize the knowledge of which helper subclass is the delegate;
153+
154+
---
155+
156+
# Real world examples
157+
158+
* [java.util.Calendar](http://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html#getInstance--)
159+
* [java.util.ResourceBundle](http://docs.oracle.com/javase/8/docs/api/java/util/ResourceBundle.html#getBundle-java.lang.String-)
160+
* [java.text.NumberFormat](http://docs.oracle.com/javase/8/docs/api/java/text/NumberFormat.html#getInstance--)
161+
* [java.nio.charset.Charset](http://docs.oracle.com/javase/8/docs/api/java/nio/charset/Charset.html#forName-java.lang.String-)
162+
* [java.net.URLStreamHandlerFactory](http://docs.oracle.com/javase/8/docs/api/java/net/URLStreamHandlerFactory.html#createURLStreamHandler-java.lang.String-)
163+
* [java.util.EnumSet](https://docs.oracle.com/javase/8/docs/api/java/util/EnumSet.html#of-E-)
164+
* [javax.xml.bind.JAXBContext](https://docs.oracle.com/javase/8/docs/api/javax/xml/bind/JAXBContext.html#createMarshaller--)
165+
166+
---
167+
168+
# Credits
169+
170+
* [Design Patterns: Elements of Reusable Object-Oriented Software](http://www.amazon.com/Design-Patterns-Elements-Reusable-Object-Oriented/dp/0201633612)
171+
172+
---
173+
174+
# Tutorials
175+
176+
* Source code http://java-design-patterns.com/patterns/factory-method/
177+
178+
</textarea>
179+
<script src="https://gnab.github.io/remark/downloads/remark-latest.min.js">
180+
</script>
181+
<script>
182+
var slideshow = remark.create();
183+
</script>
184+
</body>
185+
</html>

0 commit comments

Comments
 (0)