design patterns : iterator

This commit is contained in:
haerong22
2021-12-15 01:13:57 +09:00
parent 440b57ea56
commit 2cbcbcf901
7 changed files with 198 additions and 0 deletions

View File

@@ -0,0 +1,30 @@
package iterator.after;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Board {
List<Post> posts = new ArrayList<>();
public List<Post> getPosts() {
return posts;
}
public void setPosts(List<Post> posts) {
this.posts = posts;
}
public void addPost(String content) {
this.posts.add(new Post(content));
}
public Iterator<Post> getDefaultIterator() {
return posts.iterator();
}
public Iterator<Post> getRecentPostIterator() {
return new RecentPostIterator(this.posts);
}
}

View File

@@ -0,0 +1,30 @@
package iterator.after;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
public class Client {
public static void main(String[] args) {
Board board = new Board();
board.addPost("디자인 패턴 게임");
board.addPost("선생님, 저랑 디자인 패턴 하나 학습하시겠습니까?");
board.addPost("지금 이 자리에 계신 여러분들은 모두 디자인 패턴을 학습하고 계신 분들입니다.");
// TODO 들어간 순서대로 순회하기
List<Post> posts = board.getPosts();
Iterator<Post> iterator = board.getDefaultIterator();
while (iterator.hasNext()) {
System.out.println(iterator.next().getTitle());
}
// TODO 가장 최신 글 먼저 순회하기
Iterator<Post> recentPostIterator = board.getRecentPostIterator();
while (recentPostIterator.hasNext()) {
System.out.println(recentPostIterator.next().getTitle());
}
}
}

View File

@@ -0,0 +1,31 @@
package iterator.after;
import java.time.LocalDateTime;
public class Post {
private String title;
private LocalDateTime createdDateTime;
public Post(String title) {
this.title = title;
this.createdDateTime = LocalDateTime.now();
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public LocalDateTime getCreatedDateTime() {
return createdDateTime;
}
public void setCreatedDateTime(LocalDateTime createdDateTime) {
this.createdDateTime = createdDateTime;
}
}

View File

@@ -0,0 +1,25 @@
package iterator.after;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
public class RecentPostIterator implements Iterator<Post> {
private Iterator<Post> internalIterator;
public RecentPostIterator(List<Post> posts) {
Collections.sort(posts, (p1, p2) -> p2.getCreatedDateTime().compareTo(p1.getCreatedDateTime()));
this.internalIterator = posts.iterator();
}
@Override
public boolean hasNext() {
return this.internalIterator.hasNext();
}
@Override
public Post next() {
return this.internalIterator.next();
}
}

View File

@@ -0,0 +1,22 @@
package iterator.before;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Board {
List<Post> posts = new ArrayList<>();
public List<Post> getPosts() {
return posts;
}
public void setPosts(List<Post> posts) {
this.posts = posts;
}
public void addPost(String content) {
this.posts.add(new Post(content));
}
}

View File

@@ -0,0 +1,29 @@
package iterator.before;
import java.util.Collections;
import java.util.List;
public class Client {
public static void main(String[] args) {
Board board = new Board();
board.addPost("디자인 패턴 게임");
board.addPost("선생님, 저랑 디자인 패턴 하나 학습하시겠습니까?");
board.addPost("지금 이 자리에 계신 여러분들은 모두 디자인 패턴을 학습하고 계신 분들입니다.");
// TODO 들어간 순서대로 순회하기
List<Post> posts = board.getPosts();
for (int i = 0 ; i < posts.size() ; i++) {
Post post = posts.get(i);
System.out.println(post.getTitle());
}
// TODO 가장 최신 글 먼저 순회하기
Collections.sort(posts, (p1, p2) -> p2.getCreatedDateTime().compareTo(p1.getCreatedDateTime()));
for (int i = 0 ; i < posts.size() ; i++) {
Post post = posts.get(i);
System.out.println(post.getTitle());
}
}
}

View File

@@ -0,0 +1,31 @@
package iterator.before;
import java.time.LocalDateTime;
public class Post {
private String title;
private LocalDateTime createdDateTime;
public Post(String title) {
this.title = title;
this.createdDateTime = LocalDateTime.now();
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public LocalDateTime getCreatedDateTime() {
return createdDateTime;
}
public void setCreatedDateTime(LocalDateTime createdDateTime) {
this.createdDateTime = createdDateTime;
}
}