Javaã«ãããã¶ã¤ã³ãã¿ã¼ã³ã®3åç®ã§ãï¼
ææã¯ãåèæç®ã®ãJavaè¨èªã§å¦ã¶ãã¶ã¤ã³ãã¿ã¼ã³å
¥é第3çãã使ããã¦é ãã¾ãã
ä»åã¯ãObserverãã¿ã¼ã³ã§ããããã§ã¯ãæ©éãã£ã¦ããã¾ãããï¼
åèæç®
åèæç®ã®ãµã³ãã«ããã°ã©ã ã®ãã¦ã³ãã¼ã
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ï¼ãã¼ã¿æ§é ãä¿æããã¯ã©ã¹ã¨ãããã«å¯¾ãã¦å¦çãè¡ãã¯ã©ã¹ãåé¢ãã
ä»åã¯ããObserverãã¿ã¼ã³ãããã£ã¦ããã¾ãï¼
ãµã³ãã«ã³ã¼ãã®ç解
Observerãã¿ã¼ã³ã¯ãObserverï¼è¦³å¯è
ï¼ã¤ã³ã¿ãã§ã¼ã¹ãæã£ã観å¯è
ãã¡ãã対象ã®ãªãã¸ã§ã¯ãã®ç¶æ
ãå¤åããã¨ãã«ãéç¥ãå¾ãããä»çµã¿ã§ãã
ããã ãèãã¦ããä½ãããã®ãåãããªãã§ããããªã®ã§ãæ©éãµã³ãã«ã³ã¼ããè¦ã¦ãããã¨ã«ãã¾ãã
ä»åã®PlantUMLã®ã¯ã©ã¹å³
PlantUMLã§ä½æããã¯ã©ã¹å³ã®ã³ã¼ãã¨ç»åãã¡ã¤ã«ã§ãã
@startuml
interface Observer {
+ {abstract} void update()
}
class DigitObserver {
+ void update()
}
Observer <|.. DigitObserver
class GraphObserver {
+ void update()
}
Observer <|.. GraphObserver
abstract NumberGenerator {
- List<Observer> observers
+ void addObserver()
+ void deleteObserver()
+ void notifyObservers()
+ {abstract} int getNumber()
+ {abstract} void execute()
}
class RandomNumberGenerator {
- Random random
- int number
+ int getNumber()
+ void execute()
}
NumberGenerator <|-- RandomNumberGenerator
@enduml
Mainã¯ã©ã¹ï¼ä½¿ãå´ã®ã¯ã©ã¹ï¼
ã¾ãã¯ãObserverãã¿ã¼ã³ããã©ã使ãããã®ããè¦ãããã«ãMainã¯ã©ã¹ãè¦ã¦ããã¾ãã
åããããããªãã¨æãã®ã§ãå
ã«å®è¡çµæã示ãã¾ãã
public class Main {
public static void main(String[] args) {
NumberGenerator generator = new RandomNumberGenerator();
Observer observer1 = new DigitObserver();
Observer observer2 = new GraphObserver();
generator.addObserver(observer1);
generator.addObserver(observer2);
generator.execute();
}
}
å®è¡çµæ
Observer
ï¼è¦³å¯è
ï¼ã¯ãã¤ã³ã¿ãã§ã¼ã¹ã§ãDigitObserver
ã¯ã©ã¹ã¨ãGraphObserver
ã¯ã©ã¹ã®2人ãããã®ã¤ã³ã¿ãã§ã¼ã¹ãå®è£
ãã¦ãã¾ãã
ä¸æ¹ã観å¯ãããå´ã¯ãæ½è±¡ã¯ã©ã¹ã NumberGenerator
ã¯ã©ã¹ã§ããµãã¯ã©ã¹ã RandomNumberGenerator
ã¯ã©ã¹ã§ãã
å®è¡çµæãè¦ãã¨ãä¹±æ°çæå¨ããä¹±æ°ãçæããã¨ãã«ã2人ã®è¦³å¯è
ã«éç¥ãéã£ã¦ããããã§ãã2人ã®è¦³å¯è
ã¯ãéç¥ãåãåãã¨ãããããã®è¡¨ç¾æ¹æ³ã§ãä¹±æ°ã®å¤ã表示ãã¦ããããã§ãã
ã§ã¯ãããããã®ã½ã¼ã¹ã³ã¼ããè¦ã¦ããã¾ãã
Observerã¤ã³ã¿ãã§ã¼ã¹
public interface Observer {
public abstract void update(NumberGenerator generator);
}
Observer
ã¯ã©ã¹ãç¶æ¿ãããµãã¯ã©ã¹ï¼è¦³å¯è
ï¼ã¯ã観å¯å¯¾è±¡ã® NumberGenerator
ã¯ã©ã¹ãç¶æ¿ãã RandomNumberGenerator
ã¯ã©ã¹ã«èªåèªèº«ãç»é²ãã¾ãã
RandomNumberGenerator
ã¯ã©ã¹ã¯ä¹±æ°ãçæããã¨ã観å¯è
å
¨å¡ã® update
ã¡ã½ããã使ã£ã¦éç¥ãã¾ãã
DigitObserverã¯ã©ã¹
次ã¯ãDigitObserverã¯ã©ã¹ã§ãã
public class DigitObserver implements Observer {
@Override
public void update(NumberGenerator generator) {
System.out.println("DigitObserver:" + generator.getNumber());
try {
Thread.sleep(100);
} catch (InterruptedException e) {
}
}
}
DigitObserverã¯ã©ã¹ã® update()
ã§ã¯ãéç¥ããã£ãéã«ãåç´ã«ä¹±æ°ã®å¤ã表示ãã¦ãã¾ãã
GraphObserverã¯ã©ã¹
次ã¯ãGraphObserverã¯ã©ã¹ã§ãã
public class GraphObserver implements Observer {
@Override
public void update(NumberGenerator generator) {
System.out.print("GraphObserver:");
int count = generator.getNumber();
for (int i = 0; i < count; i++) {
System.out.print("*");
}
System.out.println("");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
}
}
}
GraphObserverã¯ã©ã¹ã® update()
ã§ã¯ãéç¥ããã£ãéã«ãä¹±æ°ã®å¤ã®æ°ã ãã*
ãåºåãã¦ãã¾ãã
NumberGeneratorã¯ã©ã¹
import java.util.ArrayList;
import java.util.List;
public abstract class NumberGenerator {
private List<Observer> observers = new ArrayList<>();
public void addObserver(Observer observer) {
observers.add(observer);
}
public void deleteObserver(Observer observer) {
observers.remove(observer);
}
public void notifyObservers() {
for (Observer o: observers) {
o.update(this);
}
}
public abstract int getNumber();
public abstract void execute();
}
NumberGeneratorã¯ã©ã¹ã¯ããã©ã¤ãã¼ãã¡ã³ãå¤æ°ã¨ãã¦ã観å¯è
ãç»é²ã§ãã observers
ï¼ãªã¹ãï¼ãæã£ã¦ãã¾ãã
addObserver()
ã¯ãå¼æ°ã§æ¸¡ããã観å¯è
ããªã¹ãã«è¿½å ãã¾ããdeleteObserver()
ã¯ãå¼æ°ã§æ¸¡ããã観å¯è
ãåé¤ãã¾ããnotifyObservers()
ã¯ããªã¹ãã®è¦³å¯è
å
¨å¡ã«ãèªåèªèº«ï¼ã¤ã³ã¹ã¿ã³ã¹ï¼ãå¼æ°ã¨ãã¦ãéç¥ï¼è¦³å¯è
ã« update()
ãã³ã¼ã«ï¼ãè¡ãã¾ãã
æ½è±¡ã¡ã½ããã¨ãã¦ãgetNumber()
ã¨ãexecute()
ãããã¾ãã
RandomNumberGeneratorã¯ã©ã¹
import java.util.Random;
public class RandomNumberGenerator extends NumberGenerator {
private Random random = new Random();
private int number;
@Override
public int getNumber() {
return number;
}
@Override
public void execute() {
for (int i = 0; i < 20; i++) {
number = random.nextInt(50);
notifyObservers();
}
}
}
NumberGeneratorã¯ã©ã¹ãç¶æ¿ããããµãã¯ã©ã¹ã®RandomNumberGeneratorã¯ã©ã¹ã¯ããã©ã¤ãã¼ãã¡ã³ãå¤æ°ã¨ãã¦ãrandom
ï¼ä¹±æ°çæå¨ï¼ã¨ãnumber
ï¼ç¾å¨ã®æ°ï¼ãæã¡ã¾ãã
getNumber()
ã¯ãç¾å¨ã®æ°ãè¿ãã¾ãã
execute()
ã¯ãä¹±æ°çæãã¦ããã®å¤ã number
ã«è¨å®ããå
¨è¦³å¯è
ã«éç¥ãè¡ãã¾ããrandom.nextInt(50)
ã¯ã0ãã49ã¾ã§ã®ã©ã³ãã ãªæ´æ°ãçæãã¦ããã¾ãã
ãããã«
ä»åã¯Observerãã¿ã¼ã³ãå¦ã³ã¾ããã
観å¯ããã¯ã©ã¹ã«å¤åããã£ãå ´åã«ã観å¯è
ãéç¥ãåãåããä»çµã¿ã§ããã観å¯è
ãå¢ããããæ¸ã£ãããããã¨ãç°¡åã«å®ç¾ã§ãã¾ãã
ã¾ããObserver
ã¯ãã¤ã³ã¿ãã§ã¼ã¹ã¨ãã¦å®ç¾©ãã¦ããã®ã§ãå¥ã®è¦³å¯è
ï¼å¥ã®ã¯ã©ã¹ï¼ãå¿
è¦ã«ãªã£ãå ´åã§ãã容æã«å¯¾å¿ã§ãã¾ãã
ä»åã¯ä»¥ä¸ã§ãï¼
æå¾ã¾ã§ãèªã¿ããã ãããããã¨ããããã¾ããã