-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFactory.java
More file actions
32 lines (25 loc) · 785 Bytes
/
Factory.java
File metadata and controls
32 lines (25 loc) · 785 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import static org.junit.Assert.assertEquals;
// A factory method creates other objects
// In this case the factory automates the setting of auto-id int
class Person0 {
public int id;
public String name;
public Person0(int id, String name) {
this.id = id;
this.name = name;
}
}
class PersonFactory {
int currId = 0;
public Person0 createPerson(String name) {
return new Person0(currId++, name);
}
}
class DemoFactory {
public static void main(String[] args) {
PersonFactory factory = new PersonFactory();
assertEquals(0, factory.createPerson("bob").id);
assertEquals(1, factory.createPerson("john").id);
assertEquals(2, factory.createPerson("tim").id);
}
}