Add all 23 GoF design pattern implementations
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 ===");
|
||||
}
|
||||
}
|
||||
31
03-behavioral/iterator/README.md
Normal file
31
03-behavioral/iterator/README.md
Normal file
@@ -0,0 +1,31 @@
|
||||
# Iterator Design Pattern — Java Example
|
||||
|
||||
**Pattern:** Behavioral → Iterator
|
||||
**Article:** https://ankurm.com/iterator-design-pattern-java/
|
||||
|
||||
## What this example shows
|
||||
|
||||
A `BookShelf` stores `Book` objects in an internal `ArrayList` but never exposes it. Callers only see the `BookIterator` interface (`hasNext()`/`next()`). `BookShelf` hands out two different concrete iterators: `ForwardIterator` walks every book in order, and `DecadeIterator` filters to only books published in a given decade, computing the next match lazily as `advance()` is called. `Main` exercises both iterators, then prints a line noting that `java.util.Iterator`/for-each work the same way under the hood.
|
||||
|
||||
## How to run
|
||||
|
||||
```bash
|
||||
javac iterator/*.java -d out/iterator
|
||||
java -cp out/iterator iterator.Main
|
||||
```
|
||||
|
||||
Requires Java 25.
|
||||
|
||||
## Post Section ↔ File Mapping
|
||||
|
||||
| Post Section | File(s) |
|
||||
|---|---|
|
||||
| Custom Iterator Implementation — the Iterator interface | `BookIterator.java` |
|
||||
| Custom Iterator Implementation — the element type | `Book.java` |
|
||||
| Custom Iterator Implementation — the Aggregate (BookShelf, ForwardIterator, DecadeIterator) | `BookShelf.java` |
|
||||
| Custom Iterator Implementation — wiring it together | `Main.java` |
|
||||
|
||||
Note: the "How Java's For-Each Loop Uses Iterator" snippet (`Iterable<Book>` example) is illustrative only — it is not part of this repository's runnable example.
|
||||
|
||||
Article: https://ankurm.com/iterator-design-pattern-java/
|
||||
All patterns: https://ankurm.com/design-patterns-java/
|
||||
Reference in New Issue
Block a user