-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPerson.txt
More file actions
86 lines (68 loc) · 2.27 KB
/
Person.txt
File metadata and controls
86 lines (68 loc) · 2.27 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
1. What design principles does this code violate?
Information Hiding
High-Quality Abstraction
Decomposition
2. Refactor the code to improve its design.
Person.java
public class Person {
private String last;
private String first;
private String middle;
public Person(String last, String first, String middle) {
this.last = last;
this.first = first;
this.middle = middle;
}
public String getLast(){
return last;
}
public String getFirst(){
return first;
}
public String getMiddle(){
return middle;
}
}
PersonClient.java
// The clients are in one file for convenience;
// imagine them as non-test methods in separate client classes.
import junit.framework.TestCase;
import java.io.*
public class PersonClient extends TestCase {
public PersonClient(String name) {super(name);}
public void client1(Writer out, Person person) throws IOException {
out.write(person.getFirst());
out.write(" ");
if (person.getMiddle() != null) {
out.write(person.getMiddle());
out.write(" ");
}
out.write(person.getLast());
}
public String client2(Person person) {
String result = person.getLast() + ", " + person.getFirst();
if (person.getMiddle() != null)
result += " " + person.getMiddle();
return result;
}
public void testClients() throws IOException {
Person bobSmith = new Person("Smith", "Bob", null);
Person jennyJJones = new Person("Jones", "Jenny", "J");
StringWriter out = new StringWriter();
client1(out, bobSmith);
assertEquals("Bob Smith", out.toString());
out = new StringWriter();
client1(out, jennyJJones);
assertEquals("Jenny J Jones", out.toString());
assertEquals("Smith, Bob", client2(bobSmith));
assertEquals("Jones, Jenny J", client2(jennyJJones));
out = new StringWriter();
client2(out, bobSmith);
assertEquals("Smith, Bob", out.toString());
out = new StringWriter();
client2(out, jennyJJones);
assertEquals("Jones, Jenny J", out.toString());
assertEquals("Smith, Bob", client2(bobSmith));
assertEquals("Jones, Jenny J", client2(jennyJJones));
}
}