Add all 23 GoF design pattern implementations (2026-06-13)
This commit is contained in:
14
03-behavioral/iterator/Book.java
Normal file
14
03-behavioral/iterator/Book.java
Normal file
@@ -0,0 +1,14 @@
|
||||
package iterator;
|
||||
|
||||
public class Book {
|
||||
private final String title;
|
||||
private final String author;
|
||||
private final int year;
|
||||
|
||||
public Book(String title, String author, int year) {
|
||||
this.title = title; this.author = author; this.year = year;
|
||||
}
|
||||
public String getTitle() { return title; }
|
||||
public int getYear() { return year; }
|
||||
@Override public String toString() { return "\"" + title + "\" by " + author + " (" + year + ")"; }
|
||||
}
|
||||
7
03-behavioral/iterator/BookIterator.java
Normal file
7
03-behavioral/iterator/BookIterator.java
Normal file
@@ -0,0 +1,7 @@
|
||||
package iterator;
|
||||
|
||||
/** Our custom Iterator interface (mirrors java.util.Iterator) */
|
||||
public interface BookIterator {
|
||||
boolean hasNext();
|
||||
Book next();
|
||||
}
|
||||
57
03-behavioral/iterator/BookShelf.java
Normal file
57
03-behavioral/iterator/BookShelf.java
Normal file
@@ -0,0 +1,57 @@
|
||||
package iterator;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Aggregate — the collection. Exposes iterators without
|
||||
* revealing its internal storage structure.
|
||||
*/
|
||||
public class BookShelf {
|
||||
private final List<Book> books = new ArrayList<>();
|
||||
|
||||
public void addBook(Book book) { books.add(book); }
|
||||
|
||||
/** Standard forward iterator */
|
||||
public BookIterator iterator() {
|
||||
return new ForwardIterator();
|
||||
}
|
||||
|
||||
/** Filtered iterator — only books from a specific decade */
|
||||
public BookIterator iteratorByDecade(int decade) {
|
||||
return new DecadeIterator(decade);
|
||||
}
|
||||
|
||||
// --- Inner iterator implementations ---
|
||||
|
||||
private class ForwardIterator implements BookIterator {
|
||||
private int index = 0;
|
||||
|
||||
@Override public boolean hasNext() { return index < books.size(); }
|
||||
@Override public Book next() { return books.get(index++); }
|
||||
}
|
||||
|
||||
private class DecadeIterator implements BookIterator {
|
||||
private final int decade;
|
||||
private int index = 0;
|
||||
private Book nextBook;
|
||||
|
||||
DecadeIterator(int decade) {
|
||||
this.decade = decade;
|
||||
advance();
|
||||
}
|
||||
|
||||
private void advance() {
|
||||
nextBook = null;
|
||||
while (index < books.size()) {
|
||||
Book b = books.get(index++);
|
||||
if (b.getYear() / 10 * 10 == decade) { nextBook = b; break; }
|
||||
}
|
||||
}
|
||||
|
||||
@Override public boolean hasNext() { return nextBook != null; }
|
||||
@Override public Book next() {
|
||||
Book b = nextBook; advance(); return b;
|
||||
}
|
||||
}
|
||||
}
|
||||
37
03-behavioral/iterator/Main.java
Normal file
37
03-behavioral/iterator/Main.java
Normal file
@@ -0,0 +1,37 @@
|
||||
package iterator;
|
||||
|
||||
/**
|
||||
* Iterator Design Pattern — Runnable Demo
|
||||
* Run: javac iterator/*.java -d out/iterator && java -cp out/iterator iterator.Main
|
||||
* Article: https://ankurm.com/iterator-design-pattern-java/
|
||||
*/
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
System.out.println("=== Iterator Design Pattern Demo ===\n");
|
||||
|
||||
BookShelf shelf = new BookShelf();
|
||||
shelf.addBook(new Book("Clean Code", "Robert Martin", 2008));
|
||||
shelf.addBook(new Book("The Pragmatic Programmer","Andrew Hunt", 1999));
|
||||
shelf.addBook(new Book("Effective Java", "Joshua Bloch", 2001));
|
||||
shelf.addBook(new Book("Design Patterns", "Gang of Four", 1994));
|
||||
shelf.addBook(new Book("Refactoring", "Martin Fowler", 2018));
|
||||
shelf.addBook(new Book("Working Effectively with Legacy Code", "Michael Feathers", 2004));
|
||||
|
||||
System.out.println("-- All books (forward iterator) --");
|
||||
BookIterator it = shelf.iterator();
|
||||
while (it.hasNext()) {
|
||||
System.out.println(" " + it.next());
|
||||
}
|
||||
|
||||
System.out.println("\n-- Books from the 2000s --");
|
||||
BookIterator it2000s = shelf.iteratorByDecade(2000);
|
||||
while (it2000s.hasNext()) {
|
||||
System.out.println(" " + it2000s.next());
|
||||
}
|
||||
|
||||
System.out.println("\n-- JDK Iterable: same pattern, different vocabulary --");
|
||||
System.out.println(" java.util.Iterator is our BookIterator; for-each uses it under the hood");
|
||||
|
||||
System.out.println("\n=== Demo complete ===");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user