design patterns : iterator
This commit is contained in:
30
design-pattern/gof/src/iterator/after/Board.java
Normal file
30
design-pattern/gof/src/iterator/after/Board.java
Normal 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);
|
||||
}
|
||||
}
|
||||
30
design-pattern/gof/src/iterator/after/Client.java
Normal file
30
design-pattern/gof/src/iterator/after/Client.java
Normal 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());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
31
design-pattern/gof/src/iterator/after/Post.java
Normal file
31
design-pattern/gof/src/iterator/after/Post.java
Normal 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;
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
22
design-pattern/gof/src/iterator/before/Board.java
Normal file
22
design-pattern/gof/src/iterator/before/Board.java
Normal 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));
|
||||
}
|
||||
}
|
||||
29
design-pattern/gof/src/iterator/before/Client.java
Normal file
29
design-pattern/gof/src/iterator/before/Client.java
Normal 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());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
31
design-pattern/gof/src/iterator/before/Post.java
Normal file
31
design-pattern/gof/src/iterator/before/Post.java
Normal 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user