Skip to content

Commit a743244

Browse files
committed
Iterator design pattern - BEFORE CODE for Face book friends project
1 parent 6deb6b9 commit a743244

2 files changed

Lines changed: 61 additions & 2 deletions

File tree

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,46 @@
11
package com.premaseem;
22

3+
import java.util.List;
4+
35
/*
46
@author: Aseem Jain
57
@title: Design Patterns with Java 9
68
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
7-
@copyright: 2018 Packt Publication
89
*/
910
public class Client {
1011
public static void main (String[] args) {
11-
System.out.println("Singleton cook example ");
12+
System.out.println("Iterator Pattern BEFORE CODE");
13+
14+
FaceBook faceBook = new FaceBook();
15+
16+
// Data structure is exposed
17+
List<String> friendsList = faceBook.getFriendsList();
18+
19+
// print friends with normal conventional loop
20+
for (int i = 0; i < friendsList.size(); i++) {
21+
System.out.println(friendsList.get(i));
22+
}
23+
24+
// Un intentional addition of data
25+
friendsList.add("Lady gaga");
26+
27+
// print friends with normal conventional loop
28+
for (int i = 0; i < friendsList.size(); i++) {
29+
System.out.println(friendsList.get(i));
30+
}
31+
32+
// Clients can accidentally or maliciously trash data structure.
33+
friendsList.clear();
34+
35+
// print friends with normal conventional loop
36+
for (int i = 0; i < friendsList.size(); i++) {
37+
System.out.println(friendsList.get(i));
38+
}
39+
40+
// Lesson Learned
41+
// 1. Underlying Data structure is exposed
42+
// 2. Data can be manipulated
43+
// 3. Data can be get trashed accidentally
44+
// 4. Data traversal is not easy or uniform
1245
}
1346
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.premaseem;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/*
7+
@author: Aseem Jain
8+
@title: Design Patterns with Java 9
9+
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
10+
*/
11+
public class FaceBook {
12+
13+
List<String> friendsList = new ArrayList<>();
14+
15+
public FaceBook () {
16+
// initialize with seed data
17+
friendsList.add("Aseem Jain");
18+
friendsList.add("James Bond");
19+
friendsList.add("Spider Man");
20+
friendsList.add("Super Man");
21+
}
22+
23+
public List<String> getFriendsList () {
24+
return friendsList;
25+
}
26+
}

0 commit comments

Comments
 (0)