Javaã«ãããã¶ã¤ã³ãã¿ã¼ã³ã®4åç®ã§ãï¼
ææã¯ãåèæç®ã®ãJavaè¨èªã§å¦ã¶ãã¶ã¤ã³ãã¿ã¼ã³å
¥é第3çãã®ãµã³ãã«ã³ã¼ãã使ããã¦é ãã¾ãã
ä»åã¯ãIteratorãã¿ã¼ã³ã§ãã
å®ã¯ã第1åã§æ¸ãã¦ãããã§ãããéä¸ã§çåãåºã¦ãã¦ã解決ã§ããªãã¦ãä¸æ¸ãã«ç ã£ã¦ã¾ããï¼ç¬ï¼
ãã®ãããã話ãããã¨æãã¾ãã
ã§ã¯ããã£ã¦ããã¾ãããï¼
åèæç®
åèæç®ã®ãµã³ãã«ããã°ã©ã ã®ãã¦ã³ãã¼ã
www.hyuki.com
ã¯ããã«
ãJavaã§ãã¶ã¤ã³ãã¿ã¼ã³ãå¦ã¶ãã®è¨äºä¸è¦§ã§ããè¯ãã£ããåèã«ãã¦ãã ããã
Javaã§ãã¶ã¤ã³ãã¿ã¼ã³ã®è¨äºä¸è¦§
å
ã«ã23種é¡ã®ãã¶ã¤ã³ãã¿ã¼ã³ã示ãã¾ãã
åèãµã¤ãï¼デザインパターン (ソフトウェア) - Wikipedia
ãã¶ã¤ã³ãã¿ã¼ã³ä¸è¦§
âçæã«é¢ãããã¿ã¼ã³
ãã»Abstract Factoryï¼é¢é£ããã¤ã³ã¹ã¿ã³ã¹ãç¶æ³ã«å¿ãã¦ãé©åã«çæããæ¹æ³ãæä¾ãã
ãã»Builderï¼è¤ååãããã¤ã³ã¹ã¿ã³ã¹ã®çæéç¨ãé è½ããã
ãã»Factory Methodï¼çæãããã¤ã³ã¹ã¿ã³ã¹ã«ä¾åããªããã¤ã³ã¹ã¿ã³ã¹ã®çææ¹æ³ãæä¾ãã
ãã»Prototypeï¼åæ§ã®ã¤ã³ã¹ã¿ã³ã¹ãçæããããã«ãååã®ã¤ã³ã¹ã¿ã³ã¹ãè¤è£½ãã
ãã»Singletonï¼ããã¯ã©ã¹ã«ã¤ãã¦ãã¤ã³ã¹ã¿ã³ã¹ã1ã¤ããåå¨ããªããã¨ãä¿è¨¼ãã
âæ§é ã«é¢ãããã¿ã¼ã³
ãã»Adapterï¼å
ã
é¢é£æ§ã®ãªã2ã¤ã®ã¯ã©ã¹ãæ¥ç¶ããã¯ã©ã¹ãä½ã
ãã»Bridgeï¼ã¯ã©ã¹ã¨å¼ã³åºãå´ã®éã®æ©æ¸¡ããããã¯ã©ã¹ã§ãå®è£
ãé è½ãã
ãã»Compositeï¼å帰çãªæ§é ã表ç¾ãã
ãã»Decoratorï¼ããã¤ã³ã¹ã¿ã³ã¹ã«å¯¾ããåçã«ä»å æ©è½ã追å ãã
ãã»Facadeï¼è¤æ°ã®ãµãã·ã¹ãã ã®çªå£ã¨ãªãå
±éã®ã¤ã³ã¿ãã§ã¼ã¹ãæä¾ãã
ãã»Flyweightï¼å¤æ°ã®ã¤ã³ã¹ã¿ã³ã¹ãå
±æããã¤ã³ã¹ã¿ã³ã¹ã®æ§ç¯ã®ããã®è² è·ãæ¸ãã
ãã»Proxyï¼å
±éã®ã¤ã³ã¿ãã§ã¼ã¹ãæã¤ã¤ã³ã¹ã¿ã³ã¹ãå
å
ããå©ç¨è
ããã®ã¢ã¯ã»ã¹ã代çãããWrapperã¨ãå¼ã°ãã
âæ¯ãèãã«é¢ãããã¿ã¼ã³
ãã»Chain of Responsibilityï¼ã¤ãã³ãã®éåä¿¡ãè¡ãè¤æ°ã®ãªãã¸ã§ã¯ããéç¶ã«ã¤ãªãããããã®éãã¤ãã³ãã渡ããã¦ããããã«ãã
ãã»Commandï¼è¤æ°ã®ç°ãªãæä½ã«ã¤ãã¦ãããããã«å¯¾å¿ãããªãã¸ã§ã¯ããç¨æãããªãã¸ã§ã¯ããåãæ¿ãããã¨ã§ãæä½ã®åãæ¿ããå®ç¾ãã
ãã»Interpreterï¼æ§æ解æã®ããã«ãææ³è¦åãåæ ããã¯ã©ã¹æ§é ãä½ã
ãã»Iteratorï¼è¤æ°ã®è¦ç´ ãå
å
ãããªãã¸ã§ã¯ãã®ãã¹ã¦ã®è¦ç´ ã«å¯¾ãã¦ãé çªã«ã¢ã¯ã»ã¹ããæ¹æ³ãæä¾ãã
ãã»Mediatorï¼ãªãã¸ã§ã¯ãéã®ç¸äºä½ç¨ã仲ä»ãããªãã¸ã§ã¯ããå®ç¾©ãããªãã¸ã§ã¯ãéã®çµå度ãä½ããã
ãã»Mementoï¼ãã¼ã¿æ§é ã«å¯¾ããä¸é£ã®æä½ã®ãããããè¨é²ãã¦ããã以åã®ç¶æ
ã®å¾©å¸°ã¾ãã¯æä½ã®åç¾ãè¡ããããã«ãã
ãã»Observerï¼ã¤ã³ã¹ã¿ã³ã¹ã®å¤åãä»ã®ã¤ã³ã¹ã¿ã³ã¹ããç£è¦ã§ããããã«ãã
ãã»Stateï¼ãªãã¸ã§ã¯ãã®ç¶æ
ãå¤åããããã¨ã§ãå¦çå
容ãå¤ããããããã«ãã
ãã»Strategyï¼ãã¼ã¿æ§é ã«å¯¾ãã¦é©ç¨ããä¸é£ã®ã¢ã«ã´ãªãºã ãã«ãã»ã«åããã¢ã«ã´ãªãºã ã®åãæ¿ãã容æã«ãã
ãã»Template Methodï¼ã¢ã«ã´ãªãºã ã¯æ½è±¡ã¯ã©ã¹ã§ãå¦çå
容ã¯ãµãã¯ã©ã¹ã§å®ç¾©ãã
ãã»Visitorï¼ãã¼ã¿æ§é ãä¿æããã¯ã©ã¹ã¨ãããã«å¯¾ãã¦å¦çãè¡ãã¯ã©ã¹ãåé¢ãã
ä»åã¯ããIteratorãã¿ã¼ã³ãããã£ã¦ããã¾ãï¼
ãµã³ãã«ã³ã¼ãã®ç解
Iteratorãã¿ã¼ã³ã¯ãéç´ãªãã¸ã§ã¯ãï¼éåä½ï¼ã«å¯¾ããã¢ã¯ã»ã¹æ¹æ³ãæä¾ãããã¨ããã¾ããéç´ãªãã¸ã§ã¯ãã¨ã¯ãä½ããã®ç©ãæ ¼ç´ãããªã¹ãã®ãããªãã®ã®ãã¨ã§ãã
ããã ãèãã¦ããä½ãå¬ããã®ãåãããªãã§ããããªã®ã§ãæ©éãµã³ãã«ã³ã¼ããè¦ã¦ãããã¨ã«ãã¾ãã
ä»åã®PlantUMLã®ã¯ã©ã¹å³
PlantUMLã§ä½æããã¯ã©ã¹å³ã®ã³ã¼ãã¨ç»åãã¡ã¤ã«ã§ãã
Iteratorã¤ã³ã¿ãã§ã¼ã¹ãå®è£
ããBookShelfIteratorã¯ã©ã¹ï¼ã¤ãã¬ã¼ã¿ï¼ã¨ãIterableã¤ã³ã¿ã¼ãã§ã¼ã¹ãå®è£
ããBookShelfã¯ã©ã¹ï¼æ¬æ£ï¼ã¨ãBookã¯ã©ã¹ï¼æ¬ï¼ã®3ã¤ã®å
·è±¡ã¯ã©ã¹ã§æ§æããã¦ãã¾ãã
@startuml
class Book {
- String name
+ String getName()
}
interface Iterator<E> {
+ {abstract} boolean hasNext()
+ {abstract} E next()
}
class BookShelfIterator {
- BookShelf bookShelf
- int index
+ boolean hasNext()
+ Book next()
}
Iterator <|.. BookShelfIterator
BookShelf -o BookShelfIterator
interface Iterable<E> {
+ {abstract} Iterator<E> iterator
}
Iterable -> Iterator : create >
class BookShelf {
- Book[] books
- int last
+ Book getBookAt()
+ void appendBook()
+ int getLength()
+ Iterator<Book> iterator()
}
Iterable <|.. BookShelf
BookShelf o-- Book
note left of BookShelf::iterator
return new BookShelfIterator(this)
end note
@enduml
Mainã¯ã©ã¹ï¼ä½¿ãå´ã®ã¯ã©ã¹ï¼
ã¾ãã¯ãIteratorãã¿ã¼ã³ããã©ã使ãããã®ããè¦ãããã«ãMainã¯ã©ã¹ãè¦ã¦ããã¾ãã
åããããããªãã¨æãã®ã§ãå
ã«å®è¡çµæã示ãã¾ãã
import java.util.Iterator;
public class Main {
public static void main(String[] args) {
BookShelf bookShelf = new BookShelf(4);
bookShelf.appendBook(new Book("Around the World in 80 Days"));
bookShelf.appendBook(new Book("Bible"));
bookShelf.appendBook(new Book("Cinderella"));
bookShelf.appendBook(new Book("Daddy-Long-Legs"));
Iterator<Book> it = bookShelf.iterator();
while (it.hasNext()) {
Book book = it.next();
System.out.println(book.getName());
}
System.out.println();
for (Book book: bookShelf) {
System.out.println(book.getName());
}
System.out.println();
}
}
å®è¡çµæ
ã¾ããBookShelfã¯ã©ã¹ï¼æ¬æ£ï¼ã®ã¤ã³ã¹ã¿ã³ã¹ãä½ãã¾ãã次ã«ãæ§ã
ãªã¿ã¤ãã«ã®4åã®æ¬ã®ã¤ã³ã¹ã¿ã³ã¹ãä½ããæ¬æ£ã«è¿½å ãã¦ããã¾ãã
æå¾ã«ãæ¬æ£ã®Iteratorã使ã£ã¦ãé çªã«æ¬ãåãåºããæ¬ã®ã¿ã¤ãã«ï¼ååï¼ã表示ãã¦ããã¾ããæåã¯ãé常éãIteratorã使ãæ¹æ³ãç¨ãã¦ããã®å¾ãæ¡å¼µforæã使ã£ã¦å®è¡ãã¦ãã¾ãã
ä»åã®Iteratorãã¿ã¼ã³ã¯ãæ¬æ£ããé çªã«æ¬ãåãåºãæ¹æ³ãæä¾ãã¦ãã¾ããIteratorãã¿ã¼ã³ã¯ããã®ã¢ã¯ã»ã¹æ¹æ³ãæä¾ãããã¨ã§ãæ¬æ£ã®ä»æ§ãå¤ãã£ãã¨ãã¦ãããã®æ¬ãåãåºãã¨ããã¯å¤æ´ããå¿
è¦ããªããã¨ãç®çã¨ãã¦ãã¾ãã
ããå°ãä¸èº«ãè¦ã¦ããã¾ãã
Bookã¯ã©ã¹
Bookã¯ã©ã¹ã¯ããã©ã¤ãã¼ãã¡ã³ãå¤æ°ã¨ãã¦ãname
ï¼æ¬ã®ååï¼ãæã¡ã¾ãã
ã¾ããã³ã³ã¹ãã©ã¯ã¿ã¯name
ãå¼æ°ã¨ãã¦ãã¾ãããããªãã¯ã¡ã½ããã¨ãã¦ã¯name
ï¼æ¬ã®ååï¼ãè¿ãgetName()
ãæã£ã¦ãã¾ãã
public class Book {
private String name;
public Book(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
BookShelfã¯ã©ã¹
BookShelfã¯ã©ã¹ï¼æ¬æ£ï¼ã¯ãIterableã¤ã³ã¿ã¼ãã§ã¼ã¹ãå®è£
ããã¯ã©ã¹ã§ãã
Iterableã¤ã³ã¿ã¼ãã§ã¼ã¹ã®å®è£
ã¯ä»¥ä¸ã§ãã
public interface Iterable<T> {
Iterator<T> iterator();
}
BookShelfã¯ã©ã¹ï¼æ¬æ£ï¼ã¯ããã©ã¤ãã¼ãã¡ã³ãå¤æ°ã¨ãã¦ãBookã¯ã©ã¹ã®é
åå¤æ° books
ï¼æ¬ãæ ¼ç´ããï¼ãæã¡ãlast
ï¼ç¾å¨æ ¼ç´ãã¦ããæ¬ã®æ°ï¼ãæã¡ã¾ãã
ã¾ããã³ã³ã¹ãã©ã¯ã¿ã¯ãæ ¼ç´ã§ããæ¬ã®æ° maxsize
ãå¼æ°ã¨ãã¦ãã¾ãããããªãã¯ã¡ã½ããã¨ãã¦ã¯ãã¤ã³ããã¯ã¹ãæå®ãã¦æ¬ãè¿ã getBookAt()
ã¨ãæå®ãããæ¬ãæ¬æ£ã«è¿½å ãã appendBook()
ã¨ãç¾å¨æ ¼ç´ãã¦ããæ¬ã®æ°ãè¿ã getLength()
ã¨ãèªåèªèº«ã® Iterator
ãè¿ã iterator()
ãæã£ã¦ãã¾ãã
Iterableã¤ã³ã¿ã¼ãã§ã¼ã¹ã¯ãIterator
ãè¿ã iterator()
ã¡ã½ããã§ããã¤ã¾ããã¤ãã¬ã¼ã¿ãè¿ãã¡ã½ãããå®è£
ããªãããã¨ãããã¨ã§ããã
import java.util.Iterator;
public class BookShelf implements Iterable<Book> {
private Book[] books;
private int last = 0;
public BookShelf(int maxsize) {
this.books = new Book[maxsize];
}
public Book getBookAt(int index) {
return books[index];
}
public void appendBook(Book book) {
this.books[last] = book;
last++;
}
public int getLength() {
return last;
}
@Override
public Iterator<Book> iterator() {
return new BookShelfIterator(this);
}
}
BookShelfIteratorã¯ã©ã¹
BookShelfIteratorã¯ã©ã¹ï¼ã¤ãã¬ã¼ã¿ï¼ã¯ãIteratorã¤ã³ã¿ãã§ã¼ã¹ãå®è£
ããã¯ã©ã¹ã§ãã
Iteratorã¤ã³ã¿ãã§ã¼ã¹ã®å®è£
ã¯ä»¥ä¸ã§ãã
public interface Iterator<E> {
boolean hasNext();
E next();
}
BookShelfIteratorã¯ã©ã¹ï¼ã¤ãã¬ã¼ã¿ï¼ã¯ããã©ã¤ãã¼ãã¡ã³ãå¤æ°ã¨ãã¦ãBookShelfã¯ã©ã¹ã®ã¤ã³ã¹ã¿ã³ã¹ bookShelf
ï¼æ¬æ£ï¼ãæã¡ãindex
ï¼ã¤ã³ããã¯ã¹ï¼ãæã¡ã¾ãã
ã¾ããã³ã³ã¹ãã©ã¯ã¿ã¯ãBookShelfã¯ã©ã¹ï¼æ¬æ£ï¼ã®ã¤ã³ã¹ã¿ã³ã¹ bookShelf
ãå¼æ°ã¨ãã¦ãã¾ãã
ãããªãã¯ã¡ã½ããã¯ãIteratorã¤ã³ã¿ãã§ã¼ã¹ãå®è£
ããã¡ã½ããã¨ãªã£ã¦ãããIterator
ã¤ã³ã¿ãã§ã¼ã¹ã® hasNext()
ï¼æ¬æ£ã«æ¬¡ã®è¦ç´ ããããã©ããï¼ã¨ã次ã®è¦ç´ ï¼æ¬ï¼ãè¿ã next()
ãæã£ã¦ãã¾ãã
import java.util.Iterator;
import java.util.NoSuchElementException;
public class BookShelfIterator implements Iterator<Book> {
private BookShelf bookShelf;
private int index;
public BookShelfIterator(BookShelf bookShelf) {
this.bookShelf = bookShelf;
this.index = 0;
}
@Override
public boolean hasNext() {
if (index < bookShelf.getLength()) {
return true;
} else {
return false;
}
}
@Override
public Book next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
Book book = bookShelf.getBookAt(index);
index++;
return book;
}
}
Iteratorãã¿ã¼ã³ã®ã¡ãªããã¨èå¯
Iteratorãã¿ã¼ã³ã使ããªãå ´åã¯ãBookShelfIteratorã¯ã©ã¹ï¼ã¤ãã¬ã¼ã¿ï¼ã¯ä½ããããBookã¯ã©ã¹ï¼æ¬ï¼ãBookShelfã¯ã©ã¹ï¼æ¬æ£ï¼ãä½ããMainã¯ã©ã¹ã§ãã®2ã¤ã®ã¯ã©ã¹ã使ã£ã¦ãæ¬æ£ã®æ¬ã«ã¢ã¯ã»ã¹ãããã¨ãå®ç¾ãã¦ããã¨æãã¾ãã
å
·ä½çã«ã¯ãæ¬æ£ã®æ¬ã«ã¢ã¯ã»ã¹ããã®ã«ãforæã§ãæ¬æ£ã¯ã©ã¹ã® getLength()
ãè¶
ããªãããã«ãgetBookAt()
ã使ã£ã¦ãæ¬ã«ã¢ã¯ã»ã¹ããã¨ããæ¹æ³ã§å®ç¾ãã¦ããã¨æãã¾ãï¼ããããå®è£
ã ã¨æãã¾ãï¼ã
Iteratorãã¿ã¼ã³ã使ããã¨ã§ãMainã¯ã©ã¹ã§ã¯ããã®2ã¤ã®ã¡ã³ãé¢æ°ã使ããã¦ãªãã¨ããããã½ã§ãããã¡ãããIteratorã¯ã©ã¹ã® hasNext()
㨠next()
ã代ããã«ä½¿ããã¦ããããã§ãã
ä»åã¯ãMainã¯ã©ã¹ã ããæ¬æ£ã¯ã©ã¹ã使ã£ã¦ããã®ã§ãIteratorãã¿ã¼ã³ã使ã£ãã¡ãªããã¯æãã«ããã®ã§ãããããããã®å ´æã§æ¬æ£ã¯ã©ã¹ã使ããã¦ããã¨ããï¼ããããå ´åã«å¤ãã®æ©æµãåãããã¨ãã§ããï¼ã¨ãæ¬æ£ã¯ã©ã¹ã® getLength()
㨠getBookAt()
ãå¤æ´ãããã¨ãã大ããªå½±é¿ãåºã¦ãã¾ãã¾ãã
Iteratorãã¿ã¼ã³ã使ã£ãå ´åã¯ãBookShelfIteratorã¯ã©ã¹ï¼ã¤ãã¬ã¼ã¿ï¼ã§ããã®å¤æ´ãå¸åã§ããã®ã§ãããããã®BookShelfã¯ã©ã¹ï¼æ¬æ£ï¼ã使ã£ã¦ããå ´æã¯å
¨ãå¤æ´ãå¿
è¦ãªã対å¿ã§ãã¾ãã
ãã®èª¬æãèããã®ã«ãæ°æ¥ããã£ã¦ãã¾ã£ãã¨ããããã§ãï¼ç¬ï¼
Iteratorãã¿ã¼ã³ã«éããªãã§ãããå¤æ´ã«å¼·ãè¨è¨ãèããã¨ãããã¨ã¯ãã¯ã©ã¹ãªã©ã®é¨åå´ã«å¤æ´ããã£ãå ´åã§ãã£ã¦ãã使ãå´ï¼ä»åã®å ´åã¯Mainã¯ã©ã¹ï¼ãå¤æ´ããªãã¦ããããã«è¨è¨ããã¨ãããã¨ã ã¨æãã¾ãã
ä»åã¯ä»¥ä¸ã§ãï¼
ãããã«
ä»åã¯Iteratorãã¿ã¼ã³ãå¦ã³ã¾ããã
Iteratorãã¿ã¼ã³ã使ãããã«ãBookShelfIteratorã¯ã©ã¹ï¼ã¤ãã¬ã¼ã¿ï¼ãä½è¨ã«ä½ã£ã¦ãã¦ããªãIteratorãã¿ã¼ã³ã使ãã¨å¬ããã®ãã説æããã®ãé£ããã£ãã§ãã
æå¾ã¾ã§ãèªã¿ããã ãããããã¨ããããã¾ããã