SOLIDã¢ãŒããã¯ãã£ïŒRobert Martinã®èè ïŒã®éçºã®ååã®èª¬æãšè§£éã«ã¯åé¡ããããŸãã å€ãã®æ å ±æºããã®å®çŸ©ãšäœ¿çšäŸã瀺ããŠããŸãã ããããç 究ããèªåèªèº«ã䜿ã£ãŠã¿ãããšãããšãç§ã¯åœŒãã®ã¢ããªã±ãŒã·ã§ã³ã®éæ³ã«ã€ããŠååãªèª¬æããªããšåžžã«èããŠããŸããã ãããŠãå éšã®ã¢ãèŠãŠãç解ããããã«ããããŠç§ã«ãšã£ãŠã¯èŠããŠããããã«ããããããã¿ãŒã ã·ã§ã«ããã«é 眮ããããšããŸããã ãŸãããããä»ã®èª°ãã«åœ¹ç«ã€ãªãã
äžèšã®èšèšã¢ãããŒãã®ãæ£ã®ãžã£ã°ãªã³ã°ãã«é²ã¿ãŸãã
åäžè²¬ä»»ååïŒSRPïŒåç¬è²¬ä»»åå
1ã€ã®ã³ãŒãã¯ã1ã€ã®ç®æšã®å®è£ äžã«ã®ã¿å€æŽããå¿ èŠããããŸãã ã³ãŒãã®ã»ã¯ã·ã§ã³ã2ã€ã®ã¿ã¹ã¯ãšç°ãªãçšéã®å€æŽãå®è£ ããå Žåãç®çããšã«ã€ã³ã¹ã¿ã³ã¹ã«ãã®ã»ã¯ã·ã§ã³ãè€è£œããå¿ èŠããããŸãã ããã¯ãéè€ãæé€ãããšããäžè¬ã«åãå ¥ããããŠããååããã®éžè±ãå¿ èŠãšãããããéåžžã«éèŠã§ãã
ãã®ååã®ç®çã¯ãã³ãŒãã»ã¯ã·ã§ã³ãããã·ãŒãžã£ãã¯ã©ã¹ãã³ã³ããŒãã³ãã®éçºã«ä»¥äžã®äžå€æ¡ä»¶ãååšãããšããäºå®ã«èµ·å ããæé»ã®ãšã©ãŒãæé€ããããšã§ãïŒä»¥äžã[ã³ã³ããŒãã³ã]ãšããçšèªã¯ãããã®æŠå¿µãçµã¿åãããããã«äœ¿çšãããŸãïŒã
- [1]æ£ããæžããã[ã³ã³ããŒãã³ã]ãå¿ ã䜿çšãããããé »ç¹ã«æ°å䜿çšãããŸãã
- [2]䜿çšã®åå Žæã§ã[ã³ã³ããŒãã³ã]ã¯äžè²«ããåäœãç¶æããåçŸå¯èœãªçµæãââããããããšãæåŸ ãããŸãã
- [3] [ã³ã³ããŒãã³ã]ãè€æ°ã®å Žæã§äœ¿çšããå Žåãçµæã¯å䜿çšå Žæãæºããå¿ èŠããããŸãã
- 䜿çšå Žæã®1ã€ã§[ã³ã³ããŒãã³ã]ã®å€æŽãå¿ èŠã§ãå¥ã®äœ¿çšå Žæã§[ã³ã³ããŒãã³ã]ã®ä»¥åã®åäœãå¿ èŠãªå Žåããã®åŸã®å€æŽïŒãŸãã¯ç°ãªãåäœãæäŸããè¿œå ãã©ã¡ãŒã¿ã«ãã[ã³ã³ããŒãã³ã]ã®äžè¬åïŒã§[ã³ã³ããŒãã³ã]ã®ã³ããŒãäœæããå¿ èŠããããŸãã
ããã°ã©ãã解決ããçŸåšã®ã¿ã¹ã¯ã«ãšã£ãŠéèŠã§ã¯ãªã[ã³ã³ããŒãã³ã]ã®äœ¿çšå Žæãããå Žåããã®[ã³ã³ããŒãã³ã]ã«å ããããå€æŽã®ãããã®äœ¿çšå Žæãšã®äºææ§ã®ãã§ãã¯ãå¿ããã®ã¯éåžžã«ç°¡åã§ãã
ãããã£ãŠããã¹ãŠã®äœ¿çšå Žæã¯ãåäžã®è²¬ä»»ã®[åäžè²¬ä»»]ãŸãŒã³ã«é 眮ããå¿ èŠããããŸããã€ãŸããããã°ã©ããŒã«ãã£ãŠè§£æ±ºãããåé¡ã«ã€ããŠã¯ãäžåºŠã«å€æŽããŠèæ ®ããŠãã ããã
ãã®ååã¯ãã³ãŒãã®ã»ã¯ã·ã§ã³ãšãè€æ°ã®å Žæã§äœ¿çšãããã³ã³ããŒãã³ããã©ã€ãã©ãªãããã°ã©ã ãããã°ã©ã ã®ã»ããã®äž¡æ¹ã«é©çšãããŸãã
å€ãã®ãœãŒã¹ã¯ãSRPã®çæ³ãšããŠãé¢æ°ãã1ã€ã ãæã€ã¯ã©ã¹ãšãã¢ããªã±ãŒã·ã§ã³ã®ãã¹ãŠã®æ©èœãã¢ã³ããã¿ãŒã³ãšããŠçµã¿åããããç¥ãªããžã§ã¯ããã®ã¯ã©ã¹ã®äŸã瀺ããŠããŸãã ãé¢æ°ãã1ã€ãããªãIMHOã¯ã©ã¹ã¯ãã³ãŒãã¢ãŒããã¯ãã£ã®ææå°æ©ãªæé©åã®èŠä»¶ã§ãããæåããå€ãã®ã¯ã©ã¹ïŒã³ãŒããšã³ãã£ãã£ïŒãæžãããšãä¿ããŸãã ãæ©èœããæ åœããç°çš®ã³ãŒããšã³ãã£ãã£ã®å€éšé¢ä¿ãåæããããããã³ãŒããçžäºäœçšãããã å°ããªã¢ããªã±ãŒã·ã§ã³ã®ãç¥ã®ãªããžã§ã¯ããã匷åãªç¯çœªã§ã¯ãããŸãããéçºãéå§ã§ããŸããå¿ èŠãªãã¹ãŠã®ãšã³ãã£ãã£ãéžæããããããæžãçããŠãæšæºã©ã€ãã©ãªãšå€éšã¢ãžã¥ãŒã«ã®å€éšãªããžã§ã¯ãããåé¢ããŸãïŒç现èãäœæããèã§åé¢ããŸãïŒã ãããžã§ã¯ãã®æé·ãšéçºã®éçšã§ãSRPã远跡ããã®ã«åœ¹ç«ã€å€ãã®æ¹æ³ããããŸãããããã®1ã€ã¯ãã¯ã©ã¹ãžã®åå²ã§ãããåã¯ã©ã¹ãæ åœãããæ©èœãã®æ°ãæå°éã«æããŸãïŒçŽ°èã®åå²ãšäœå ã§ã®å°éåïŒã
ããã§ãSRPãç¶æããããã®äžé£ã®ãã¯ããã¯ãæžããããšæããŸããããã®äœæ¥ã¯ãŸã å®äºããŠããŸããïŒãæã䌞ã°ããããšãæã¿ãŸãïŒã ãããã®ããªãã¯ãæ¢ãããšãã§ããæçœãªé åããïŒ
- ãã¶ã€ã³ãã¿ãŒã³;
- ãã¹ãŠã®ã¢ããªã±ãŒã·ã§ã³ã¡ãœããïŒGitHubã®ãã©ãŒã¯ïŒãæºããã³ã³ããŒãã³ããäœæããã®ã§ã¯ãªããç°ãªãç¹æ®ãªã³ã³ããŒãã³ããã©ã³ãã䜿çšããŸãã
ééåçïŒOCPïŒééåç
ããã°ã©ããæ°ããã¿ã¹ã¯ãå®è£ ããã«ã¯ãæ°ããã³ãŒããè¿œå ããå¿ èŠããããå€ãã³ãŒãã¯å€æŽããå¿ èŠããªãããã«ãã³ãŒãã®éçºãèšç»ããããšãæé©ã§ãã è¿œå ããã«ã¯ã³ãŒããéããŠïŒéãïŒãå€æŽããã«ã¯ã³ãŒããéããŠïŒéããïŒãªããã°ãªããŸããã
ãã®ååã®ç®çã¯ã人件費ãæå°éã«æããéçºã«ããã次ã®äžå€æ¡ä»¶ã«èµ·å ããæé»ã®ãšã©ãŒãæé€ããããšã§ãã
- åè¿°ã®[1]ã[2]ã[3]
- æ°ããã¿ã¹ã¯ãå®è£ ããããã«ãããã°ã©ãã¯æ°ãã[ã³ã³ããŒãã³ã]ãè¿œå ããããå€ã[ã³ã³ããŒãã³ã]ã®åäœãå€æŽãããã§ããŸãã
- [ã³ã³ããŒãã³ã]ã®è¿œå ã«ã¯ãæ°ãã䜿çšå Žæã§ã®æ€èšŒãå¿ èŠã§ãããããã°ã©ããŒã®æéãèŠããŸãã
- æ°ããã¿ã¹ã¯ã«ãã£ãŠåŒãèµ·ãããã[ã³ã³ããŒãã³ã]ã®åäœã®å€æŽã«ã¯ãæ°ãã䜿çšã®å Žæãšå€ã䜿çšã®ãã¹ãŠã®å Žæã§ã®æ€èšŒãå¿ èŠã§ãããããã°ã©ãã®æéãããããŸããå ¬éããã[ã³ã³ããŒãã³ã]ã®å Žåã[ã³ã³ããŒãã³ã]ã䜿çšãããã¹ãŠã®ããã°ã©ãã®äœæ¥
ããã°ã©ããè²»ããæéãæå°éã«æããªãããæ°ããã¿ã¹ã¯ãå®è£ ãããªãã·ã§ã³ãéžæããããšããå§ãããŸãã
å€ãã®å ŽåããœãããŠã§ã¢éçºã®å®è·µã§ã¯ãè¿œå ã®ã³ã¹ãã¯å€æŽã®ã³ã¹ããããã¯ããã«å°ãªãããã[Open-Closed]ååã䜿çšããã¡ãªãããæããã«ãªããŸãã åæã«ãæ°ããã¿ã¹ã¯ã®å®è£ ã[ã³ã³ããŒãã³ã]ã®ã¿ãè¿œå ããããšã«ãªãç¶æ ã§ããã°ã©ã ã¢ãŒããã¯ãã£ãç¶æããããã®å€ãã®ãã¯ããã¯ããããŸãã ãã®ã¢ãŒããã¯ãã£ã§ã®äœæ¥ã«ãããã°ã©ããŒã®æéãå¿ èŠã§ããã倧èŠæš¡ãªãããžã§ã¯ãã§ã®ç·Žç¿ã§ã¯ãå€ãæé ãå€æŽããã¢ãããŒãã䜿çšãããããã¯ããã«å°ãªãããšãããããŸãã ãããŠãã¡ããããã®éçºã®èª¬æã¯çæ³åã§ãã è¿œå ãŸãã¯å€æŽããã ãã§ã¯ãã¿ã¹ã¯ã®å®è£ ã¯ã»ãšãã©ãããŸããã å®éã«ã¯ããããã®ã¢ãããŒããçµã¿åãããŠäœ¿çšââããŸãããOCPã¯addã¢ãããŒãã䜿çšããå©ç¹ã匷調ããŠããŸãã
ãããŠãããã§OCPãç¶æããããã®äžé£ã®ãã¯ããã¯ãæžããããšæããŸãã ãããã®ããªãã¯ãæ¢ãããšãã§ããæçœãªé åããïŒ
- ãã¶ã€ã³ãã¿ãŒã³;
- æ©èœã®é åžãæŽæ°ãéçºã®ããã®dllã©ã€ãã©ãªãšãªãã·ã§ã³ã
- COMã©ã€ãã©ãªãšãã®äžã®ãªããžã§ã¯ãã®éçºã
- ããã°ã©ãã³ã°èšèªã®éçºãšä»¥åã«èšè¿°ãããã³ãŒãã®ãµããŒãã
- å·ã®ç«æ³ã·ã¹ãã ãéçºããã
Liskov Substitution PrincipleïŒLSPïŒããŒãã©Liskov Substitution Principle
ãã®ååã¯ãããŒã¹ã€ã³ã¿ãŒãã§ã€ã¹[base]ã®æ¡åŒµã®äœ¿çšãå®è£ ã«éå®ããããŒã¹ã€ã³ã¿ãŒãã§ã€ã¹ã®åå®è£ ãããŒã¹ã€ã³ã¿ãŒãã§ã€ã¹ãšããŠåäœããå¿ èŠãããããšã瀺ããŠããŸãã åæã«ãåºæ¬çãªã€ã³ã¿ãŒãã§ã€ã¹ã¯ããã®äœ¿çšå Žæã§äºæ³ãããåäœãä¿®æ£ããŸãã ãŸããããŒã¹ã€ã³ã¿ãŒãã§ã€ã¹ã«ãã£ãŠä¿®æ£ããããäºæ³ãããåäœãšã¯ç°ãªãå®è£ åäœã®ååšããäžå€åŒã®éåã®å¯èœæ§ã«ã€ãªãããŸã[2]ã
ãã®ååã¯ãæœè±¡åã«åºã¥ããŠèšèšææ³ãããŒã¹ã«ããŠãããæŽç·ŽãããŠããŸãã ãã®ã¢ãããŒãã§ã¯ãæœè±¡åãå°å ¥ãããŸã-å€ãã®ç¶æ³ã«ç¹æã®ããã€ãã®åºæ¬çãªããããã£ãšåäœãä¿®æ£ãããŸããã ããšãã°ã[component-procedure]ãç¶æ³ã®åã®äœçœ®ã«ç§»åãïŒãããã¹ãå ã®ã«ãŒãœã«ãããæ£ã®äžã®æ¬ãããé åã®èŠçŽ ããããã³ã¹ã®è¶³ããªã©ããããŠããã®[component]ïŒå€ãã®å Žåãæ¥åžžçãªçµéšã«ãããã®ã§ããã圢åŒåãããŠããªãïŒããã€ãã®åææ¡ä»¶ãšåäœãããšãã°ïŒã移åå¯èœãªãªããžã§ã¯ãã®ååšãããæ°åç¹°ãè¿ããããèŠçŽ ã®é åºã®ååšãããèŠçŽ ã®åºå®äœçœ®ã®ååšãã LSPã§ã¯ã[component]ã®æ°ãã䜿çšç¶æ³ãè¿œå ãããšãã«ãããŒã¹ã®ãã¹ãŠã®åææ¡ä»¶ãšå¶éãæºããããŠããå¿ èŠããããŸãã ãããŠããç ç³çŒ¶ã®äžã®ç©ç©ãã®ç¶æ³ã¯ããã®æœè±¡åã§ã¯èª¬æã§ããŸããããç©ç©ã«ã¯ãã¡ããäœçœ®ããããç©ç©ã以åã«ãã£ãäœçœ®ããããç©ç©ããããã®äžã«ç§»åããããšãã§ããŸã-èŠçŽ ã®åºå®äœçœ®ã¯ãããŸãã
ãã®ååã®ç®çã¯ãéçºã«ããã次ã®äžå€æ¡ä»¶ã«èµ·å ããæé»ã®ãšã©ãŒãæé€ããããšã§ãã
- åè¿°ã®[1]ã[2]ã[3]ã
- åºæ¬çãª[æé ]ã§ã¯ãå€æ°ã®ç¶æ³ã§åœ¹ç«ã€åäœã説æãããã®é©çšã«å¿ èŠãªå¶çŽãèšå®ããŸãã
ããŒã¹å®è£ ã®éçºããã[æé ]ã¯ãããŒããã©ãã¯ãããæé»ã®ïŒéå ¬åŒã«æäŸãããïŒå¶éãå«ããã¹ãŠã®å¶éãæºãããªããã°ãªããŸããã
å€ãã®å Žåããã®ååã説æããããã«ãé·æ¹åœ¢ïŒ[ããŒã¹]ïŒãšæ£æ¹åœ¢ïŒå®è£ ïŒã®äŸã瀺ããŸãã ç¶æ³
class CSquare : public CRectangle
ã [base]ã§ã¯ãå¹ ãšé«ããæäœããæäœïŒSetïŒGetïŒWidthãSetïŒGetïŒHeightïŒãå°å ¥ãããŠããŸãã CSquareã®å®è£ ã§ã¯ããããã®Setæäœã¯ãªããžã§ã¯ãã®äž¡æ¹ã®ãµã€ãºã匷å¶çã«å€æŽããŸãã 次ã®å¶éã[ããŒã¹]ã§ãéå ¬åŒã«ãèšå®ãããŠãããšãã説æãåžžã«ãããŸããã§ããïŒãå¹ ãé«ããåå¥ã«äœ¿çšããæ©èœãã CSquareã®å®è£ ã§ã¯ãéåãçºçãã䜿çšããå Žæã§ã¯ããã®ç¬ç«æ§ã®äœ¿çšã«åºã¥ããåçŽãªäžé£ã®ã¢ã¯ã·ã§ã³ãå®è¡ãããŸãr.SetWidth(r.GetWidth()*2); r.SetHeight(r.GetHeight()*2)
r.SetWidth(r.GetWidth()*2); r.SetHeight(r.GetHeight()*2)
-å®è£ ã®å ŽåãCSquareã¯CRectangleã§æ³å®ããã2åã§ã¯ãªããäž¡æ¹ã®ãµã€ãºã4åã«å¢ãããŸãã
ç§èŠãã®ååã¯ããã®ãããªéå ¬åŒã®å¶çŽã远跡ããããšã®å°é£ãã瀺ããŠãããéçºã¢ãããŒããåºæ¬å®è£ ããéåžžã«æçšãã€é »ç¹ã«äœ¿çšããã«ã¯ç¹å¥ãªæ³šæãå¿ èŠã§ãã
ã€ã³ã¿ãŒãã§ãŒã¹ã®åé¢ã®ã€ã³ã¿ãŒãã§ãŒã¹åé¢åçïŒISPïŒã®ååã äŸåé¢ä¿å転ã®åçïŒDIPïŒäŸåé¢ä¿å転ã®åç
ãããã®2ã€ã®ååã¯ãèŠä»¶ã®é åã§ã¯éåžžã«å¯æ¥ã§ãã äž¡æ¹ãšãã2ã€ã®[ã³ã³ããŒãã³ã]ïŒãã¯ã©ã€ã¢ã³ãããšããµãŒããŒãã®çžäºäœçšã®ããã®ããŒã«ãšããŠãå¯èœãªéãäœãåºæ¬ã€ã³ã¿ãŒãã§ãŒã¹ã䜿çšããããšã®æçšæ§ãæé»çã«æ瀺ããŸãã ãã®å Žåã[ã³ã³ããŒãã³ã]ã§äœ¿çšãããäžè¬æ å ±ã¯ãåºæ¬ã€ã³ã¿ãŒãã§ã€ã¹ã«éäžããŠããŸãã äžæ¹ã®[ã³ã³ããŒãã³ã]ïŒããµãŒããŒãïŒã¯åºæ¬ã€ã³ã¿ãŒãã§ãŒã¹ã®å®è£ ãå®è£ ããããäžæ¹ã®[ã³ã³ããŒãã³ã]ïŒãã¯ã©ã€ã¢ã³ããïŒã¯ãã®å®è£ ãåç §ããŸãã
ãããã®ååã®ç®æšã¯ãã³ã³ããŒãã³ãã®äŸåé¢ä¿ãæå°éã«æããåºç€ãšãªãã€ã³ã¿ãŒãã§ã€ã¹ãå€æŽããªãå Žåã«ã³ãŒãã®ç¬ç«ããå€æŽãèš±å¯ããããšã§ãã ã³ã³ããŒãã³ããSRPã®ååã®èŠä»¶ãæºãããŠããå Žåãã³ã³ããŒãã³ãå€æŽã®ç¬ç«æ§ã«ãããè€éããšåŽåã軜æžãããŸãã 以äžã®äžå€æ¡ä»¶ãéçºã«ååšãããããåæ§ã®ã¢ãããŒããå¯èœã§ãã
- åè¿°ã®[1]ã[2]ã[3]
- åã蟌ãŸããå[ã³ã³ããŒãã³ã]åäœã¯ããã®äœ¿çšã®å¶éã圢æããŸãã
- [ã³ã³ããŒãã³ã]ã®å䜿çšå Žæã§ã¯ããã®ãã¹ãŠã®å¶éãé¢ä¿ããå ŽåããããŸãã
- å®çŸ©ã®åºæ¬çãª[ã³ã³ããŒãã³ã]çµæã¯ã[ã³ã³ããŒãã³ã]å®è£ ãããè€éãããã³å¶éã®æ°ãå°ãªãã
- [ã³ã³ããŒãã³ã]ãå€æŽãããšããã®å¶éãå€æŽããã䜿çšãããã¹ãŠã®å Žæã®æ€èšŒãå¿ èŠã«ãªããŸããããã«ãããããã°ã©ããŒã®æéã®æ¶è²»ã
ããŒã¹[ã³ã³ããŒãã³ã]ã®äœ¿çšå Žæã¯ã[ã³ã³ããŒãã³ã]å®è£ ã«å€æŽãå ããåŸã«æ€èšŒãå¿ èŠãšããŸããã
æªäœ¿çšã®æ©èœãšå¶éãç Žæ£ããããšã«ãããããŒã¹ã€ã³ã¿ãŒãã§ãŒã¹ã®ããµã€ãºããæå°éã«æãã[ã³ã³ããŒãã³ã]ã®å®è£ ãïŒLSPïŒ
ISPã®ååã¯ãå ¬éãããæ©èœã®ãã¹ãŠããã®ãã¯ã©ã€ã¢ã³ããã«ãã£ãŠäœ¿çšãããŠããããã§ã¯ãªãå ŽåãããµãŒããŒãã®ã€ã³ã¿ãŒãã§ãŒã¹ã®åé¢ïŒåé¢ïŒã®å¿ èŠæ§ã匷調ããŠããŸãã ãã®å Žåãã¯ã©ã€ã¢ã³ããå¿ èŠãšãã[ããŒã¹]ã®ã¿ãå²ãåœãŠãããå ±åå¶éæ å ±ã®æå°åãä¿èšŒãããŸãã
ãããŠããã§ãDIPãç¶æããããã®äžé£ã®ãã¯ããã¯ãæžããããšæããŸãã ãããã®ããªãã¯ãæ¢ãããšãã§ããæçœãªé åããïŒ
- ã¯ã©ã¹ã®èª¬æããããªãã¯éšåãšãã©ã€ããŒãéšåïŒããã³OOPã®ä»ã®ååïŒã«åé¢ããŸãã
- éãããé¢æ°ãšãªããžã§ã¯ãèšè¿°åã®ã»ãããæã€åçã©ã€ãã©ãªãšã®çžäºäœçšã®èª¬æã
- ããã¯ã©ã€ãã©ãªã«ã¢ã¯ã»ã¹ããããã®ã€ã³ã¿ãŒãã§ã€ã¹ãšããŠãã¡ã€ã«ãã£ããããã䜿çšããŸãã
èŠåºãã«æ»ã£ãŠããç解ã§ããªãããéžæãããçç±ã説æããŸãã é·ãéèŠããã§éåžžã«æçšãªã«ãŒã«ã誀ã£ãŠåŒ·èª¿ããããã«ãåŠå®ãè¿œå ãããŸãã 誀解ããä¿¡é Œãããã¯ãããžãŒã®å¿çšã«ãªãœãŒã¹ãè²»ããããã®çµæãèªå·±æºè¶³ãšãã¡ãã·ã§ããã«ãªãã¯ãããžãŒãžã®é¢äžã«ã€ããŠèªæ ¢ããå¯èœæ§ãé€ããŠããã¯ãããžãŒãç解ããããããã£ãŠãã¯ãããžãŒã䜿çšããªãæ¹ãè¯ãã§ãã
ãæž èŽããããšãããããŸããã