浏览代码

update README

Froilan Miranda 6 年前
父节点
当前提交
a780416ffb
共有 1 个文件被更改,包括 89 次插入0 次删除
  1. 89
    0
      README.md

+ 89
- 0
README.md 查看文件

@@ -0,0 +1,89 @@
1
+# Lambdas Exercise
2
+Suppose that you are creating a social networking application. You want to create a feature that enables an administrator to perform any kind of action, such as sending a message, on members of the social networking application that satisfy certain criteria. The following table describes this use case in detail:
3
+
4
+Suppose that members of this social networking application are represented by the following Person class:
5
+
6
+```
7
+public class Person {
8
+
9
+    public enum Sex {
10
+        MALE, FEMALE
11
+    }
12
+
13
+    String name;
14
+    LocalDate birthday;
15
+    Sex gender;
16
+    String emailAddress;
17
+
18
+    public int getAge() {
19
+        // ...
20
+    }
21
+
22
+    public void printPerson() {
23
+        // ...
24
+    }
25
+}
26
+```
27
+
28
+Suppose that the members of your social networking application are stored in a `List<Person>` instance.
29
+
30
+### Approach 1: Create Methods That Search for Members That Match One Characteristic
31
+One simplistic approach is to create several methods; each method searches for members that match one characteristic, such as gender or age. The following method prints members that are older than a specified age:
32
+
33
+```
34
+public static void printPersonsOlderThan(List<Person> roster, int age) {
35
+    for (Person p : roster) {
36
+        if (p.getAge() >= age) {
37
+            p.printPerson();
38
+        }
39
+    }
40
+}
41
+```
42
+### Approach 2: Create More Generalized Search Methods
43
+The following method is more generic than printPersonsOlderThan; it prints members within a specified range of ages:
44
+
45
+```
46
+public static void printPersonsWithinAgeRange(
47
+    List<Person> roster, int low, int high) {
48
+    for (Person p : roster) {
49
+        if (low <= p.getAge() && p.getAge() < high) {
50
+            p.printPerson();
51
+        }
52
+    }
53
+}
54
+```
55
+What if you want to print members of a specified sex, or a combination of a specified gender and age range? What if you decide to change the Person class and add other attributes such as relationship status or geographical location? Although this method is more generic than printPersonsOlderThan, trying to create a separate method for each possible search query can still lead to brittle code. You can instead separate the code that specifies the criteria for which you want to search in a different class.
56
+
57
+### Approach 3: Specify Search Criteria Code in a Local Class
58
+
59
+The following method prints members that match search criteria that you specify:
60
+
61
+```
62
+public static void printPersons(
63
+    List<Person> roster, CheckPerson tester) {
64
+    for (Person p : roster) {
65
+        if (tester.test(p)) {
66
+            p.printPerson();
67
+        }
68
+    }
69
+}
70
+```
71
+
72
+This method checks each Person instance contained in the List parameter roster whether it satisfies the search criteria specified in the CheckPerson parameter tester by invoking the method tester.test. If the method tester.test returns a true value, then the method printPersons is invoked on the Person instance.
73
+
74
+To specify the search criteria, you implement the CheckPerson interface:
75
+
76
+```
77
+interface CheckPerson {
78
+    boolean test(Person p);
79
+}
80
+```
81
+
82
+### Your Mission
83
+Using the 3rd approach, create a branch for each of the following implementations:
84
+
85
+* Specify Search Criteria Code in a Local Class
86
+* Specify Search Criteria Code in an Anonymous Class
87
+* Specify Search Criteria Code with a Lambda Expression
88
+
89
+**Don't forget your test!**