èæ¯
éå»æ°å¹Žã«ããã£ãŠãç§ã¯å€æ°ã®ã€ã³ã¿ãã¥ãŒã«åå ããŠããŸããã ãããã®ããããã§ãç§ã¯å¿åè ã«å¯äžã®è²¬ä»»ã®ååïŒä»¥äžSRPïŒã«ã€ããŠå°ããŸããã ãããŠãã»ãšãã©ã®äººã¯ååã«ã€ããŠäœãç¥ããŸããã ãããŠãå®çŸ©ãèªãããšãã§ãã人ã§ããã圌ããä»äºã§ãã®ååãã©ã®ããã«äœ¿ã£ãŠããããèšãããšã¯ã»ãšãã©ã§ããŸããã§ããã 圌ãã¯ãSRPãèªåãæžããã³ãŒããååã®ã³ãŒãã¬ãã¥ãŒã«ã©ã®ããã«åœ±é¿ããããèšãããšãã§ããŸããã§ããã ãŸããSOLIDå šäœãšåæ§ã«ãSRPã¯ãªããžã§ã¯ãæåããã°ã©ãã³ã°ã«ã®ã¿é¢é£ããŠãããšãã誀解ããããŸããã ãŸããããç¥ãããŠãããã¬ãŒã ã¯ãŒã¯ã§æšå¥šãããŠããã¹ã¿ã€ã«ã§ã³ãŒããèšè¿°ããããšããçç±ã ãã§ãå€ãã®å Žåã人ã ã¯ãã®ååã«éåããæãããªã±ãŒã¹ãç¹å®ã§ããŸããã§ããã
Reduxã¯ãã¬ã€ãã©ã€ã³ãSRPã«éåããŠãããã¬ãŒã ã¯ãŒã¯ã®ä»£è¡šçãªäŸã§ãã
SRPã®åé¡
ç§ã¯ããã®ååã®äŸ¡å€ããããããããå©ç¹ããå§ããããšæããŸãã ãŸãããã®ååã¯OOPã ãã§ãªããæç¶ãåããã°ã©ãã³ã°ãé¢æ°åãããã«å®£èšåã«ãé©çšãããããšã«æ³šæããŠãã ããã åŸè ã®ä»£è¡šãšããŠã®HTMLã¯ãç¹ã«ReactãAngularãªã©ã®UIãã¬ãŒã ã¯ãŒã¯ã«ãã£ãŠå¶åŸ¡ãããŠããå Žåã¯ç¹ã«ãå解ããããšãã§ããŸãã ããã«ããã®ååã¯ä»ã®ãšã³ãžãã¢ãªã³ã°åéã«ãé©çšãããŸãã ãããŠãå·¥åŠã ãã§ãªããè»äºåéã§ããã®ãããªè¡šçŸããããŸããããåå²ããŠåŸæãããããšã§ããããã¯ãæŠããŠåãååã®å ·äœåã§ãã è€éãã¯æ®ºãããããéšåã«åå²ããããªãã¯åã¡ãŸãã
ä»ã®ãšã³ãžãã¢ãªã³ã°åéã«é¢ããŠã¯ãããããã§ãéçºãããèªç©ºæ©ãã©ã®ããã«ãšã³ãžã³ãæ éããããã€ãããã®ã³ãã³ãã§ãªããŒã¹ã«åãæ¿ããªãã£ããã«ã€ããŠã®èå³æ·±ãèšäºããããŸããã åé¡ã¯ãã·ã£ãŒã·ã®ç¶æ ã誀ã£ãŠè§£éããŠããããšã§ãã ãšã³ãžã³ã³ã³ãããŒã©ãŒã¯ãã·ã£ãŒã·ãå¶åŸ¡ããã·ã¹ãã ã«äŸåãã代ããã«ãã·ã£ãŒã·ã«ããã»ã³ãµãŒããªãããã¹ã€ãããªã©ãçŽæ¥èªã¿åããŸãã ãŸãããšã³ãžã³ããããã¿ã€ãã®èªç©ºæ©ã«æèŒãããåã«ããšã³ãžã³ãé·ãèªèšŒãåããå¿ èŠãããããšãèšäºã§èšåãããŸããã ãŸãããã®å Žåã®SRPéåã¯ãã·ã£ãŒã·ã®èšèšãå€æŽãããšãã«ããšã³ãžã³ã³ã³ãããŒã©ãŒã®ã³ãŒããå€æŽããŠåèªèšŒããå¿ èŠããããšããäºå®ã«æããã«ã€ãªãããŸããã ããã«æªãããšã«ããã®ååã«éåããããšã¯é£è¡æ©ãšãã€ãããã®äººçã«ã»ãšãã©äŸ¡å€ããããŸããã 幞ããªããšã«ãç§ãã¡ã®æ¥åžžã®ããã°ã©ãã³ã°ã¯ãã®ãããªçµæãè ãããã®ã§ã¯ãããŸããããè¯ãã³ãŒããæžããšããååãç¡èŠããã¹ãã§ã¯ãããŸããã çç±ã¯æ¬¡ã®ãšããã§ãã
- ã³ãŒãã®å解ã«ãããã³ãŒãã®è€éãã軜æžãããŸãã ããšãã°ãåé¡ã®è§£æ±ºçãšããŠåŸªç°çè€é床4ã®ã³ãŒããèšè¿°ããå¿ èŠãããå Žåããã®ãããª2ã€ã®åé¡ãåæã«è§£æ±ºããæ¹æ³ã«ã¯è€é床16ã®ã³ãŒããå¿ èŠã«ãªããŸããä»äºã«å¯Ÿããéé¡ã«ãªããŸããããšã«ããåŸåã¯ã»ãŒåãã«ãªããŸãã
- å解ãããã³ãŒãã®åäœãã¹ãã¯ç°¡çŽ åãããããå¹ççã«ãªããŸãã
- å解ãããã³ãŒãã¯ãå€æŽã«å¯Ÿããæµæãå°ãªããªããŸãã å€æŽãè¡ããšãããã¹ãããå¯èœæ§ã¯äœããªããŸãã
- ã³ãŒãã®æ§é ãæ¹åãããŠããŸãã ãã¡ã€ã«ããã©ã«ããŒã«é 眮ãããã³ãŒãå ã®äœããæ€çŽ¢ããããšã¯ã1ã€ã®å€§ããªãããã¯ãã¹ãããã¯ããã«ç°¡åã§ãã
- å®åã³ãŒããããžãã¹ããžãã¯ããåé¢ãããšãã³ãŒãçæããããžã§ã¯ãã«é©çšã§ããããã«ãªããŸãã
ãããŠããããã®ãã¹ãŠã®å åã¯äžç·ã«ãªãããããã¯åãã³ãŒãã®å åã§ãã ããšãã°ãååã«ãã¹ããããã³ãŒããšååã«æ§é åãããã³ãŒããéžæããå¿ èŠã¯ãããŸããã
æ¢åã®å®çŸ©ã¯æ©èœããŸãã
å®çŸ©ã®1ã€ã¯ããã³ãŒãïŒã¯ã©ã¹ãŸãã¯é¢æ°ïŒãå€æŽããçç±ã¯1ã€ã ãã§ããã¹ãã§ããã§ãã ãã®å®çŸ©ã®åé¡ã¯ãSOLIDã®2çªç®ã®ååã°ã«ãŒãã§ããOpen-Closeååãšç«¶åããããšã§ãã ãã®å®çŸ©ïŒãã³ãŒãã¯æ¡åŒµã®ããã«éãããå€æŽã®ããã«éããããªããã°ãªããŸãããã å€æŽã®çç±ã®1ã€ãšãå€æŽã®å®å šãªçŠæ¢ã ããã§äœãæå³ããã®ãããã詳现ã«æããã«ãããšãååéã«ççŸã¯ãªãããšãããããŸããããã¡ãžãŒå®çŸ©ã®éã«ã¯ééããªãççŸããããŸãã
2çªç®ã®ãããçŽæ¥çãªå®çŸ©ã¯ããã³ãŒãã«ã¯1ã€ã®è²¬ä»»ã®ã¿ãå¿ èŠã§ããã§ãã ãã®å®çŸ©ã®åé¡ã¯ããã¹ãŠãäžè¬åããããšã¯äººéã®æ¬æ§ã§ãããšããããšã§ãã
ããšãã°ãé¶ã飌è²ããŠããèŸ²å Žãããããã®æç¹ã§èŸ²å Žã®è²¬ä»»ã¯1ã€ã ãã§ãã ãã®ãããã¢ãã«ãç¹æ®ãããããšã決å®ãããŸããã æ¬èœçã«ã¯ã2ã€ã®è²¬ä»»ãããããšãèªããã®ã§ã¯ãªããé€é¶å ŽãšåŒã³ãŸãã ããã«çŸãè¿œå ãããšããããããããã¡ãŒã ã«ãªããŸãã 次ã«ãããã§ããããŸãã¯ããã³ãæ œå¹ããããã«äžè¬åããã次ã®ååãèããŸãã åãããšããå€æŽã®ã1ã€ã®çç±ãã«ãåœãŠã¯ãŸããŸãã ãã®çç±ã¯ãæ³ååã§ååã«äžè¬åã§ããŸãã
å¥ã®äŸã¯ãå®å®ã¹ããŒã·ã§ã³ç®¡çè ã¯ã©ã¹ã§ãã 圌ã¯ä»ã«äœãããŸããã圌ã¯å®å®ã¹ããŒã·ã§ã³ã管çããã ãã§ãã ãã®ã¯ã©ã¹ã1ã€ã®è²¬ä»»ã§ã©ã®ããã«æ°ã«å ¥ã£ãŠããŸããïŒ
ãŸããæ±è·è ããã®æè¡ã«ç²ŸéããŠãããšãã«Reduxã«ã€ããŠèšåããã®ã§ã質åãããŸããå žåçãªSRPãªãã¥ãŒãµãŒã¯éåããŸããïŒ
ãªãã¥ãŒãµãŒã«ã¯switchã¹ããŒãã¡ã³ããå«ãŸããŠãããå Žåã«ãã£ãŠã¯æ°åãŸãã¯æ°çŸã®ã±ãŒã¹ã«ãŸã§æ¡å€§ããããšããããŸãã ã¬ãã¥ãŒãµãŒã®å¯äžã®è²¬ä»»ã¯ãã¢ããªã±ãŒã·ã§ã³ã®ç¶æ é·ç§»ã管çããããšã§ãã ã€ãŸããæåéããäžéšã®å¿åè ãçããŸããã ãŸãããã®æèŠãè£ä»ãããã³ãã¯ãããŸããã
å šäœãšããŠãããçš®ã®ã³ãŒããSRPã®ååãæºãããŠããããã«èŠããããåæã«äžå¿«ãªã«ãããããå Žå-ãªããããèµ·ããã®ããç¥ã£ãŠãã ããã ãã³ãŒãã«ã¯1ã€ã®è²¬ä»»ãå¿ èŠããšããå®çŸ©ãæ©èœããªãããã§ãã
ããé©åãªå®çŸ©
è©Šè¡é¯èª€ãããããè¯ãå®çŸ©ãã§ããŸããïŒ
ã³ãŒãã®è²¬ä»»ã¯å€§ããããŠã¯ãããŸãã
ã¯ããã¯ã©ã¹ãŸãã¯é¢æ°ã®è²¬ä»»ãã枬å®ãããå¿ èŠããããŸãã ãããŠãããã倧ããããå Žåããã®å€§ããªè²¬ä»»ãããã€ãã®å°ããªè²¬ä»»ã«åå²ããå¿ èŠããããŸãã èŸ²å Žã®äŸã«æ»ããšãé¶ã飌è²ãã責任ããã倧ããããå¯èœæ§ããããããšãã°ããã€ã©ãŒãå±€ããäœããã®æ¹æ³ã§åé¢ããããšã¯çã«ããªã£ãŠããŸãã
ããããããã枬å®ããæ¹æ³ããã®ã³ãŒãã®è²¬ä»»ã倧ãããããšå€æããæ¹æ³ã¯ïŒ
æ®å¿µãªãããç§ã¯æ°åŠçã«æ£ç¢ºãªæ¹æ³ã¯ãªããçµéšçãªæ¹æ³ãããããŸããã ãããŠããããã®ãã¹ãŠã¯çµéšã«åºã¥ããŠããŸããåå¿è ã®éçºè ã¯ã³ãŒããå解ããããšã¯ã§ããŸãããããé«åºŠãªéçºè ã¯ã³ãŒããææããã®ã«åªããŠããŸãã
- ã¡ããªãã¯åŸªç°çè€é床ã æ®å¿µãªããããã®ã¡ããªãã¯ããã¹ã¯ããæ¹æ³ããããŸãããåéãããšãã¢ããªã±ãŒã·ã§ã³å ã§æãè匱ãªå Žæã衚瀺ãããå¯èœæ§ããããŸãã
- é¢æ°ãšã¯ã©ã¹ã®ãµã€ãºã 800è¡ã®é¢æ°ã¯ãäœãåé¡ãããããšãç解ããããã«èªãå¿ èŠã¯ãããŸããã
- ããããã®èŒžå ¥ã é£ã®ããŒã ã®ãããžã§ã¯ãã§ãã¡ã€ã«ãéããšãã€ã³ããŒãã®ç»é¢å šäœã衚瀺ãããããŒãžãäžã«æŒããšãç»é¢ã«ã¯ã€ã³ããŒãã®ã¿ã衚瀺ãããŠããŸããã 2åç®ã®ãã¬ã¹ã®åŸãã³ãŒãã®å§ãŸããèŠãŸããã ãã¹ãŠã®ææ°ã®IDEã¯ãã©ã¹èšå·ã®äžã§ã€ã³ããŒããé衚瀺ã«ã§ãããšèšãããšãã§ããŸãããè¯ãã³ãŒãã¯ãåãããé衚瀺ã«ããå¿ èŠã¯ãªããšèšããŸãã ããã«ãå°ããªã³ãŒããåå©çšããå¿ èŠããã£ãããããã®ãã¡ã€ã«ããå¥ã®ã³ãŒãã«åé€ããã€ã³ããŒãã®4åã®1ãŸãã¯3åã®1ããã®éšåã®åŸãã«ç§»åããŸããã ãã®ã³ãŒãã¯æããã«ããã«å±ããŠããŸããã§ããã
- åäœãã¹ãã ããã§ã責任ã®éãå€æããã®ãå°é£ãªå Žåã¯ããã¹ããæžãããã«åŒ·å¶ããŠãã ããã å¢çç·ã®ã±ãŒã¹ãªã©ãã«ãŠã³ãããã«ãé¢æ°ã®äž»ãªç®çã§20åã®ãã¹ããèšè¿°ããå¿ èŠãããå Žåã¯ãå解ãå¿ èŠã§ãã
- åãããšã¯ããã¹ãã®éå§æã®æºå段éãå€ãããŠãçµäºæã®ãã§ãã¯ã«ãåœãŠã¯ãŸããŸãã ã¡ãªã¿ã«ãã€ã³ã¿ãŒãããäžã§ã¯ããããã ãã¹ãã§ã¢ãµãŒãããã®ã¯äžè¬ã«1ã€ã ãã§ãã ããããç§ã¯ãarbitrarilyæçã«è¯ãã¢ã€ãã¢ã絶察çãªãã®ã«äžãããããšãã°ãããŠéçŸå®çã«ãªããšä¿¡ããŠããŸãã
- ããžãã¹ããžãã¯ã¯ãå€éšããŒã«ã«çŽæ¥äŸåããªãã§ãã ããã Oracleãã©ã€ããŒã®Expressã«ãŒãã§ã¯ãããããã¹ãŠãããžãã¹ããžãã¯ããåé¢ããããã€ã³ã¿ãŒãã§ãŒã¹ã®èåŸã«é ãããããããšãæãŸããã§ãã
ããã€ãã®ãã€ã³ãïŒ
ãã¡ããããã§ã«è¿°ã¹ãããã«ãã³ã€ã³ã«ã¯è£è¿ããããã1è¡ã®800ã¡ãœããã¯800è¡ã®1ã¡ãœãããããè¯ããªããããããŸããããã¹ãŠã®ãã©ã³ã¹ãåããŠããã¯ãã§ãã
第äº-ç§ã¯ããã®ã³ãŒãããã®è²¬ä»»ã«åŸã£ãŠã©ãã«çœ®ãã¹ãããšããåé¡ãã«ããŒããŠããŸããã ããšãã°ãéçºè ãDALã¬ã€ã€ãŒã«å€ãã®ããžãã¯ãåã蟌ãã®ãé£ããå ŽåããããŸãã
第äžã«ããé¢æ°ããã50è¡ä»¥äžããªã©ã®ç¹å®ã®ããŒãå¶éãææ¡ããŸããã ãã®ã¢ãããŒãã«ã¯ãéçºè ããããŠããããããŒã ã®éçºã®æ¹åæ§ã®ã¿ãå«ãŸããŸãã 圌ã¯ç§ã®ããã«åããŠããŸãã圌ã¯ä»ã®äººã®ããã«ãéã皌ããªããã°ãªããŸããã
ãããŠæåŸã«ãTDDã䜿çšããå Žåãããã ãã§ããããã20åã®ã¢ãµãŒã·ã§ã³ãå«ã20åã®ãã¹ããäœæããã¯ããåã«ãã³ãŒãã確å®ã«å解ã§ããŸãã
å®åã³ãŒãããããžãã¹ããžãã¯ãåé¢ãã
è¯ãã³ãŒãã®ã«ãŒã«ã«ã€ããŠèšãã°ãäŸããªããã°ã§ããŸããã æåã®äŸã¯ãå®åã³ãŒãã®åé¢ã«é¢ãããã®ã§ãã
ãã®äŸã¯ãããã¯ãšã³ãã³ãŒãã®éåžžã®èšè¿°æ¹æ³ã瀺ããŠããŸãã 人ã ã¯éåžžãURLããªã¯ãšã¹ãã¡ãœãããªã©ã®WebãµãŒããŒExpressãžã®ãã©ã¡ãŒã¿ã瀺ãã³ãŒãã䜿çšããŠããžãã¯ãå¯æ¥ã«èšè¿°ããŸãã
ããžãã¹ããžãã¯ãç·è²ã®ããŒã«ãŒãšããŠããŒã¯ããèµ€è²ã®æ£åšã³ãŒããã¯ãšãªãã©ã¡ãŒã¿ãŒãšçžäºäœçšããŸãïŒèµ€è²ïŒã
ç§ã¯ãã®2ã€ã®è²¬ä»»ãåžžã«ãã®ããã«å ±æããŠããŸãã
ãã®äŸã§ã¯ãExpressãšã®ãã¹ãŠã®å¯Ÿè©±ã¯å¥ã®ãã¡ã€ã«ã«ãããŸãã
äžèŠãããšããã2çªç®ã®äŸã§ã¯æ¹åãèŠãããªãã£ãããã«èŠãããããããŸããã1ã€ã§ã¯ãªã2ã€ã®ãã¡ã€ã«ãããã以åã¯ååšããŠããªãã£ãè¿œå è¡ã衚瀺ãããŸããã ãããŠããã®ã³ãŒãã®åé¢ã¯äœããããããŸããïŒ ãŸãããã¢ããªã±ãŒã·ã§ã³ãšã³ããªãã€ã³ããã¯Expressã§ã¯ãªããªããŸããã ããã¯éåžžã®Typescripté¢æ°ã§ãã ãŸãã¯ãCïŒãã©ããã«é¢ä¿ãªãWebAPIãäœæããJavaScripté¢æ°ã
ããã«ãããæåã®äŸã§ã¯äœ¿çšã§ããªãããŸããŸãªã¢ã¯ã·ã§ã³ãå®è¡ã§ããŸãã ããšãã°ãExpressãçºçãããããšãªãããã¹ãå ã§httpèŠæ±ã䜿çšããã«ãåäœãã¹ããäœæã§ããŸãã ãããŠãã©ããªçš®é¡ã®æ¿¡ããããå¿ èŠããªããšããŠããRouterãªããžã§ã¯ããããã¹ãããªããžã§ã¯ãã«çœ®ãæããã°ãã¢ããªã±ãŒã·ã§ã³ã³ãŒãããã¹ãããçŽæ¥åŒã³åºãããšãã§ããŸãã
ãã®å解ã«ãã£ãŠæäŸãããå¥ã®èå³æ·±ãæ©èœã¯ãuserApiServiceã解æãããã®ãµãŒãã¹ãExpressã«æ¥ç¶ããããŒã¹ã§ã³ãŒããçæããã³ãŒããžã§ãã¬ãŒã¿ãŒãäœæã§ããããšã§ãã ä»åŸã®åºçç©ã§ã¯ã次ã®ããšã瀺ãäºå®ã§ããã³ãŒãçæã§ã¯ãã³ãŒããèšè¿°ããããã»ã¹ã®æéãç¯çŽã§ããŸããã ã³ãŒããžã§ãã¬ãŒã¿ãŒã®ã³ã¹ãã¯ããã®å®åæãã³ããŒããå¿ èŠããªããªã£ããšããäºå®ã«ãã£ãŠå ±ãããŸããã ã³ãŒãçæã¯ããããçæããã³ãŒãããµããŒããå¿ èŠãšããªããšããäºå®ã«ãã£ãŠå ±ãããŸããããã¯æéãç¯çŽããæãéèŠãªããšãšããŠãé·æçã«ã¯éçºè ã®ç¥çµãç¯çŽããŸãã
åå²ããŠåŸæãã
ã³ãŒããèšè¿°ãããã®æ¹æ³ã¯é·ãéååšããŠãããç§ã¯èªåã§çºæããŸããã§ããã ããžãã¹ããžãã¯ãèšè¿°ãããšãã«éåžžã«äŸ¿å©ã§ãããšããçµè«ã«éããŸããã ãã®ããã«ãå¥ã®æ¶ç©ºã®äŸãæãã€ããŸãããããã«ç°¡åã«ã³ãŒããèšè¿°ããããã«é©åã«å解ãããåœåæ¹æ³ã«ãã£ãŠèªå·±ææžåãããã³ãŒããäœæããæ¹æ³ã瀺ããŸããã
ããžãã¹ã¢ããªã¹ãããã¿ã¹ã¯ãååŸããŠãåŸæ¥å¡ã¬ããŒããä¿éºäŒç€Ÿã«éä¿¡ããã¡ãœãããäœæãããšããŸãã ãããè¡ãã«ã¯ïŒ
- ããŒã¿ããŒã¹ããããŒã¿ãååŸããå¿ èŠããããŸã
- ç®çã®åœ¢åŒã«å€æãã
- çµæã®ã¬ããŒããéä¿¡
ãã®ãããªèŠä»¶ã¯ãåžžã«æ瀺çã«èšè¿°ãããŠããããã§ã¯ãªããã¢ããªã¹ããšã®äŒè©±ãããã®ãããªã·ãŒã±ã³ã¹ãæ瀺ããããæ確ã«ãããå ŽåããããŸãã ãã®ã¡ãœãããå®è£ ããããã»ã¹ã§ã¯ãããŒã¿ããŒã¹ãŸãã¯ãããã¯ãŒã¯ãžã®æ¥ç¶ãæ¥ãã§éãã®ã§ã¯ãªãããã®åçŽãªã¢ã«ãŽãªãºã ãããã®ãŸãŸãã³ãŒãã«å€æããŠã¿ãŠãã ããã ãã®ãããªãã®ïŒ
async function sendEmployeeReportToProvider(reportId){ const data = await dal.getEmployeeReportData(reportId);â const formatted = reportDataService.prepareEmployeeReport(data);â await networkService.sendReport(formatted);â }
ãã®ã¢ãããŒãã䜿çšãããšãããªãã·ã³ãã«ã§èªã¿ãããã³ãŒããååŸã§ããŸããããã®ã³ãŒãã¯ç°¡åã§ããã¹ãã®å¿ èŠã¯ãªããšæããŸãã ãŸããã¬ããŒããéä¿¡ããªãã®ã¯ãã®ã¡ãœããã®è²¬ä»»ã§ããããã®è²¬ä»»ã¯ãã®è€éãªã¿ã¹ã¯ã3ã€ã®ãµãã¿ã¹ã¯ã«åå²ããããšã§ããã
次ã«ãèŠä»¶ã«æ»ããã¬ããŒãã絊äžã»ã¯ã·ã§ã³ãšåŽåæéã®ã»ã¯ã·ã§ã³ã§æ§æãããŠããå¿ èŠãããããšã確èªããŸãã
function prepareEmployeeReport(reportData){ const salarySection = prepareSalarySection(reportData);â const workHoursSection = prepareWorkHoursSection(reportData);â return { salarySection, workHoursSection };â }
ãã®ä»ãäºçŽ°ã«è¿ãå°ããªã¡ãœããã®å®è£ ãæ®ããŸã§ãã¿ã¹ã¯ãåå²ãç¶ããŸãã
ãªãŒãã³ã¯ããŒãºååãšã®çžäºäœçš
èšäºã®åé ã§ãSRPãšOpen-Closeã®ååã®å®çŸ©ã¯äºãã«ççŸããŠãããšè¿°ã¹ãŸããã åè ã¯å€æŽã®çç±ã1ã€ããã«éããªããšè¿°ã¹ãåŸè ã¯å€æŽã®ããã«ã³ãŒããéããå¿ èŠããããšè¿°ã¹ãŠããŸãã ãããŠãååèªäœã¯ãäºãã«ççŸããªãã ãã§ãªããå察ã«ãçžäºã«çžä¹å¹æãçºæ®ããŸãã 5ã€ã®SOLIDååã¯ãã¹ãŠã1ã€ã®è¯ãç®çãç®æããŠããŸã-ã©ã®ã³ãŒãããæªããããã©ã®ããã«å€æŽããŠãè¯ããã³ãŒãã«ãªãããéçºè ã«äŒããããšã§ãã ç®èãªããšã«ãç§ã¯5ã€ã®è²¬ä»»ããã1ã€ã®è²¬ä»»ã«çœ®ãæããŸããã
ãããã£ãŠãã¬ããŒããä¿éºäŒç€Ÿã«éä¿¡ããåã®äŸã«å ããŠãããžãã¹ã¢ããªã¹ããæ¥ãŠãä»åºŠã¯ãããžã§ã¯ãã«2ã€ç®ã®æ©èœãè¿œå ããå¿ èŠããããšèšã£ãŠããããšãæ³åããŠãã ããã åãã¬ããŒããå°å·ããå¿ èŠããããŸãã
SRPã¯ãå解ã«é¢ãããã®ã§ã¯ãªãããšèããŠããéçºè ããããšæ³åããŠãã ããã
ãããã£ãŠããã®ååã¯åœŒã«å解ã®å¿ èŠæ§ã瀺ããŠãããã圌ã¯1ã€ã®æ©èœã§æåã®ã¿ã¹ã¯å šäœãå®çŸããŸããã ã¿ã¹ã¯ã圌ã«æ¥ãåŸã圌ã¯2ã€ã®è²¬ä»»ã1ã€ã«ãŸãšããŸãã ãããã®éã«ã¯å€ãã®å ±éç¹ãããããã®ååã¯äžè¬åããŠããŸãã çŸåšããã®è²¬ä»»ã¯ããµãŒãã¹ã¬ããŒãããšåŒã°ããŠããŸãã ãã®å®è£ ã¯æ¬¡ã®ããã«ãªããŸãã
async function serveEmployeeReportToProvider(reportId, serveMethod){ /* lots of code to read and convert the report */ switch(serveMethod) { case sendToProvider: /* implementation of sending */ case print: /* implementation of printing */ default: throw; } }
ãããžã§ã¯ãã®ã³ãŒããæãåºããŸããïŒ ç§ãèšã£ãããã«ãSRPã®çŽæ¥çãªå®çŸ©ã¯äž¡æ¹ãšãæ©èœããŸããã ãã®ãããªã³ãŒããèšè¿°ã§ããªããšããæ å ±ãéçºè ã«éä¿¡ããããšã¯ãããŸããã ãããŠãã©ã®ã³ãŒããæžãããšãã§ããŸãã éçºè ããã®ã³ãŒããå€æŽããçç±ã¯ãŸã 1ã€ã ãã§ãã 圌ã¯åã«ä»¥åã®çç±ãåŒã³çŽããã¹ã€ãããè¿œå ããèœã¡çããŠããŸãã ãããŠãããã§ã¯ãOpen-Closeååã®åçãç»å ŽããŸããããã¯ãæ¢åã®ãã¡ã€ã«ãå€æŽããããšã¯äžå¯èœã§ãããšçŽæ¥è¿°ã¹ãŠããŸãã æ°ããæ©èœãè¿œå ãããšãã«ãæ¢åã®ãã¡ã€ã«ãç·šéããã®ã§ã¯ãªããæ°ãããã¡ã€ã«ãè¿œå ããå¿ èŠãããããã«ã³ãŒããèšè¿°ããå¿ èŠããããŸããã ã€ãŸãããã®ãããªã³ãŒãã¯äžåºŠã«2ã€ã®ååã®èŠ³ç¹ããã¯æªãã§ãã ãããŠãããæåã®ãã®ããããèŠãã®ãå©ããªãã£ããã2çªç®ã®ãã®ãå©ãã«ãªãã¯ãã§ãã
ãããŠãåå²çµ±æ²»æ³ã¯åãåé¡ãã©ã®ããã«è§£æ±ºããŸããïŒ
async function printEmployeeReport(reportId){ const data = await dal.getEmployeeReportData(reportId);â const formatted = reportDataService.prepareEmployeeReport(data);â await printService.printReport(formatted);â }
æ°ããé¢æ°ãè¿œå ããŸãã å®è£ ãæããªãããããã¹ã¯ãªããé¢æ°ããšãåŒã°ããããšãããã責任ã®å解éšåãåŒã³åºãé åºã決å®ããŸãã æããã«ãæåã®2è¡ãæåã®2ã€ã®å解ããã責任ã¯ã以åã«å®è£ ãããé¢æ°ã®æåã®2è¡ãšäžèŽããŠããŸãã ããžãã¹ã¢ããªã¹ãã説æãã2ã€ã®ã¿ã¹ã¯ã®æåã®2ã€ã®ã¹ããããäžèŽããããã«ã
ãããã£ãŠããããžã§ã¯ãã«æ°ããæ©èœãè¿œå ããããã«ãæ°ããã¹ã¯ãªããã¡ãœãããšæ°ããprintServiceãè¿œå ããŸããã å€ããã¡ã€ã«ã¯å€æŽãããŠããŸããã ã€ãŸãããã®ã³ãŒãèšè¿°æ¹æ³ã¯ã2ã€ã®ååã®èŠ³ç¹ããèŠãŠé©åã§ãã ãããŠãSRPãšãªãŒãã³ã¯ããŒãº
代æ¿æ¡
ãŸãã次ã®ãããªé©åã«å解ãããã³ãŒããååŸããããã®ä»£æ¿ã®ç«¶åæ¹æ³ã«ã€ããŠãèšåããããšæããŸãããŸãããé¡ã«ãã³ãŒããèšè¿°ãããã®åŸããã¡ãŠã©ãŒã®æ¬ããªãã¡ã¯ã¿ãªã³ã°ãã«åŸã£ãŠãããŸããŸãªææ³ã䜿çšããŠãªãã¡ã¯ã¿ãªã³ã°ããŸã ãããã®æ¹æ³ã¯ããã§ã¹ã®ã²ãŒã ã«å¯Ÿããæ°åŠçã¢ãããŒããæãåºãããŸãããæŠç¥ã®èŠ³ç¹ããèªåãäœãããŠããããæ£ç¢ºã«ç解ãããèªåã®ããžã·ã§ã³ã®ãéã¿ããèšç®ããåããå ããŠæ倧åããããšããŸãã ã¡ãœãããšå€æ°ã«ååãä»ããããšã¯æ¢ã«å°é£ã§ãããããžãã¹äŸ¡å€ããªãå Žåã¯äžå¯èœã«ãªããšããå°ããªçç±ã§ããã®ã¢ãããŒãã奜ãã§ã¯ãããŸããã§ããã ããšãã°ããããã®ææ³ã§ããã¡ãã¡ãã6ã€ã®åäžã®è¡ãéžæããå¿ èŠãããããšã瀺åãããŠããå ŽåãããããéžæããŠããã®ã¡ãœãããäœãšåŒã³ãŸããïŒ someSixIdenticalLinesïŒïŒïŒ
äºçŽãããã-ãã®æ¹æ³ãæªããšã¯æããªãã䜿ãæ¹ãåŠã¶ããšãã§ããªãã£ãã
åèš
ååã«åŸããšãã¡ãªãããåŸãããŸãã
ã1ã€ã®è²¬ä»»ããªããã°ãªããªãããšããå®çŸ©ã¯æ©èœããŸããã
ããè¯ãå®çŸ©ãšããã€ãã®éæ¥çãªæ©èœããããã ã³ãŒãã¯å解ã®å¿ èŠæ§ã瀺ãåããããŸãã
åå²çµ±æ²»ã®ã¢ãããŒãã«ãããããã«é©åã«æ§é åãããèªå·±ææžåã³ãŒããæžãããšãã§ããŸãã