Javaããã°ã©ãã³ã°è½åèªå®è©¦é¨èª²é¡ããã°ã©ã ã®ãªãã¡ã¯ã¿ãªã³ã°ã¬ãã¼ãï¼ãã®ï¼ï¼
æ¨æ¥æ¸ããSI業界(日本)のJavaプログラマーにはオブジェクト指向より忍耐力が求められている? - 達人プログラマーを目指してãäºæ³ä»¥ä¸ã«å¤§ããªåé¿ãããé©ãã¦ãã¾ããç¹ã«ããã®æåãªã²ãããã«ãSIæ¥çã®ç¾ç¶ã¨æªæ¥ã«é¢ãã¦ã³ã¡ã³ãããã¦ããã ãã¾ãããï¼SI業界からはさっさと抜けだしたほうがいいï¼
ãã ããSIæ¥çã®ä»å¾ãã©ããã¨ãããã¨ãæ°ãããµã¼ãã¹ã使ã£ããã¸ãã¹ã®ãã¨ã«ã¤ãã¦ã¯ãç§èªèº«æå
端æè¡ã«ååã«ãã£ããã¢ããã§ãã¦ããããèªåã®èããæ´çã§ãã¦ããªããããä¸æ¦èããªããã¨ã«ãã¦ãããã§ã¯ä¾ã®è©¦é¨åé¡ã®è¨è¨ã¨ãªãã¡ã¯ã¿ãªã³ã°ã«ã¤ãã¦èå¯ãã¦ã¿ããã¨æãã¾ããå
·ä½çãªä¾ã«åºã¥ãã¦èª¬æãããã¨ã§ããªãã¸ã§ã¯ãæåãSIæ¥çã®å¤ãã®æ¹ã
ã«èãããã¦ããã»ã©ç解ä¸è½ãªãã®ãªã®ã§ã¯ãªããåé¡ãåç´åããããã°ã©ã ã®ä¿å®æ§ãæ¡éãã«åä¸ãããããã§ãããã¦éè¦ãªå½¹å²ãæããã¨ãããã¨ããç解ããã ãããã®ã¨ä¿¡ãã¾ããå®éããã¯ãã®ã³ã¡ã³ãã®ä¸ã§ã
SEãPGãå¤ãã¯ä¼ç¤¾å¡ã§ãã£ã¦å°é家ã§ã¯ãªããããç®å ã®ã³ã¹ããèããã¨ä»æ¹ããªãæ°ããããOOPçãªè¨è¨ãã³ã¼ãã£ã³ã°ãããå ´åãå¾ã®ã¡ã³ããã³ã¹ãä»»ãããã¨ãã§ããªãæ²æ¨ãªç¶æ³ã ããããã
ã¨ããã³ã¡ã³ããããã ãã¦ããã®ã§ãããæ£ãããªãã¸ã§ã¯ãæåã®ã¢ã¼ããã¯ãã£ã¼ãæ¡ç¨ããè¨è¨ãåãå
¥ãããã¨ã§ãå¾æ¥ã®ã¹ãã²ããã£ã³ã¼ããä¿å®ãããã¨ã«æ¯ã¹ã¦ä¿å®æ§ããã£ã¨é«ãã§ããã®ã§ãããã¾ãããªãã¸ã§ã¯ãæåï¼ã¨ãã人ãå¤ãã¨æãã¾ãããç§ã¨ãã¦ã¯ãªãã¸ã§ã¯ãæåæè¡ã®é
åãä¼ãããã¨ã§ãå¤ãã®æ¹ã
ã®èª¤è§£ã解ããã¨ãã§ããã°ã¨æãã¾ãã
ãã¡ããããªãã¸ã§ã¯ãæåãå¦ã¶ã«ã¯ããããã«ã以åã®ããæ¹ããããã£ã¨å¤ãã®ä½ç³»çãªç¥èãå¿
è¦ã§ãããæ·å±
ãé«ããã¨ã¯å¦å®ã§ãã¾ããããããããã±ãããCPUãè¨è¨ãããããªå¤©æã¨ã³ã¸ãã¢ã§ãªãã¦ãããã¡ãã¨åå¼·ããã°å°ãªãã¨ãåºæ¬çãªèãæ¹ã¯å¤ãã®äººã
ãå¦ã¶ãã¨ãã§ãããã®ã§ããã¨ä¿¡ãã¾ãã
ãã ãããã®ç¹ã«ã¤ãã¦ã¯ãã¾ãã¯ã£ããã¨ã¯è¨ãããªãããã§ãããç§ã®èãã§ã¯ããªãã¸ã§ã¯ãæåãçã«ç解ãã使ãããªããã¨ãã§ããã®ã¯ãªãã¸ã§ã¯ãæåè¨èªã«ç¿çããããã°ã©ãã¼ã ãã§ã¯ãªãã®ãã¨ãããã¨ãããã¾ãã確ãã«ãUMLãªã©ã®è¨æ³ã使ã£ãä¸æµã®åæãè¨è¨ã®ææ³ãããã¾ãããå°ãªãã¨ã1ã¤ã®ãªãã¸ã§ã¯ãæåããã°ã©ãã³ã°ã«ç¿çããããã°ã©ãã¼ã¨ãã¦ã®çµé¨ããªãã¨ããªãã¸ã§ã¯ãæåã®ã¨ãã»ã³ã¹ãæ¬å½ã«ç解ãããã¨ã¯ãããã¦é£ããããããã¯ãã»ã¨ãã©ä¸å¯è½ãªãã¨ã®ããã«æãã®ã§ãããã¡ãããªãã¸ã§ã¯ãæåè¨èªã§ããã°ã©ãã³ã°ãã§ããã°ãªãã¸ã§ã¯ãæåãç解ã§ããã¨ãããã¨ã§ã¯ãªãã§ãããå°ãªãã¨ããªãã¸ã§ã¯ãæåããã°ã©ãã³ã°ï¼OOPï¼ã®ç¿çã¯ãªãã¸ã§ã¯ãæåã®ç解ã«æ¬ ãããã¨ã®ã§ããªãå¿
è¦æ¡ä»¶ã ã¨æãã¾ãããªãã¸ã§ã¯ãæåã®è©±ãããç解ã§ããªãã¨ããæ¹ã¯ãã»ã¼ä¾å¤ãªããããã°ã©ãã³ã°ãããªãä¸æµå°éã®æ¹ãªã®ã§ã¯ãªããã¨æãã¾ãããã¨ã話ã¨ãã¦ä½åãªãã¸ã§ã¯ãæåã®è§£èª¬æ¸ãèªãã§ããããã°ã©ãã³ã°ã®çµé¨ãªãã§ã¯ããªã¢ã¼ãã£ãºã ãæ½è±¡åãã«ãã»ã«åã¨ãã£ããã¨ã®ãã¥ã¢ã³ã¹ãä½å¾ãããã¨ã¯ã»ã¼ä¸å¯è½ã«è¿ãã¨æãã¾ããããã¯ã¡ããã©ãä¸è¬ç¸å¯¾æ§çè«ãéååå¦ã®åé¡ãç解ããã®ã«æ°å¼ã使ããªãä¸è¬åãã®åèæ¸ãä½åèªãã ã¨ããã§çµå±æ£ããç解ã«å°éã§ããªãã®ã¨åããã¨ã§ããã§ãããããããç¾ä»£çãªãªãã¸ã§ã¯ãæåéçºã§ã¯ããè¨ãããããã«è¨è¨è
ã¯ããã°ã©ãã¼ã§ããã¨ãããã¨ãæã¾ããã¨ãããã¨ã«ãªãã®ã§ããCOBOLæ代ã®çºæ³ã§ããã°ã©ã ãä½ãPGã¯ä¸ç´è·ã§ãããè¨è¨ã¯ä¸æµã®SEãè¡ãã¨ããå¤ãããæ çµã¿ããé¢ããããªãã®ã§ããã°ãæ£ããè¨è¨ãªã©ã§ããããããªãã®ã§ã¯ãªãã§ããããï¼
ã§ãããã
- è¨è¨ã«ããããä¸ç´è·ã®æ¹ãçå£ã«ãªãã¸ã§ã¯ãæåããã°ã©ãã³ã°ã®åå¼·ããã
- ããã°ã©ãã³ã°ããã人ãä¸ç´è·ã¨ã¿ãªããæ£ããè©ä¾¡ããè¨è¨è ã¨ãã¦æè¦ãã¢ã¤ãã¢ãå°éãã
ã®ãããããå¿ããã¦ããã ããã°ããã£ã¨ãã¾ãããã¯ãã§ããã¨æãã¾ãã
COBOLæ代ã®è¨è¨ã¨éããç¾ä»£çãªãªãã¸ã§ã¯ãæåè¨è¨ã§ã¯åºæ¬ã¢ã¼ããã¯ãã£ã¼ã®è¨è¨ã大åã§ã
ãã®è©¦é¨åé¡ã®ãªãªã¸ãã«ã®è¨è¨æ¸ã¯20年以ä¸åã®COBOLæ代ã®è¨è¨ææ³ã«åºã¥ãã¦è¨è¿°ããã¦ãã¾ãããã®æ代ã«ã¯Javaã®ãããªãªãã¸ã§ã¯ãæåè¨èªã¯ã¾ã ä¸è¬çã§ã¯ããã¾ããã§ããããã®æ代ã®è¨è¨ã¯æ§é åè¨è¨ã§ãã®ã§ã
- å¦çã®æµããããã¼ãã£ã¼ãã¨ãã¦è¨è¨ãã
- æ©è½åä½ã§ããã°ã©ã ããµãããã°ã©ã ã«åå²ãã
- ãã¼ã¿æ§é ï¼ãã¡ã¤ã«ãã©ã¼ãããï¼ãè¨è¨ãã
- ç»é¢ã¬ã¤ã¢ã¦ããæ¸å¼ãè¨è¨ãã
ã¨ãããã¨ãã»ã¨ãã©ã§ãããå®éã試é¨åé¡ã®è¨è¨æ¸ã«ã¯ãã¡ã½ããããã©ã¡ã¼ã¿ã¼ã®ä¸è¦§ãªã©ãã½ã¼ã¹ãJava Docãèªãã°ããããããªåé·ãªå 容ãé¤ãã¨ä¸ã®ããããã®å 容以ä¸ã®ãã®ã¯å«ã¾ãã¦ãã¾ããã§ããããã®ãããªè¨è¨ã§ãã£ã¦ã
- ä¸åº¦ä½æã»è©¦é¨ãå®äºããã決ãã¦æ©è½å¤æ´ã追å ããªã
- ãã¡ã¤ã«ã®ã½ã¼ãããã¼ã¸ãæ¸ãæããªã©å®åå¦çããããªã
- é常ã«é·ãæéã¨ãéãããã¦éçºãã
ãªã©æãªããã®ããã°ã©ã ã§ããã°ãä½ã¨ããªã£ãããããã¾ãããããããªãããç¾ä»£ã®ã³ã³ãã¥ã¼ã¿ã¼ã¯å½æã¨ã¯é度ãè¨æ¶å®¹éã¯æåéãæ¡éãã«é²æ©ãã¦ãã¾ããã顧客ããã以åããã¯ããã«è¤éãªè¦ä»¶ãçæéã«éçºãããã¨ãè¦æ±ãããããã«ãªã£ã¦ãã¦ãã¾ãããããã£ã¦ãããã°ã©ãã³ã°è¨èªãããå¹ççã«éçºãä¿å®ãã§ãããã®ãç»å ´ãã¦ãã¦ããããã§ãããè¨è¨ãããã°ã©ãã³ã°ã®ããæ¹ãå½ç¶æã¨ã¯éã£ãæ¹æ³ã§è¡ãå¿
è¦ãããã¾ãããªãã¸ã§ã¯ãæåè¨èªãç¾å¨ä¸»æµã«ãªã£ã¦ããã®ã¯ãããããèæ¯ãèããã°å¿
ç¶ã®ãã¨ã§ãããç¡è¦ãç¶ãããã¨ã¯ã§ããªãã®ã§ãã
ãããããJavaã使ã£ãéçºã§ã¯ä¸è¨ã®è¨è¨ã«å ãã¦ãªãã¸ã§ã¯ãæåçãªã¢ã¼ããã¯ãã£ã¼ããã¡ãã¨è¨è¨ãããã¨ãé常ã«å¤§åã«ãªãã¾ããã¢ã¼ããã¯ãã£ã¼ã®è¨è¨ã§ã¯ã¾ã以ä¸ã®ãã¨ãèããå¿
è¦ãããã¾ãã
- æ½è±¡åº¦ï¼æ©è½åºæâå ±éæ©è½ï¼ããã¼ã¿ã¨ã®è·é¢ï¼ç»é¢ããâDBããï¼ãªã©ã®åºæºã«ããã¬ã¤ã¤ã¼ã«åå²ããã
- ã¬ã¤ã¤ã¼éã®ä¾åé¢ä¿ããããããã®ã¬ã¤ã¤ã¼ã®ä¸ã§å®è£ ãã¹ããµã¼ãã¹ã®å 容ã決ããã
- å ¨ä½çãªæ©è½ãã¢ã¸ã¥ã¼ã«ãã¨ã«åå²ããã
ä»åã®ä¾é¡ã ã¨ã以ä¸ã®UMLã®ãããªã¢ãã«ãèãããã¾ãã
ã¢ããªã±ã¼ã·ã§ã³å±¤ã§ã¯ã¡ãã¥ã¼ã®é¸æã«å¯¾å¿ããåã
ã®ã¦ã¼ã¹ã±ã¼ã¹åºæã®å¦çãè¨è¿°ããã³ã³ããã¼ã©ã¼ãã¸ãã¯ã主ã«æ ¼ç´ããã¾ããä¸æ¹ããã¡ã¤ã³å±¤ã«ã¯ä¸ä½ã®ã¬ã¤ã¤ã¼ããåç
§ãããã¨ã³ãã£ãã£ã¯ã©ã¹ãªã©ãæ ¼ç´ãããã¤ã³ãã©å±¤ã«ã¯ãã¡ã¤ã«ã¢ã¯ã»ã¹ãç»é¢æä½ãªã©æ±ç¨çãªãã¸ãã¯ãæ½è±¡åãã¦æ±ããããããã¯ã©ã¹ã©ã¤ãã©ãªã¼ãæ ¼ç´ããã¾ãã
ãªããJavaè¨èªã§ã¯ããããã¢ã¸ã¥ã¼ã«ã®æ§é ã¯ããã±ã¼ã¸ã¨ããè¨èªæ§é ã§è¡¨ç¾ãããã¨ãã§ãã¾ãããã¨ãã¨ã®è©¦é¨ã®ãªãªã¸ãã«ã®ã½ã¼ã¹ã§ã¯å
¨ã¯ã©ã¹ãããã©ã«ãããã±ã¼ã¸ã«æ ¼ç´ããã¦ãã¾ããããããããã®ãããªåºæ¬ã¨ãªãã¢ã¼ããã¯ãã£ã¼ã決ããå ´åã¯ãã¢ã¸ã¥ã¼ã«ã«å¯¾å¿ãã¦ã½ã¼ã¹ã³ã¼ãä¸ã®ããã±ã¼ã¸ãä½æãã¾ããå®éã«ä»¥ä¸ã®ãããªã¤ã¡ã¼ã¸ã¨ãªãã¾ãã
ãã®ããã«ãIDEãªã©ã®ç¾ä»£çãªéçºç°å¢ã§ã¯ãããã±ã¼ã¸ã®æ§é ããããã«é層åãã¦è¡¨ç¤ºããã¾ãããã¯ã©ã¹ã®æ ¼ç´å ´æã®ç§»åãèªåãªãã¡ã¯ã¿ãªã³ã°æ©è½ã使ã£ã¦ã¹ã¤ã¹ã¤å®è¡ã§ãã¾ããæã®ã³ã¼ãã£ã³ã°ã®ã¤ã¡ã¼ã¸ã ã¨ããã°ã©ã ã¯æåã®ç¾
åã¨ããã¤ã¡ã¼ã¸ãå¼·ãã¨æãã¾ãããå®éã¯ããã§ã¯ãªãã¦ãããªããã¸ã¥ã¢ã«ãªå³è³ã®æè¦ãè¦æ±ãããä½æ¥ã§ãããã¨ããåããããã ããã¨æãã¾ããã¾ããããã±ã¼ã¸ã®ã©ãã«ã©ã®ãããªã¯ã©ã¹ãé
ç½®ãããã¨ãããã¨ã¯ãUMLä¸ããã½ã¼ã¹ã³ã¼ãä¸ã®æ¹ãããããããå¤æ´ã容æã¨ãããã¨ãããã¾ããã¡ããã©æ°å¦è
ãæ°å¼ãå±éããããã¦æ¹ç¨å¼ã解ããããCADã®å³é¢ã使ã£ã¦èªåè»ã®é¨åãè¨è¨ãããããã®ã¨åããããªæè¦ã§ãã½ã¼ã¹ã³ã¼ãä¸ã§è¨è¨ãèªç±èªå¨ã«å¤ãã¦ããã¨ãããã¨ãã§ããã®ã§ããæåã«Excelæ¹ç¼ç´ã使ã£ã¦ä½å³ãã¦ããã½ã¼ã¹ãèµ·ããã¨ãããã¨ãããã«éå¹çã§ç¡æå³ãªãã¨ãããåããããã ããã®ã§ã¯ãªãã§ãããããï¼å®éãã®è¨äºã§ç´¹ä»ãã¦ããUMLå³ã¯ã½ã¼ã¹ããªãã¡ã¯ã¿ãªã³ã°ãã¦ãããªãã¼ã¹ãã¦èµ·ããããã®ã§ããï¼
å ±éçã«ä½¿ããããªãã¸ãã¯ãå ±éã¯ã©ã¹ã¨ãã¦æ½è±¡åãåå©ç¨ãã
ãªãã¸ã§ã¯ãæåã®è¨è¨ã«ããã¦ã¯ãã¯ã©ã¹ã¨ããæ½è±¡ãé©åã«èãåºããã¨ãè¨è¨ã®ä¸å¿ã¨ãªãã¾ããã¯ã©ã¹ã¯å
¥éæ¸ã ã¨ããããããã®é
æ
®ãããä¹ãç©ããèªåè»ããªã©ææ¡ããããå®ä¸çã®ã¨ã³ãã£ãã£ãã¯ã©ã¹ã®ä¾ã¨ãã¦æ±ããã¨ãå¤ãã§ãããå®éã®ãªãã¸ã§ã¯ãæåããã°ã©ãã³ã°ã«ããã¦ã¯ãããããå®ä¸çã®ãã®ä»¥å¤ã«ãæ©è½ããã¼ã¿ã®å¡ã¨ãã¦ä¾¿å©ãªåä½ãæ½è±¡åãã¦æ½åºãããã¨ãå¤ãããã¾ããå®éããã®ä¾é¡ã®å ´åãåãªããã¼ã¿ã®CRUDå¦çãªã®ã§ãæ´¾é£ãã¸ãã¹ã¨ããæ¥åãã¡ã¤ã³ã«ç¹åããè¤éæ§ã¨ããã®ã¯ã¾ã£ããåå¨ãã¾ãããã ããããã®é¨åã«ã¤ãã¦ã¯ãæ¬å½ã®æå³ã§ã®ã¯ã©ã¹ãæ½åºããå¿
è¦ã¯ããã¾ãããåã«ãã¡ã¤ã«ã®ä¸è¡åã®ãã¼ã¿ãä¿æããæ§é ä½ã¨ãã¦ã¯ã©ã¹ãæ½åºãã¦ããã°ååã§ããï¼ãã ã試é¨ã§ãªãå®éã®æ¥åã§ã¯ãããã£ãã³ã¢ãã¡ã¤ã³ã®é åã¯ä»æ§å¤æ´ãæ©è½è¿½å ã®å¯è½æ§ãé«ãé¨åãªã®ã§å°æ¥çã«ã¯ã¯ã©ã¹ãæ½åºããããã«ãªãã¡ã¯ã¿ãªã³ã°ããæå³ãé«ãã®ã§ãããï¼
ãã®åé¡ã«ããã¦ãããããæãã³ã¼ãã£ã³ã°ãé¢åãªã®ã¯
- ã¿ãåºåãã®ããã¹ããã¡ã¤ã«ãèªã¿æ¸ãããå¦ç
- ç»é¢ã³ã³ã½ã¼ã«ã«ã¡ãã¥ã¼ã表示ããããå ¥åã対話çã«åãåã£ããããå¦ç
ãªã©ã®é¨åã«ãªãã¾ãããããã®å¦çã«ã¯åã«ãã¸ãã¯ã ãã§ãªãã¦ãå¦çãéè¡ããã®ã«å¿ è¦ãªå¤æ°ãå¿ è¦ã«ãªãã¾ãããããã£ã¦ãããããé¨åããããJavaè¨èªã§è¨ãã¨ããã®æ¬å½ã®ã¯ã©ã¹ã¨ãã¦æ½åºãã価å¤ãããé¨åã«ãªãã¾ãããªããè¨è¨ã®åæã®æ®µéã§ã¯ã¯ã©ã¹ã®å®è£ ã®ä¸èº«ã§ã¯ãªããã¾ãã¯å¤é¨ããã¢ã¯ã»ã¹å¯è½ãªï¼publicãªï¼ã¤ã³ã¿ã¼ãã§ã¼ã¹ã«çç®ãã¦è¨è¨ãããã¨ãå¤ãã§ãã*1ãããã£ã¦ã以ä¸ã§ããããæ½åºããæ¦å¿µã«ã¤ã³ã¿ã¼ãã§ã¼ã¹åãä¸ãããã®ã¤ã³ã¿ã¼ãã§ã¼ã¹ã«å¯¾ãã¦å¼ã³åºãå¯è½ãªæä½ãå®ç¾©ãã¾ãããã¨ãã°ãä¸è¨ã®ãã¿ãåºåãã®ããã¹ããã¡ã¤ã«ãèªã¿æ¸ãããå¦çãã«ã¯Repositoryã¨ããååãä¸ãã以ä¸ã®ãããªJavaã®ã¤ã³ã¿ã¼ãã§ã¼ã¹ã¨ãã¦å®ç¾©ãã¾ãã*2
package sample.common.io; import java.util.List; import sample.common.entity.EntityBase; /** * ã¨ã³ãã£ãã£ãæ°¸ç¶åããããã®ã¬ãã¸ããªã¼ã¯ã©ã¹ãå®è£ ãã¹ãã¤ã³ã¿ã¼ãã§ã¼ã¹ã§ãã * * @param <E> ã¨ã³ãã£ãã£ã«å¯¾ããç·ç§°åãã©ã¡ã¼ã¿ã¼ */ public interface Repository<E extends EntityBase> { /** * IDã§ã¨ã³ãã£ãã£ãæ¤ç´¢ãããï¼è«çåé¤æ¸ã¿ã®ã¨ã³ãã£ãã£ã¯é¤å¤ãããï¼ * ã¨ã³ãã£ãã£ãè¦ã¤ãããªãå ´åã¯EntityNotFoundExceptionãéåºãããã * * @param id ID * @return æ¤ç´¢çµæã®ã¨ã³ãã£ã㣠* @throws EntityNotFoundException ã¨ã³ãã£ãã£ãè¦ã¤ãããªãå ´å */ E findById(long id); /** * ã¨ã³ãã£ãã£ãå ¨ä»¶æ¤ç´¢ãããï¼è«çåé¤æ¸ã¿ã®ã¨ã³ãã£ãã£ã¯é¤å¤ãããï¼ * ã¨ã³ãã£ãã£ãè¦ã¤ãããªãå ´åã¯ç©ºã®Listãè¿ãã * * @return ã¨ã³ãã£ãã£ã®ãªã¹ã * @throws EntityNotFoundException ã¨ã³ãã£ãã£ãè¦ã¤ãããªãå ´å */ List<E> findAll(); /** * ã¨ã³ãã£ãã£ã®å±æ§å¤ãç §åãã¦æ¤ç´¢ãã * @param example æ¤ç´¢å¯¾è±¡ã®å±æ§å¤ãæ ¼ç´ãããªãã¸ã§ã¯ã * @return æ¤ç´¢çµæã«ä¸è´ããã¨ã³ãã£ãã£ã®ãªã¹ã */ List<E> findByExample(E example); /** * ã¨ã³ãã£ãã£ãæ°è¦ä½æããã * @param data ä½æ対象ã®ã¨ã³ãã£ã㣠*/ void create(E data); /** * ã¨ã³ãã£ãã£ãæ´æ°ããã * @param data æ´æ°å¯¾è±¡ã®ã¨ã³ãã£ã㣠*/ void update(E data); /** * ã¨ã³ãã£ãã£ãè«çåé¤ããã * @param data è«çåé¤å¯¾è±¡ã®ã¨ã³ãã£ã㣠*/ void delete(long id); }
ãã®ã¤ã³ã¿ã¼ãã§ã¼ã¹ã¯ç·ç§°åã®ãã©ã¡ã¼ã¿ã¼ä»ãã§å®ç¾©ããã¦ãããããä»»æã®ã¨ã³ãã£ãã£ã¯ã©ã¹ã«å¯¾ãã¦æ±ç¨çã«å©ç¨ãããã¨ãã§ãã¾ãããããã£ã¦ãä¸åº¦æ£ããå®è£
ãã¦ãã¾ãã¨ãHumanResouceï¼äººæï¼ã ããããWorkï¼ç¨¼åï¼ã ããããã©ã®ã¯ã©ã¹ã«å¯¾ãã¦ãåå©ç¨ã§ãã¾ãããããããã¡ã¤ã«ãèªã¿æ¸ããããã¸ãã¯ã¯ãã®ã¤ã³ã¿ã¼ãã§ã¼ã¹ã®ä¸èº«ã«ã«ãã»ã«åããã¦ãã¾ãã®ã§ãå©ç¨å´ã¯ã¾ã£ããæèããå¿
è¦ãããã¾ããã
ãã¨ãã°ã人æãIDã§æ¤ç´¢ããå¦çã¯ä»¥ä¸ã®ããã«é常ã«ç°¡åã«è¨è¿°ã§ããããã«ãªãã¾ãã
HumanResource hr = hrRepository.findById(id);
åæ§ã«ã稼åã«ã¤ãã¦ã
Work work = workRepository.findById(id);
ã¨ã»ã¨ãã©åãä¸è¡ã§æ¸ãã§ãã¾ãã¾ããè±èªãªã®ã§æ¥æ¬äººã«ã¯ã¡ãã£ã¨èªã¿ã«ããã®ã§ãããè±èªè³ã§èªãã°ããHRã®ã¬ãã¸ããªã¼ããIDã§æ¤ç´¢ããHRæ å ±ãä¿æããããã¨ãããã¨ã§ãããä¸æµã®ä»æ§æ¸ã®è¨è¿°ã¬ãã«ã¨å¤ããã¨ãããããã¾ãããï¼多くの日本人がオブジェクト指向プログラミングを苦手とするのは英語アレルギーだからか?ï¼ããã以ä¸ã«å¼ç¨ããããã¨ãã¨ã®ãªãªã¸ãã«ã®ã½ã¼ã¹ã¨æ¯ã¹ã¦ã¿ãã°ãã©ã¡ããåç´ã§ãããä¸ç®çç¶ã§ããï¼ãã®å ´åãåã ã®ã¨ã³ãã£ãã£ãã¨ã«ä¼¼ããããªãã¸ãã¯ãä½ãç´ãå¿ è¦ãããã¾ããï¼
ï¼Javaããã°ã©ãã³ã°è½åèªå®è©¦é¨1ç´åé¡ããå¼ç¨ï¼ /** æå®ããã人æIDã®ãã¹ã¦ã®æ å ±ã®èªè¾¼ã¿ * @return 人ææ å ± */ String[][] getjData() { BufferedReader br = null; try { br = new BufferedReader( new FileReader( "jinzai.txt" ) ); //人ææ å ±ãã¹ã¿ãéã String instr; //人ææ å ±ãã¹ã¿ãã1ã¬ã³ã¼ããã¤èªè¾¼ã¿ while( (instr = br.readLine()) != null ) { if( instr.substring( 0, instr.indexOf('\t') ).equals(jID) ) { //人æIDãä¸è´ if( (instr.length()-1) == instr.lastIndexOf('\t') ) { //åé¤æ¥ä»ãªã return setjData( instr ); //人ææ å ±ãè¿ã } } } } catch( FileNotFoundException e ) { //人æãã¹ã¿ããªã } catch( IOException e ) { //人ææ å ±ãã¹ã¿ã¸ã®ã¢ã¯ã»ã¹ã¨ã©ã¼ } finally { try { if ( br != null) br.close(); //人ææ å ±ãã¹ã¿ãéãã } catch( IOException e) { //æ£å¸¸ã«ã¯ãã¼ãºã§ããªãã£ãå ´åã®ã¨ã©ã¼ } } return null; }
ã«ãã»ã«åã«ããã¡ãªããã¯åã«ã½ã¼ã¹ã³ã¼ãã®è¡æ°ãå
±éåã«ããå°ãªããªãã¨ãããã¨ã ãã§ã¯ããã¾ãããã«ãã»ã«åã«ãã£ã¦ãä»å¾ãã¡ã¤ã«ã®é
ç®ãå¤æ´ããããããã©ã¼ããããå¤æ´ããã¦ãããã¡ãã¡ã®ã³ã¼ããä¿®æ£ããå¿
è¦ããªããªããããä¿å®æ§ãå®éã«ããã¨åä¸ããã®ã§ãã
ã¡ãªã¿ã«ãRepositoryã¯ã¤ã³ã¿ã¼ãã§ã¼ã¹ã§ãã®ã§ãå¥éå®è£
ã¯ã©ã¹ãå¿
è¦ã«ãªãã¾ããã¯ã©ã¹å³ã§é¢ä¿ã示ãã¨ä»¥ä¸ã®ããã«ãªãã¾ãã
ãªããåèã¾ã§ã«ãRepositoryã¤ã³ã¿ãã§ã¼ã¹ã®å®è£
ä¾ã以ä¸ã®è¨è¿°ãã¾ãããã®ã¯ã©ã¹ãä¸ã¤å®è£
ãåä½ãã¹ããã¦ããã°ãããããã¨ã³ãã£ãã£ã«å¯¾ããCRUDå¦çã§æ±ç¨çã«åå©ç¨ã§ããããã«ãªãã¾ãã
package sample.common.io; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.List; import org.apache.commons.lang.StringUtils; import sample.common.SystemException; import sample.common.entity.EntityBase; import sample.common.util.ExampleMatcher; import sample.common.util.IdMatcher; import sample.common.util.Matcher; import sample.common.util.TrueMatcher; /** * åºåãæåã使ã£ãããã¹ããã¡ã¤ã«ä¸ã®ãã¼ã¿ãèªã¿æ¸ãããããã®ã¬ãã¸ããªã¼å®è£ ã¯ã©ã¹ã§ãã * ãã®ã¯ã©ã¹ã¯ã¹ãã¼ããæã¡ã¹ã¬ããã»ã¼ãã§ã¯ãªãç¹ã«æ³¨æãããã¨ã */ public class CharSeparatedFileRepository<E extends EntityBase> implements Repository<E> { // ==================================================== // ãã£ã¼ã«ã // ==================================================== private File masterFile; private File workFile; private String separator = "\t"; private BufferedReader reader; private BufferedWriter writer; private Class<E> entityClass; private final TrueMatcher<E> TRUE_MATCHER = TrueMatcher.instance(); // ==================================================== // ãããã㣠// ==================================================== public File getMasterFile() { return masterFile; } public void setMasterFile(File masterFile) { this.masterFile = masterFile; } public File getWorkFile() { return workFile; } public void setWorkFile(File workFile) { this.workFile = workFile; } public Class<E> getEntityClass() { return entityClass; } public void setEntityClass(Class<E> entityClass) { this.entityClass = entityClass; } public String getSeparator() { return separator; } public void setSeparator(String separator) { this.separator = separator; } // ==================================================== // ã¡ã½ãã // ==================================================== private List<E> doFind(Matcher<E> matcher) { try { List<E> result = new ArrayList<E>(); openForRead(); String line; // ãã¹ã¿ãã1è¡ãã¤èªè¾¼ã¿ while ((line = reader.readLine()) != null) { E entity = toEntity(line); if (!entity.isLogicalDeleted() && matcher.isMatch(entity)) { result.add(entity); } } return result; } catch (IOException e) { throw new SystemException("æ¤ç´¢å¦çå®è¡æã«IOä¾å¤ãçºçãã¾ããã", e); } finally { close(); } } @Override public E findById(long id) { Matcher<E> idMatcher = new IdMatcher<E>(id); List<E> result = doFind(idMatcher); if (result.isEmpty()) { throw new EntityNotFoundException("id = " + id + "ã®ã¨ã³ãã£ãã£ã¯åå¨ãã¾ããã"); } // TODO ä¸ææ§ãã§ãã¯ã¯ãã¦ããªã return result.get(0); } @Override public List<E> findAll() { return doFind(TRUE_MATCHER); } @Override public List<E> findByExample(E example) { Matcher<E> exampleMatcher = new ExampleMatcher<E>(example); return doFind(exampleMatcher); } static interface FileUpdator { void handle() throws IOException; } private void processUpdate(FileUpdator fileUpdator) { try { openForWrite(); fileUpdator.handle(); } catch (IOException e) { throw new SystemException("åé¤å¦çå®è¡æã«IOä¾å¤ãçºçãã¾ããã", e); } finally { close(); } commit(); } private void writeEntity(E data) throws IOException { String outputLine = fromEntity(data); writer.write(outputLine); writer.newLine(); } @Override public void create(final E data) { if (data == null) throw new IllegalArgumentException("ãã©ã¡ã¼ã¿ã¼ãä¸æ£ã§ãã"); processUpdate(new FileUpdator() { @Override public void handle() throws IOException { String line; List<Long> idList = new ArrayList<Long>(); // ãã¹ã¿ãã1è¡ãã¤èªè¾¼ã¿ while ((line = reader.readLine()) != null) { E entity = toEntity(line); idList.add(entity.getId()); writeEntity(entity); } long maxId = Collections.max(idList); data.setId(maxId + 1); data.preCreate(); // æ´æ°ãä½ææ¥ä»ã®çºè¡ writeEntity(data); } }); } @Override public void update(final E data) { if (data == null) throw new IllegalArgumentException("ãã©ã¡ã¼ã¿ã¼ãä¸æ£ã§ãã"); if (!data.isPersisted()) throw new IllegalArgumentException("ãã©ã¡ã¼ã¿ã¼ãæ°¸ç¶åããã¦ãã¾ããã"); processUpdate(new FileUpdator() { @Override public void handle() throws IOException { String line; // ãã¹ã¿ãã1è¡ãã¤èªè¾¼ã¿ while ((line = reader.readLine()) != null) { E entity = toEntity(line); if (data.getId().equals(entity.getId())) { if (entity.isLogicalDeleted()) { // æ¢ã«è«çåé¤æ¸ã¿ã®å ´å throw new EntityNotFoundException("id = " + entity.getId() + "ã®ã¨ã³ãã£ãã£ã¯æ¢ã«è«çåé¤ããã¦ãã¾ãã"); } data.preUpdate(); entity = data; } writeEntity(entity); } } }); } @Override public void delete(final long id) { processUpdate(new FileUpdator() { @Override public void handle() throws IOException { String line; boolean deleted = false; // ãã¹ã¿ãã1è¡ãã¤èªè¾¼ã¿ while ((line = reader.readLine()) != null) { E entity = toEntity(line); if (id == entity.getId()) { if (entity.isLogicalDeleted()) { // æ¢ã«è«çåé¤æ¸ã¿ã®å ´å throw new EntityNotFoundException("id = " + id + "ã®ã¨ã³ãã£ãã£ã¯æ¢ã«è«çåé¤ããã¦ãã¾ãã"); } entity.logicalDelete(); deleted = true; } writeEntity(entity); } if (!deleted) { // ãã©ã¡ã¼ã¿ã¼ã§æå®ãããã¨ã³ãã£ãã£ãåå¨ããªãã£ãå ´å throw new EntityNotFoundException("id = " + id + "ã®ã¨ã³ãã£ãã£ã¯åå¨ãã¾ããã"); } } }); } private String fromEntity(E entity) { return StringUtils.join(entity.toArray(), getSeparator()); } private E toEntity(String line) { try { E entity = entityClass.newInstance(); entity.fromArray(StringUtils.split(line, getSeparator())); return entity; } catch (InstantiationException e) { throw new SystemException("ã¨ã³ãã£ãã£ã®å¾©å æã«ä¾å¤ãçºçãã¾ããã", e); } catch (IllegalAccessException e) { throw new SystemException("ã¨ã³ãã£ãã£ã®å¾©å æã«ä¾å¤ãçºçãã¾ããã", e); } } private void commit() { try { if (!masterFile.delete()) { throw new IOException(); } // ãã³ãã©ãªã¼ãã¡ã¤ã«ããã¹ã¿ã«ç½®æã workFile.renameTo(masterFile); } catch (IOException e) { throw new SystemException("ã¯ã¼ã¯ãã¡ã¤ã«ã®å¤æ´ããã¹ã¿ã¼ãã¡ã¤ã«ã«åæ ã§ãã¾ããã", e); } } // NOTE // æ¬æ¥ã¯å ¨ãã¡ã¤ã«ã®å 容ãã¡ã¢ãªä¸ã«èªã¿è¾¼ãã§å¦çããã»ããç°¡åã ãã // ãªãªã¸ãã«ã®å®è£ ã極åæ®ããã¨ã«ããã private void openForWrite() throws IOException { reader = new BufferedReader(new FileReader(masterFile)); writer = new BufferedWriter(new FileWriter(workFile)); } private void openForRead() throws IOException { reader = new BufferedReader(new FileReader(masterFile)); } private void close() { if (reader != null) { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } if (writer != null) { try { writer.close(); } catch (IOException e) { e.printStackTrace(); } } } }
ï¼ãã®ï¼ã«ã¤ã¥ãï¼
*1:ããã¯ãã¹ããã¡ã¼ã¹ãï¼TDDï¼ã®ãããªææ³ã§ãæ¬è³ªçã«ã¯å¤ããã¾ãããå ã«ã¡ã½ããã®ã¤ã³ã¿ã¼ãã§ã¼ã¹ã決ãããã¹ãã¯ã©ã¹ãä½ã£ã¦ããã¯ã©ã¹ã®ä¸èº«ã®å®è£ ãå®æããã¾ãã
*2:é©åãªã¯ã©ã¹ã®æ½åºã¯ç¢ºãã«é·å¹´ã®çµé¨ããããå ã®åãè¦æ±ãããã¨ããã§ãããåå¿è ããã°ã©ãã¼ã«ã¯è·ãéãã¨ããã§ãããããããã¶ã¤ã³ãã¿ã¼ã³ãªã©ã®æ¬ãåå¼·ãããã¨ã§ãããç¨åº¦ã¯ã©ã¹æ½åºã®ãã³ããå¾ããã¨ãå¯è½ã§ãã