Javaã«ãããã¶ã¤ã³ãã¿ã¼ã³ã®1åç®ã§ãï¼
ææã¯ãåèæç®ã®ãJavaè¨èªã§å¦ã¶ãã¶ã¤ã³ãã¿ã¼ã³å
¥é第3çãã使ããã¦é ãã¾ãã
ä»åã¯ãSingletonãã¿ã¼ã³ã§ãã23åã®ãã¶ã¤ã³ãã¿ã¼ã³ã®ä¸ã§ããæãç解ãããããã¿ã¼ã³ã®ä¸ã¤ã ã¨æãã¾ãã
ã§ã¯ããã£ã¦ããã¾ãããï¼
åèæç®
åèæç®ã®ãµã³ãã«ããã°ã©ã ã®ãã¦ã³ãã¼ã
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ï¼ãã¼ã¿æ§é ãä¿æããã¯ã©ã¹ã¨ãããã«å¯¾ãã¦å¦çãè¡ãã¯ã©ã¹ãåé¢ãã
ä»åã¯ããSingletonãã¿ã¼ã³ãããã£ã¦ããã¾ãï¼
ãµã³ãã«ã³ã¼ãã®ç解
ã·ã³ã°ã«ãã³ã¯ã対象ã¯ã©ã¹ã®ã¤ã³ã¹ã¿ã³ã¹ãå¿
ã1ã¤ã ãã«ãªããã¨ãä¿è¨¼ã§ãããã¶ã¤ã³ãã¿ã¼ã³ã§ãã
ç¨éã¨ãã¦ããããã調ã¹ã¦ã¿ã¦ã¿ãã¨ãããå®ç¨çãªä¾ã¨ãã¦ã¯ã以ä¸ã®ãããªãã®ã§ããã
- ãã¬ã¼ã¯ã©ã¹
- ã©ããã使ã£ã¦ããã°ã®è¨å®ã¯ä¸ç·ã§ãã£ã¦ã»ãã
- ãã使ãããï¼staticå¤æ°ã¯å®è¡éå§æã«çæãããï¼
- 管çã¯ã©ã¹
- ãã¼ãã¦ã§ã¢è³æºï¼ã¡ã¢ãªãã½ã±ãããªã©ç©ççã«å¶å¾¡å¯¾è±¡ãåºå®ããã¦ããï¼
- 対象ã®è³æºã®ç¶æ
ãªã©ã¯ãè¤æ°ãã£ããå°ã
確ãã«ãã·ã³ã°ã«ãã³ãã¿ã¼ã³ã使ãã¨ãé½åãè¯ãããã§ããã
TicketMakerã¯ã©ã¹ã®PlantUMLã®ã¯ã©ã¹å³
PlantUMLã§ä½æããã¯ã©ã¹å³ã®ã³ã¼ãã¨ç»åãã¡ã¤ã«ã§ãã
TicketMakerã¯ã©ã¹ã®ã³ã³ã¹ãã©ã¯ã¿ã¯ãã©ã¤ãã¼ãã¡ã½ããã«ãªã£ã¦ãã¾ããããã¯ãå¤ããã¤ã³ã¹ã¿ã³ã¹ãä½æãããªãããã«ããããã§ããã·ã³ã°ã«ãã³ãã¿ã¼ã³ã§ã¯ãã¡ã½ããï¼TicketMakerã¯ã©ã¹ã§ã¯getInstanceã¡ã½ããï¼ã§ã¤ã³ã¹ã¿ã³ã¹ãåå¾ããã¾ãã
@startuml
class TicketMaker {
- int ticket
- {static} TicketMaker singleton
- TicketMaker()
+ {static} TicketMaker getInstance()
+ synchronized int getNextTicketNumber()
}
@endum
Mainã¯ã©ã¹ï¼ã·ã³ã°ã«ãã³ã使ãå´ã®ã¯ã©ã¹ï¼
ã¾ãã¯ãã·ã³ã°ã«ãã³ã®ã¯ã©ã¹ãã©ã使ãããã®ããè¦ãããã«ãMainã¯ã©ã¹ãè¦ã¦ããã¾ãã
ã¾ããå®è¡çµæã示ãã¾ãã
public class Main {
public static void main(String[] args) {
System.out.println("Start.");
for (int i = 0; i < 10; i++) {
System.out.println(i + ":" + TicketMaker.getInstance().getNextTicketNumber());
}
System.out.println("End.");
}
}
å®è¡çµæ
ã·ã³ã°ã«ãã³ã®ã¯ã©ã¹ã¯ãTicketMaker
ã§ãã
ãã±ããã¡ã¼ã«ã¼ï¼ãã±ãããçºè¡ããæ©è½ãæã¤ãã®ï¼ã«å¯¾ãã¦ãgetNextTicketNumber()
ï¼ãã±ããçªå·ãä»ä¸ããï¼ãã³ã¼ã«ããã¨ãã¦ãã¼ã¯ãªï¼å¯ä¸ã®ï¼ãã±ããçªå·ãçºè¡ãã¦ããã¾ãã
ä»åã¯ãforã«ã¼ãã§ããã±ããçªå·ã10åçºè¡ãã¦ãã¾ãããã±ããçªå·ãã1000ãã被ããªãçªå·ã§ã10åçºè¡ããã¦ãããã¨ã確èªã§ãã¾ãã
ãããæ®éã®ã¯ã©ã¹ã§ãè¤æ°ã®ã¤ã³ã¹ã¿ã³ã¹ãåå¨ããå ´åãåããã±ããçªå·ãçºè¡ãããã¨å°ãã¾ãããã·ã³ã°ã«ãã³ã使ãã¨ããããã被ããé²ãããã«é½åãè¯ãããã§ãã
TicketMakerã¯ã©ã¹
ã·ã³ã°ã«ãã³ã¯ã©ã¹ã® TicketMaker
ã§ãã
public class TicketMaker {
private int ticket = 1000;
private static TicketMaker singleton = new TicketMaker();
private TicketMaker() {
}
public static TicketMaker getInstance() {
return singleton;
}
public synchronized int getNextTicketNumber() {
return ticket++;
}
}
TicketMakerã¯ã©ã¹ã¯ããã©ã¤ãã¼ãã¡ã³ãå¤æ°ã¨ãã¦ãticket
ï¼ç¾å¨ã®ãã±ããçªå·ï¼ã¨ãstaticãªãã©ã¤ãã¼ãã¡ã³ãå¤æ°ã¨ãã¦ãsingleton
ï¼èªèº«ã®ã¤ã³ã¹ã¿ã³ã¹ï¼ãæã¡ã¾ãã
TicketMakerã¯ã©ã¹ã®ã³ã³ã¹ãã©ã¯ã¿ã¯ä½ããã¦ãã¾ããã
ãããªãã¯ã¡ã½ããã¨ãã¦ã¯ãsingleton
ï¼èªèº«ã®ã¤ã³ã¹ã¿ã³ã¹ï¼ãè¿ãgetInstance()
ã¨ãã¦ãã¼ã¯ãªãã±ããçªå·ãè¿ã getNextTicketNumber()
ãå®è£
ããã¦ãã¾ãã
getNextTicketNumber()
ã«ã¯ãsynchronized
ãä»ãã¦ãã¾ãããããã¯ãè¤æ°ã¹ã¬ããããåæã«å¼ã°ãã¦ããé çªã«å®è¡ãã¦ãããä»çµã¿ã§ãã
ãã®ä»çµã¿ãå
¥ã£ã¦ããªãå ´åãå
ã«å®è¡ãããã¹ã¬ããããã±ããçªå·ãè¿ããå¾ããã±ããçªå·ãã¤ã³ã¯ãªã¡ã³ãããåã«ãå¥ã®ã¹ã¬ãããå²ãè¾¼ãã§ãåããã±ããçªå·ãè¿ãã¦ãã¾ãå¯è½æ§ãããããªã©ã®åé¡ãçºçããããããã¾ããï¼ã¨ãããã¨ã ã¨æãã¾ãï¼ã
ãããã«
ä»åã¯ã·ã³ã°ã«ãã³ãã¿ã¼ã³ãå¦ã³ã¾ããã
ã·ã³ã°ã«ãã³ãã¿ã¼ã³ã®ã¡ãªããã¨ãå®ç¨çãªäºä¾ã ã£ãã¨æãã¾ãã
ã¯ã©ã¹å³ã®ãããã§ãæ¯åã¢ã¤ãã£ããç»åã«æ©ã¾ãªãã¦è¯ãããã§ãï¼ç¬ï¼
ä»åã¯ä»¥ä¸ã§ãï¼
æå¾ã¾ã§ãèªã¿ããã ãããããã¨ããããã¾ããã