Add all 23 GoF design pattern implementations (2026-06-13)

This commit is contained in:
Ankur
2026-06-13 21:44:56 +05:30
commit a5beb61425
106 changed files with 2977 additions and 0 deletions

View 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 + ")"; }
}

View File

@@ -0,0 +1,7 @@
package iterator;
/** Our custom Iterator interface (mirrors java.util.Iterator) */
public interface BookIterator {
boolean hasNext();
Book next();
}

View 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;
}
}
}

View 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 ===");
}
}