Javaã«ãããã¶ã¤ã³ãã¿ã¼ã³ã®9åç®ã§ãï¼
ä»åãææã¯ãåèæç®ã®ãJavaè¨èªã§å¦ã¶ãã¶ã¤ã³ãã¿ã¼ã³å
¥é第3çãã®ãµã³ãã«ã³ã¼ãã使ããã¦é ãã¾ãã
ä»åã¯ãPrototypeãã¿ã¼ã³ã§ãã
ã§ã¯ããã£ã¦ããã¾ãããï¼
åèæç®
åèæç®ã®ãµã³ãã«ããã°ã©ã ã®ãã¦ã³ãã¼ã
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ï¼ãã¼ã¿æ§é ãä¿æããã¯ã©ã¹ã¨ãããã«å¯¾ãã¦å¦çãè¡ãã¯ã©ã¹ãåé¢ãã
ã¨ã³ã¸ãã¢ã°ã«ã¼ãã®ã©ã³ãã³ã°ã«åå ä¸ã§ãã
æ°æ¥½ã«ãããã¨ãããããé¡ããããã¾ãð
ä»åã¯ããPrototypeãã¿ã¼ã³ãããã£ã¦ããã¾ãï¼
ãµã³ãã«ã³ã¼ãã®ç解
Prototypeãã¿ã¼ã³ã¯ãåæ§ã®ã¤ã³ã¹ã¿ã³ã¹ãçæããããã«ãååã®ã¤ã³ã¹ã¿ã³ã¹ãè¤è£½ãããã¨ããã¾ããä¸åº¦ä½æãããã®ããããã¿ã¤ãã¨ãã¦ãããä¸åº¦åããã®ãä½ããããããããã¿ã¤ããã³ãã¼ããæ¹ãç°¡åã ã¨ãããã¨ã表ç¾ãããã¿ã¼ã³ã§ãã
ããã§ã¯ãæ©éãµã³ãã«ã³ã¼ããè¦ã¦ãããã¨ã«ãã¾ãã
ä»åã®PlantUMLã®ã¯ã©ã¹å³
PlantUMLã§ä½æããã¯ã©ã¹å³ã®ã³ã¼ãã¨ç»åãã¡ã¤ã«ã§ãã
Prototypeãã¿ã¼ã³ãå®ç¾ãã¦ããã®ã¯ãProductã¤ã³ã¿ãã§ã¼ã¹ã¨ããããå®è£
ããUnderlinePenã¯ã©ã¹ã¨MessageBoxã¯ã©ã¹ã§ãã
Productã¤ã³ã¿ãã§ã¼ã¹ã® createCopy()
ã¯ãèªåèªèº«ï¼thisï¼ãã³ãã¼ã³ã³ã¹ãã©ã¯ã¿ã§ã³ãã¼ãã¦è¿ãã¡ã½ããã§ãããã«ãã£ã¦ä½æãããããã¿ã¤ããã³ãã¼ããPrototypeãã¿ã¼ã³ãå®ç¾ãã¦ãã¾ãã
Managerã¯ã©ã¹ã¯ãProductã¤ã³ã¿ãã§ã¼ã¹ã®ã¤ã³ã¹ã¿ã³ã¹ãç»é²ãåç
§ããããã®ç®¡çã¯ã©ã¹ã§ãã
@startuml
package framework {
interface Product {
+ {abstract} void use()
+ {abstract} Product createCopy()
}
class Manager {
- Map<String, Product> showcase
+ void register()
+ Product create()
}
Manager -> Product : Uses >
}
class UnderlinePen {
- char ulchar
+ UnderlinePen(char ulchar)
+ UnderlinePen(UnderlinePen prototype)
+ void use()
+ Product createCopy()
}
Product <|.. UnderlinePen
class MessageBox {
- char decochar
+ MessageBox(char decochar)
+ MessageBox(MessageBox prototype)
+ void use()
+ Product createCopy()
}
Product <|.. MessageBox
@enduml
Mainã¯ã©ã¹ï¼ä½¿ãå´ã®ã¯ã©ã¹ï¼
ä»åã®å®è£
ã®ã¤ã¡ã¼ã¸ã沸ãããã«ãå
ã«Mainã¯ã©ã¹ãè¦ã¦ããã¾ããã¾ããå®è¡çµæãåããã¦ç¤ºãã¾ãã
ã¾ããæºåã¨ãã¦ãManagerã¯ã©ã¹ï¼ããã¼ã¸ã£ï¼ã®ã¤ã³ã¹ã¿ã³ã¹ãçæãã¦ãã¾ããããã«ã-
ã使ã£ãUnderlinePenã¯ã©ã¹ï¼ä¸ç·ï¼ã*
ã使ã£ãMessageboxã¯ã©ã¹ï¼å²ãæ ï¼ã/
ã使ã£ãMessageBoxã¯ã©ã¹ï¼å²ãæ ï¼ã®ã¤ã³ã¹ã¿ã³ã¹ãçæãã¦ãã¾ãã
ããã«ç»é²ã¨ãã¦ãããã¼ã¸ã£ã«ãä½æããä¸ç·ãã¢ã¹ã¿ãªã¹ã¯ã®å²ãæ ãã¹ã©ãã·ã¥ã®å²ãæ ããããããååãä»ãã¦ãããã¿ã¤ãã¨ãã¦ç»é²ãã¦ãã¾ãã
çæã¨ä½¿ç¨ã¨ãã¦ãããã¼ã¸ã£ã«ç»é²ããä¸ç·ãå¼ã³åºãã¦ï¼ä¸ç·ã®ãããã¿ã¤ããã³ãã¼ãã¦ï¼ã"Hello, world." ãä¸ç·ã§è£
飾ãã¦åºåãã¾ããåæ§ã«ãããã¼ã¸ã£ã«ç»é²ããã¢ã¹ã¿ãªã¹ã¯ã®å²ãæ ãã³ãã¼ãã¦ã"Hello, world." ãè£
飾ãã¦åºåããã¹ã©ãã·ã¥ã®å²ãæ ãã³ãã¼ãã¦ã"Hello, world." ãè£
飾ãã¦åºåãã¦ãã¾ãã
import framework.Manager;
import framework.Product;
public class Main {
public static void main(String[] args) {
Manager manager = new Manager();
UnderlinePen upen = new UnderlinePen('-');
MessageBox mbox = new MessageBox('*');
MessageBox sbox = new MessageBox('/');
manager.register("strong message", upen);
manager.register("warning box", mbox);
manager.register("slash box", sbox);
Product p1 = manager.create("strong message");
p1.use("Hello, world.");
Product p2 = manager.create("warning box");
p2.use("Hello, world.");
Product p3 = manager.create("slash box");
p3.use("Hello, world.");
}
}
å®è¡çµæ
Hello, world.
ã¨ããæååã«å¯¾ãã¦ã-
ã®ä¸ç·ã¨ã*
ã®å²ãæ ã/
ã®å²ãæ ã§åºåããã¦ãã¾ããã
ããå°ãä¸èº«ãè¦ã¦ããã¾ãã
Productã¤ã³ã¿ãã§ã¼ã¹
Productã¤ã³ã¿ãã§ã¼ã¹ã¯ãframeworkããã±ã¼ã¸ã§ã2ã¤ã®æ½è±¡ã¡ã½ãããå®ç¾©ãã¦ãã¾ãã
package framework;
public interface Product {
public abstract void use(String s);
public abstract Product createCopy();
}
UnderlinePenã¯ã©ã¹
UnderlinePenã¯ã©ã¹ã¯ãProductã¤ã³ã¿ãã§ã¼ã¹ãå®è£
ããä¸ç·ãå¼ãè£
飾ã®ã¯ã©ã¹ã§ãã
ãã©ã¤ãã¼ãã¡ã³ãå¤æ°ã¨ãã¦ãulchar
ãæã¡ã¾ããMainã¯ã©ã¹ã§ã¯ã-
ãç»é²ãã¦ãã¾ããã-
ã使ã£ãä¸ç·ã®è£
飾ã¨ãããã¨ã«ãªãã¾ããä»ã®æåã使ã£ãä¸ç·ãå¼ãè£
飾ã®ã¤ã³ã¹ã¿ã³ã¹ãä½ããã¨ãã§ãã¾ãã
ã³ã³ã¹ãã©ã¯ã¿ã2ã¤ããã¾ãï¼å¤éå®ç¾©ããªã¼ãã¼ãã¼ãã¨å¼ã°ãã¾ãï¼ã1ã¤ç®ã¯æ®éã®ã³ã³ã¹ãã©ã¯ã¿ã§ãå¼æ°ã§ä¸ããããcharåã®æåã§ãã©ã¤ãã¼ãã¡ã³ãå¤æ°ãåæåãã¾ãã2ã¤ç®ã¯ã³ãã¼ã³ã³ã¹ãã©ã¯ã¿ã¨å¼ã°ããã³ã³ã¹ãã©ã¯ã¿ã§ãèªåèªèº«ã¨åãã¯ã©ã¹åã®å¼æ°ãåãåã£ã¦ãå
¨ã¡ã³ãå¤æ°ãã³ãã¼ããã³ã³ã¹ãã©ã¯ã¿ã§ããããã«ãã£ã¦ãPrototypeãã¿ã¼ã³ãå®ç¾ãã¦ãã¾ãã
use()
ã¯ãProductã¤ã³ã¿ãã§ã¼ã¹ã§å®ç¾©ããã¦ããã¡ã½ããã§ãå¼æ°ã§æ¸¡ãããæååã対象ã¨ãã¦ãä¸ç·ã§è£
飾ããã¡ã½ããã§ãã
createCopy()
ããProductã¤ã³ã¿ãã§ã¼ã¹ã§å®ç¾©ããã¦ããã¡ã½ããã§ãã³ãã¼ã³ã³ã¹ãã©ã¯ã¿ãå¼ã³åºãã¦ãèªåèªèº«ã®ã¤ã³ã¹ã¿ã³ã¹ãçæãã¦è¿ãã¦ãã¾ãã
import framework.Product;
public class UnderlinePen implements Product {
private char ulchar;
public UnderlinePen(char ulchar) {
this.ulchar = ulchar;
}
public UnderlinePen(UnderlinePen prototype) {
this.ulchar = prototype.ulchar;
}
@Override
public void use(String s) {
int ulen = s.length();
System.out.println(s);
for (int i = 0; i < ulen; i++) {
System.out.print(ulchar);
}
System.out.println();
}
@Override
public Product createCopy() {
return new UnderlinePen(this);
}
}
MessageBoxã¯ã©ã¹
MessageBoxã¯ã©ã¹ã¯ãUnderlinePenã¯ã©ã¹ã¨åæ§ã«ãProductã¤ã³ã¿ãã§ã¼ã¹ãå®è£
ããå²ãæ ã§è£
飾ããã¯ã©ã¹ã§ãã
ãã©ã¤ãã¼ãã¡ã³ãå¤æ°ã¨ãã¦ãdecochar
ãæã¡ã¾ããMainã¯ã©ã¹ã§ã¯ã*
ã¨ã/
ãç»é²ãã¦ãã¾ããã*
ã¨ã/
ã使ã£ãå²ãæ ã®è£
飾ã¨ãããã¨ã«ãªãã¾ãã
UnderlinePenã¯ã©ã¹ã¨åæ§ã«ãã³ã³ã¹ãã©ã¯ã¿ã2ã¤ããã¾ããå
容ãUnderlinePenã¯ã©ã¹ã¨åããªã®ã§èª¬æã¯çç¥ãã¾ãã
use()
㨠createCopy()
ããUnderlinePenã¯ã©ã¹ã¨ã»ã¨ãã©åãã§ããuse()
ã¯ãUnderlinePenã¯ã©ã¹ã§ã¯ä¸ç·ã§ããããMessageBoxã¯ã©ã¹ã§ã¯å²ãæ ãªã®ã§ãå°ãå®è£
ãå¢ãã¦ãã¾ããã
import framework.Product;
public class MessageBox implements Product {
private char decochar;
public MessageBox(char decochar) {
this.decochar = decochar;
}
public MessageBox(MessageBox prototype) {
this.decochar = prototype.decochar;
}
@Override
public void use(String s) {
int decolen = 1 + s.length() + 1;
for (int i = 0; i < decolen; i++) {
System.out.print(decochar);
}
System.out.println();
System.out.println(decochar + s + decochar);
for (int i = 0; i < decolen; i++) {
System.out.print(decochar);
}
System.out.println();
}
@Override
public Product createCopy() {
return new MessageBox(this);
}
}
Managerã¯ã©ã¹
Managerã¯ã©ã¹ã¯ãProductã¤ã³ã¿ãã§ã¼ã¹ãå®è£
ããã¯ã©ã¹ã®ç»é²ãè¤è£½ãè¡ãã¯ã©ã¹ã§ãã
ãã©ã¤ãã¼ãã¡ã³ãå¤æ°ã¨ãã¦ãHashMapã¯ã©ã¹ã® showcase
ãæã¡ã¾ããHashMapã¯ã©ã¹ã¯ããã¼ã¨å¤ãã»ããã«ãããã¼ã¿æ§é ã§ããä»åã¯ãååããã¼ã¨ãã¦ãProductã¤ã³ã¿ãã§ã¼ã¹ãå®è£
ããã¯ã©ã¹ã®ã¤ã³ã¹ã¿ã³ã¹ãå¤ã¨ãã¦æ ¼ç´ãã¾ãã
register()
ã¯ãå¼æ°ã§æ¸¡ãããååã¨Productã¤ã³ã¿ãã§ã¼ã¹ãå®è£
ããã¯ã©ã¹ã®ã¤ã³ã¹ã¿ã³ã¹ããshowcase
ã«ç»é²ãã¾ãã
create()
ã¯ãå¼æ°ã§æ¸¡ãããååã使ã£ã¦ãshowcase
ããProductã¤ã³ã¿ãã§ã¼ã¹ãå®è£
ããã¯ã©ã¹ã®ã¤ã³ã¹ã¿ã³ã¹ãåãåºããProductã¯ã©ã¹ã® createCopy()
ãå¼ã³åºãã¦ãã¤ã³ã¹ã¿ã³ã¹ãã³ãã¼ãã¦è¿ãã¾ãã
package framework;
import java.util.HashMap;
import java.util.Map;
public class Manager {
private Map<String,Product> showcase = new HashMap<>();
public void register(String name, Product prototype) {
showcase.put(name, prototype);
}
public Product create(String prototypeName) {
Product p = showcase.get(prototypeName);
return p.createCopy();
}
}
Prototypeãã¿ã¼ã³ã®ã¡ãªããã¨èå¯
Prototypeãã¿ã¼ã³ã¯ãã¤ã³ã¹ã¿ã³ã¹ãã³ãã¼ããã¨ãããç¹å¾´çã§ãã
ãã¤ã³ããªã©ã®ã½ããã§ãã¦ã¼ã¶ãçµµãæããã¨ãã¾ããé·ãæéãããã¦æããçµµãããä¸ã¤å¿
è¦ã«ãªã£ãã¨ããã¦ã¼ã¶ã®æä½ãããä¸åº¦è¡ãããããçµµãã³ãã¼ãã¾ãããï¼å½ããåã§ããï¼ããã®ã³ãã¼ã®é¨åãPrototypeãã¿ã¼ã³ã«ãããã¾ãã
ãã¤ã³ãã ã¨ã³ãã¼ããã®ãå½ããåã§ããããã½ããã¦ã§ã¢ã®å¦çã®å ´åã§ã¯ãæå¤ã¨ã³ãã¼ã¯ããããããä¸åº¦åãå¦çãå®è¡ããã¦ããå ´åãçµæ§ããã¾ãããã¤ã³ãã§çµµãæãã®ã¯å¤§å¤ã§ãããã½ããã¦ã¨ã¢ã®å ´åã¯ããã®ã¡ã½ããããã1åå¼ã¶ã ãã§å®ç¾ã§ããããã§ãã
ã¡ã½ãããããä¸åº¦å¼ã¶æ¹ãããã®ã¡ã½ããã®çµæãè¤è£½ããããç°¡åãªå ´åãå¤ãããã§ããããããçµæãè¤è£½ããæ¹ãã½ããã®å¦çæéã¯å§åçã«æ©ãã§ããå ´åãå¤ãã§ãã
å°ãå人çãªè¦è§£ãå
¥ã£ã¦ãã¾ã£ã¦ãã¾ãããPrototypeãã¿ã¼ã³ã¯ãè¤è£½ããã¨ããé¸æè¢ãæ³èµ·ããã¦ããããã¿ã¼ã³ã ã¨æãã¾ãã
ä»åã¯ä»¥ä¸ã§ãï¼
ãããã«
ä»åã¯Prototypeãã¿ã¼ã³ãå¦ã³ã¾ããã
æå¾ã¾ã§ãèªã¿ããã ãããããã¨ããããã¾ããã