Javaã«ãããã¶ã¤ã³ãã¿ã¼ã³ã®8åç®ã§ãï¼
ä»åãææã¯ãåèæç®ã®ãJavaè¨èªã§å¦ã¶ãã¶ã¤ã³ãã¿ã¼ã³å
¥é第3çãã®ãµã³ãã«ã³ã¼ãã使ããã¦é ãã¾ãã
ä»åã¯ãAdapterãã¿ã¼ã³ã§ããAdapterã¨ã¯é©åãããã¨ããæå³ã§ããæ¢åã®ã¯ã©ã¹ãå¿
è¦ãªã¤ã³ã¿ãã§ã¼ã¹ã«é©åãããã¤ã¡ã¼ã¸ã§ãã
ã§ã¯ããã£ã¦ããã¾ãããï¼
åèæç®
åèæç®ã®ãµã³ãã«ããã°ã©ã ã®ãã¦ã³ãã¼ã
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ï¼ãã¼ã¿æ§é ãä¿æããã¯ã©ã¹ã¨ãããã«å¯¾ãã¦å¦çãè¡ãã¯ã©ã¹ãåé¢ãã
ã¨ã³ã¸ãã¢ã°ã«ã¼ãã®ã©ã³ãã³ã°ã«åå ä¸ã§ãã
æ°æ¥½ã«ãããã¨ãããããé¡ããããã¾ãð
ä»åã¯ããAdapterãã¿ã¼ã³ãããã£ã¦ããã¾ãï¼
ãµã³ãã«ã³ã¼ãã®ç解
Adapterãã¿ã¼ã³ã¯ãå
ã
é¢é£æ§ã®ãªã2ã¤ã®ã¯ã©ã¹ãæ¥ç¶ããã¯ã©ã¹ãä½ããã¨ããã¾ããæ¢åã®ã¯ã©ã¹ããæ°ããè¦æ±ãããã¤ã³ã¿ãã§ã¼ã¹ã«å¯¾ãã¦ãæ¸ãæããã®ã§ã¯ãªããæ¢åã®ã¯ã©ã¹ãç¶æ¿ãã¦ãæ°ããã¤ã³ã¿ãã§ã¼ã¹ã«é©åããã¯ã©ã¹ãä½ã£ã¦å¯¾å¿ãããã¤ã¡ã¼ã¸ã§ãã
ããã§ã¯ãæ©éãµã³ãã«ã³ã¼ããè¦ã¦ãããã¨ã«ãã¾ãã
ä»åã®PlantUMLã®ã¯ã©ã¹å³
PlantUMLã§ä½æããã¯ã©ã¹å³ã®ã³ã¼ãã¨ç»åãã¡ã¤ã«ã§ãã
æ°ããè¦æ±ãããã¤ã³ã¿ãã§ã¼ã¹ã®Printã¤ã³ã¿ãã§ã¼ã¹ã¨ãæ¢åã®ã¯ã©ã¹ãBannerã¯ã©ã¹ã§ããæ°ããã¤ã³ã¿ãã§ã¼ã¹ã«é©åãããAdapterãã¿ã¼ã³ãé©ç¨ããã®ãPrintBannerã¯ã©ã¹ã§ããã
Mainã¯ã©ã¹ï¼ä½¿ãå´ã®ã¯ã©ã¹ï¼ã¯Printã¤ã³ã¿ãã§ã¼ã¹ãç¥ãã ãã§ãä»ã®2ã¤ã®ã¯ã©ã¹ã®ãã¨ã¯ç¥ããªãã¦ããã¨ãããç¹å¾´ãªã®ã§ãMainã¯ã©ã¹ãæ示ãã¦ãã¾ãã
@startuml
interface Print {
+ {abstract} void printWeak()
+ {abstract} void printStrong()
}
Main --> Print : Uses >
class Banner {
- String string
+ void showWithParen()
+ void showWithAster()
}
class PrintBanner {
+ void printWeak()
+ void printStrong()
}
Print <|. PrintBanner : implements <
PrintBanner -|> Banner : extends >
@enduml
Mainã¯ã©ã¹ï¼ä½¿ãå´ã®ã¯ã©ã¹ï¼
ã¾ãã¯ãä»åã®å®è£
ã®ã¤ã¡ã¼ã¸ã沸ãããã«ãå
ã«Mainã¯ã©ã¹ãè¦ã¦ããã¾ããã¾ããå®è¡çµæãåããã¦ç¤ºãã¾ãã
PrintBannerã¯ã©ã¹ã®ã¤ã³ã¹ã¿ã³ã¹ãPrintã¯ã©ã¹ã®å¤æ°ã«æ ¼ç´ãã¦ãprintWeak()
㨠printStrong()
ãã³ã¼ã«ãã¦ãã¾ãã
printWeak()
ã¯ã"Hello"ã¨ããæååãæ¬å¼§ã§è£
飾ããã¡ã½ããã§ãprintStrong()
ã¯ã強調ã®ã¢ã¹ã¿ãªã¹ã¯ã§è£
飾ããã¡ã½ããã§ãã
public class Main {
public static void main(String[] args) {
Print p = new PrintBanner("Hello");
p.printWeak();
p.printStrong();
}
}
å®è¡çµæ
ããå°ãä¸èº«ãè¦ã¦ããã¾ãã
Printã¤ã³ã¿ãã§ã¼ã¹
Printã¤ã³ã¿ãã§ã¼ã¹ã¯ã2ã¤ã®æ½è±¡ã¡ã½ãããå®ç¾©ãã¦ãã¾ãã
public interface Print {
public abstract void printWeak();
public abstract void printStrong();
}
Bannerã¯ã©ã¹
Bannerã¯ã©ã¹ã¯ãéå»ã«æ¢ã«å®è£
ãããã¨ä»®å®ããã¯ã©ã¹ã§ãã
ãã©ã¤ãã¼ãã¡ã³ãå¤æ°ã¨ãã¦ãstring
ãæã¡ã¾ãã
showWithParen()
ã¯ãæ¬å¼§ã§è£
飾ããã¡ã½ããã§ãã
showWithAster()
ã¯ãã¢ã¹ã¿ãªã¹ã¯ã§å¼·èª¿ãè£
飾ããã¡ã½ããã§ãã
æ°ããè¦æ±ãããPrintã¤ã³ã¿ãã§ã¼ã¹ã«å¯¾ãã¦ãBannerã¯ã©ã¹ã¯ãæ©è½ã¨ãã¦ã¯ååãªã¡ã½ãããæã£ã¦ãã¾ãããã¤ã³ã¿ã¼ãã§ã¼ã¹ã¯åãã¾ãããããã§ã次ã«èª¬æããAdapterãã¿ã¼ã³ãå®è£
ããPrintBannerã¯ã©ã¹ãå¿
è¦ã«ãªãã¾ãã
public class Banner {
private String string;
public Banner(String string) {
this.string = string;
}
public void showWithParen() {
System.out.println("(" + string + ")");
}
public void showWithAster() {
System.out.println("*" + string + "*");
}
}
PrintBannerã¯ã©ã¹
Adapterãã¿ã¼ã³ãå®ç¾ãã PrintBannerã¯ã©ã¹ã¯ãPrintã¤ã³ã¿ãã§ã¼ã¹ãå®è£
ããã¯ã©ã¹ã§ããããã¤ãBannerã¯ã©ã¹ãç¶æ¿ããã¯ã©ã¹ã§ãã
Printã¤ã³ã¿ãã§ã¼ã¹ã®2ã¤ã®æ½è±¡ã¡ã½ãããå®è£
ãã¦ãã¾ãã
printWeak()
ã¯ãBannerã¯ã©ã¹ã® showWithParen()
ã®æ©è½ãå¼ã³åºãã¦ãã¾ãã
printStrong()
ã¯ãBannerã¯ã©ã¹ã® showWithAster()
ã®æ©è½ãå¼ã³åºãã¦ãã¾ãã
Printã¤ã³ã¿ãã§ã¼ã¹ã«åããã¦ãæ¢åã®Bannerã¯ã©ã¹ããã¾ãé©åããã¦ããã¨ããããAdapterãã¿ã¼ã³ã®ç¹å¾´ã§ãã
public class PrintBanner extends Banner implements Print {
public PrintBanner(String string) {
super(string);
}
@Override
public void printWeak() {
showWithParen();
}
@Override
public void printStrong() {
showWithAster();
}
}
Adapterãã¿ã¼ã³ã®ã¡ãªããã¨èå¯
Adapterãã¿ã¼ã³ã¯ãéå»ã«å®è£
ã¨ãã¹ããè¡ããå®ç¸¾ã®ããã¯ã©ã¹ããæ°ããè¦æ±ãããã¤ã³ã¿ã¼ãã§ã¼ã¹ã«é©åãããæ¹æ³ãæä¾ãã¦ããã¾ãã
æ¢åã®ã¯ã©ã¹ã使ãã¾ãããã¨ã¯ãããããã¨æãã¾ãããã®ã¨ããæ¢åã®ã¯ã©ã¹ãå¤æ´ãã¦å¯¾å¿ãã¦ãã¾ãã¨ãæ¢åã®ã¯ã©ã¹ã®ä¿¡é ¼æ§ã¯ãªããªãããã¹ãã®ããç´ããçºçãã¦ãã¾ãã¾ãããã£ããæ¢åã®ã¯ã©ã¹ã使ãã¾ããã¦ããã®ã«ããã¹ãããæéãå¤ãå¿
è¦ã¨ããã®ã§ã¯ããã£ãããªãã§ããã
ããããå ´åã«ãAdapterãã¿ã¼ã³ãé©ç¨ããã¨ãæ¢åã®ã¯ã©ã¹ãå
¨ãå¤æ´ããã«å¯¾å¿ãããã¨ãã§ãã¾ãã
é©åãããã¯ã©ã¹ï¼ä»åã®PrintBannerã¯ã©ã¹ï¼ã®ãã¹ãã¯å¿
è¦ã«ãªãã¾ãããã¤ã³ã¿ãã§ã¼ã¹ãé©åãããã ãã®ã¯ã©ã¹ãªã®ã§ãæ¢åã®ã¯ã©ã¹ã«æ¯ã¹ãã¨ãè¦æ¨¡ã®å°ããã¯ã©ã¹ã«ãªãããã¹ãã®æéãå°ãªãã¦æ¸ãã§ãããã
使ãã¾ãããããªã¯ã©ã¹ãè¦ã¤ããã¨ãã«ãããã«å¤æ´ãå§ããã®ã§ã¯ãªãããã£ããèãã¦å¯¾å¿ãããã¨ãéè¦ã¨ãããã¨ã§ãããããããã±ã¼ã¹ã§ãè½ã¡çãã¦ãAdapterãã¿ã¼ã³ãæãåºããã¨ãé£ããã§ããï¼ç¬ï¼ã
ä»åã¯ä»¥ä¸ã§ãï¼
ãããã«
ä»åã¯Adapterãã¿ã¼ã³ãå¦ã³ã¾ããã
æå¾ã¾ã§ãèªã¿ããã ãããããã¨ããããã¾ããã