Javaã«ãããã¶ã¤ã³ãã¿ã¼ã³ã®2åç®ã§ãï¼
ææã¯ãåèæç®ã®ãJavaè¨èªã§å¦ã¶ãã¶ã¤ã³ãã¿ã¼ã³å
¥é第3çãã使ããã¦é ãã¾ãã
ä»åã¯ãTemplate Methodãã¿ã¼ã³ã§ãã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ï¼ãã¼ã¿æ§é ãä¿æããã¯ã©ã¹ã¨ãããã«å¯¾ãã¦å¦çãè¡ãã¯ã©ã¹ãåé¢ãã
ä»åã¯ããTemplate Methodãã¿ã¼ã³ãããã£ã¦ããã¾ãï¼
ãµã³ãã«ã³ã¼ãã®ç解
Template Methodãã¿ã¼ã³ã¯ãå¦çã®æµãã¯æ½è±¡ã¯ã©ã¹ã§å®ç¾©ãã¦ãããåå¦çã®å
·ä½çãªå
容ã¯ãããããã®ãµãã¯ã©ã¹ã§å®è£
ãããã¶ã¤ã³ãã¿ã¼ã³ã§ãã
åãããããä¾ãããã¾ããã
Template Method パターン - Wikipedia
æç« ãæ¸ãæµãããããããã¢ã¤ãã ï¼ããã£ï¼ãããã¿ã¨ãæ½è±¡ã¯ã©ã¹ã§å®ç¾©ãã¦ããã¾ãã
æ®éã®æç« ã®ãµãã¯ã©ã¹ã§ã¯ãããããã¢ã¤ãã ï¼ããã£ï¼ãããã¿ããé常ã®æç« ã¨ãã¦ãç®æ¡æ¸ãã«æ¸ãã¦ãã¾ããä¸æ¹ãHTMLã®ãµãã¯ã©ã¹ã§ã¯ãåãæµãããHTMLã®ç®æ¡æ¸ãã®æ§æã使ç¨ãã¦æ¸ãã¦ãã¾ãã
å¦çã®æµãã¯åãã ãã©ãããããã®å¦çã®å
容ãå°ããã¤éããããªå®è£
ã®å ´åã«å½¹ç«ã¡ããã§ãã
ä»åã®PlantUMLã®ã¯ã©ã¹å³
PlantUMLã§ä½æããã¯ã©ã¹å³ã®ã³ã¼ãã¨ç»åãã¡ã¤ã«ã§ãã
@startuml
abstract class AbstractDisplay {
+ {abstract} void open()
+ {abstract} void print()
+ {abstract} void close()
+ final void display()
}
class CharDisplay {
- char ch
+ void open()
+ void print()
+ void close()
}
AbstractDisplay <|-- CharDisplay
class StringDisplay {
- String string
- int width
- void printLine()
+ void open()
+ void print()
+ void close()
}
AbstractDisplay <|-- StringDisplay
@enduml
Mainã¯ã©ã¹ï¼ä½¿ãå´ã®ã¯ã©ã¹ï¼
ã¾ãã¯ãTemplate Methodãã¿ã¼ã³ã©ã使ãããã®ããè¦ãããã«ãMainã¯ã©ã¹ãè¦ã¦ããã¾ãã
åããããããªãã¨æãã®ã§ãå
ã«å®è¡çµæã示ãã¾ãã
public class Main {
public static void main(String[] args) {
AbstractDisplay d1 = new CharDisplay('H');
AbstractDisplay d2 = new StringDisplay("Hello, world.");
d1.display();
d2.display();
}
}
å®è¡çµæ
Template Methodã®ã¯ã©ã¹ã¯ãAbstractDisplay
ã§ãã
ã½ã¼ã¹ã³ã¼ãã®ã³ã¡ã³ãã«ãããã¾ãããå¦çã®æµãã¯ãAbstractDisplay
ã® display()
ã§å®ç¾©ããã¦ãã¦ãåå¦çï¼ä¸ã«ã½ã¼ã¹ã示ãã¾ãï¼ã¯ãCharDisplay
㨠StringDisplay
ã§ãããããå®ç¾©ããã¦ããããã§ãã
ããã ãã§ã¯åãããªãã®ã§ãããããã®ã½ã¼ã¹ã³ã¼ããè¦ã¦ããã¾ãã
AbstractDisplayã¯ã©ã¹
Template Methodã®ã¯ã©ã¹ã® AbstractDisplay
ã§ãã
public abstract class AbstractDisplay {
public abstract void open();
public abstract void print();
public abstract void close();
public final void display() {
open();
for (int i = 0; i < 5; i++) {
print();
}
close();
}
}
æ½è±¡ã¯ã©ã¹ãå®ããå¦çã®æµãã¯ãdisplay()
ã§ãããæåã« open()
ãã³ã¼ã«ãã¦ã次ã¯ãprint()
ã5åã³ã¼ã«ããæå¾ã¯ãclose()
ãã³ã¼ã«ãã¦ãã¾ãã
display()
ã¯ã'final'ï¼ãªã¼ãã¼ã©ã¤ãã§ããªãï¼ã¨ãã¦ããã¨ãããéè¦ãªããã§ããããããããããããããµãã¯ã©ã¹ã§ä¸æ¸ãã§ãã¦ãã¾ãã¨ããµãã¯ã©ã¹ãããã«ç¶æ¿ããå«ã¯ã©ã¹ãä½ãããã㧠display()
ã«å¯¾ãã¦ãå¦çé ãå¤ãã¦ãã¾ããããªä¸æ¸ãããã¦ãã¾ãã¨ãæ£ããåããªããã®ã«ãªã£ã¦ãã¾ãããã ã¨æãã¾ãã
open()
ãprint()
ãclose()
ã¯ããµãã¯ã©ã¹ã§å®ç¾©ãã¦ããããã§ãã
CharDisplayã¯ã©ã¹
次ã¯ãCharDisplayã¯ã©ã¹ã§ãã
public class CharDisplay extends AbstractDisplay {
private char ch;
public CharDisplay(char ch) {
this.ch = ch;
}
@Override
public void open() {
System.out.print("<<");
}
@Override
public void print() {
System.out.print(ch);
}
@Override
public void close() {
System.out.println(">>");
}
}
ãã©ã¤ãã¼ãã¡ã³ãå¤æ°ã¨ãã¦ãch
ãæã¡ãã³ã³ã¹ãã©ã¯ã¿ã§ã¯ãå¼æ°ã§ ch
ãåæåãã¦ãã¾ãã
open()
ã§ã¯ <<
ãæ¨æºåºåã«è¡¨ç¤ºãï¼æ¹è¡ã¯åºåããªãï¼ãprint()
ã§ã¯ ch
ã表示ãã¾ããæå¾ã«ãclose()
ã§ã¯ >>
ãåºåãã¦ãã¾ãï¼æ¹è¡ãåºåããï¼ã
Mainã¯ã©ã¹ã§ãCharDisplayã¯ã©ã¹ã®ã¤ã³ã¹ã¿ã³ã¹ãä½ãã¨ãã«ãå¼æ°ã« H
ãæå®ãã¦ãã¾ããããã£ã¦ãå®è¡çµæã®éãã<<HHHHH>>
ã¨è¡¨ç¤ºãããããã§ããããªãã»ã©ã
StringDisplayã¯ã©ã¹
次ã¯ãStringDisplayã¯ã©ã¹ã§ãã
public class StringDisplay extends AbstractDisplay {
private String string;
private int width;
public StringDisplay(String string) {
this.string = string;
this.width = string.length();
}
@Override
public void open() {
printLine();
}
@Override
public void print() {
System.out.println("|" + string + "|");
}
@Override
public void close() {
printLine();
}
private void printLine() {
System.out.print("+");
for (int i = 0; i < width; i++) {
System.out.print("-");
}
System.out.println("+");
}
}
ãã©ã¤ãã¼ãã¡ã³ãå¤æ°ã¨ãã¦ãstring
㨠width
ãæã¡ãã³ã³ã¹ãã©ã¯ã¿ã§ã¯ãå¼æ°ã§ string
ãåæåããwidth
ã¯ãã®é·ãã§åæåãã¦ãã¾ãã
open()
㨠close()
ã¯åãå®è£
ã«ãªã£ã¦ããã+
ãåºåããå¾ãstring
ã¨åãé·ãã® -
ãåºåãã¦ãã¾ããprint()
ã§ã¯ |
ã®å¾ãstring
ã表示ããæå¾ã«ã|
ã表示ãã¦ãã¾ãã
Mainã¯ã©ã¹ã§ãStringDisplayã¯ã©ã¹ã®ã¤ã³ã¹ã¿ã³ã¹ãä½ãã¨ãã«ãå¼æ°ã« Hello, world.
ãæå®ãã¦ãã¾ããããã£ã¦ãå®è¡çµæã®éãã以ä¸ã¨è¡¨ç¤ºãããããã§ãã
+++++++++++++++
|Hello, world.|
|Hello, world.|
|Hello, world.|
|Hello, world.|
|Hello, world.|
+++++++++++++++
ãããã«
ä»åã¯Template Methodãã¿ã¼ã³ãå¦ã³ã¾ããã
ä¼¼ããããªå¦çã®æµãã®ã¯ã©ã¹ã¯ãTemplate Methodãã¿ã¼ã³ãç¨ããã¨ãåããããªå®è£
ãåå¨ãããã¨ãé²ããã¨ãã§ãã¾ãããã¾ããå
ã«å¦çã®æµãã決ã¾ã£ã¦ããå ´åã«ãééããªãããããå®ããã¨ãå®ç¾ã§ãããã§ãã
ä»åã¯ä»¥ä¸ã§ãï¼
æå¾ã¾ã§ãèªã¿ããã ãããããã¨ããããã¾ããã