ããªãä¾åãæ³¨å ¥ããã®ããâããã ãè¦ãã¨ããµã¤ã±ã§ã¹ããªãã¥ã¢ã«ãªæ¬ãã¨èº«æ§ãã¾ããããã¡ãããããªå 容ã§ã¯ãªãããä¾åæ§ã®æ³¨å ¥ãã«ã¤ãã¦æ¸ãããæ¬ã«ãªãã¾ãããä¾åæ§ã®æ³¨å ¥ãã¨ã¯ãã¢ã¸ã¥ã¼ã«éã®çµå度ãä¸ããããã®ãã¶ã¤ã³ãã¿ã¼ã³ã§ãè±èªã§ã¯Dependency Injectionã¨ããããã®é æåãã¨ã£ã¦DIã¨ç¥ãããã¨ãããã¾ãããã®ç¹å¾´ã¯ãªãã¸ã§ã¯ãã®çæã¨å©ç¨ãåé¢ãããã¨ã
ãã¨ãã°ãååIDãä¸ããããããååã¬ãã¸ããªãããã®ååIDã«è©²å½ããååæ å ±ãåå¾ãããååãµã¼ãã¹ã¯ã©ã¹ãããã¨ãã¾ããããããã¤ã¼ãã«å®è£ ãã¦ã¿ãã¨ã以ä¸ã®ããã«ãªãã¾ãã
class ProductService: def get_product_by_product_id(product_id): product_repository = ProductRepository() for product in product_repository.get_all_products(): if product.product_id == product_id: return product return None
ããã¯ã¢ã¸ã¥ã¼ã«çµå度ãé«ãç¶æ
ã§ããã¨ãã°ãåä½ãã¹ããªã©ã§ProductRepository
ãã¢ãã¯åãããå ´åããã®ã³ã¼ãã§ã¯ãããç°¡åã«å®ç¾ãããã¨ã¯ã§ãã¾ãããããã§ãä¾åæ§ã®æ³¨å
¥ãæ¡ç¨ãã以ä¸ã®ããã«ãªãã¡ã¯ã¿ãªã³ã°ãã¾ãã
class ProductService: def __init__(self, product_service): self.product_service = product_service def get_product_by_product_id(product_id): for product in self.product_repository.get_all_products(): if product.product_id == product_id: return product return None
ãã¨ã¯ããã¹ã¦ã®ããã°ã©ã ã®ã¨ã³ããªãã¤ã³ãã§ãProductService
ã¯ã©ã¹ãåæåãã¾ãã
if __name__ == '__main__': product_repository = ProductRepository() product_service = Product_Service(product_repository)
ãããããã¨ã§ãã¢ã¸ã¥ã¼ã«ã®çµå度ãä¸ãããåä½ãã¹ãã«ã¢ãã¯ã使ããªã©ã容æã«ãªãã¾ãããã¾ããªãã¸ã§ã¯ãã®çæã¨å©ç¨ãåé¢ãããªãã¸ã§ã¯ãã®çæãä¸å åãããã¨ã§ããã¨ãã°ã·ã³ã°ã«ãã³ã«ãããããªãããªã©ããªãã¸ã§ã¯ãã®ã©ã¤ãã¿ã¤ã 管çããããããªã£ãããããã°ã©ã å ¨ä½ã®è¦éããè¯ããªã£ããã¨ããã¾ãã¾ãªã¡ãªããã享åãããã¨ãã§ãã¾ãã
ä¸è¨ã®ä¾ã¯ä¾åæ§ã®æ³¨å ¥ã®ä¸é¨ã示ããã«ããã¾ãããæ¬æ¸ã§ã¯ãããããä¾åæ§ã®æ³¨å ¥ã£ã¦ãªãã ã£ã?ãã¨ããã¨ããããå§ã¾ã£ã¦ããã®ã¡ãªããããã¡ãªãããå®è£ ã®ãã¯ããã¯ããã¢ã³ããã¿ã¼ã³ã¾ã§ããã¨ç´°ãã«è§£èª¬ãã¦ãã¾ããã¾ããå ±éçãªé¢å¿äºããã¾ãåãæ±ãããã®ã¢ã¹ãã¯ãå¿åããã°ã©ãã³ã°ããªãã¸ã§ã¯ãã®çæé¨åãèªååããDIã³ã³ããã¨ãã£ããä¾åæ§ã®æ³¨å ¥ã«é¢é£ãããããã¯ã«ã¤ãã¦ããããªãã®ç´å¹ ãããã¦ãã¾ããä¾åæ§ã®æ³¨å ¥ã«ã¤ãã¦ãããã¨ããããå 容ãä½ç³»çã«å¦ã¹ãã¨ãã£ã¦ééããªãã§ãããã
ã¡ãªã¿ã«æ¬æ¸ã®åéã§ãããB5å¤ã§656ãã¼ã¸ãããã¾ããããããåã¯å°ããæ¹ã§ãã¹ãã¼ããã©ã³ã ã¨èªãã®ãã¤ããããããããã ãã®åéãä¾åæ§ã®æ³¨å ¥ã«ã ãè²»ããã¦ããããã§ããããä¸èº«ã®ã¤ã¾ãå ·åã¯å端ã§ã¯ããã¾ãããéã«ããã¨ãåãªããã¶ã¤ã³ãã¿ã¼ã³ã®ã²ã¨ã¤ã«ã¤ãã¦ãããã ãæ¸ãã¦ããããã§ãããæå³ãµã¤ã±ã§ã¹ããªãã¥ã¢ã«ã¨ããããã(´ã»Ïã»`)
ãµã³ãã«ã³ã¼ãã¯C#ã§æ¸ããã¦ãã¾ãããC#ã.NETç¹æã®ç¥èã¯ããã»ã©æ±ããããªãã®ã§ããªãã¸ã§ã¯ãæåããã°ã©ãã³ã°ã¨Cç³»è¨èªã®çµé¨ãããã°ãã¾ã£ããåé¡ãªãã¨æãã¾ããç¾ã«èªåã¯Javaã¡ã¤ã³ã®SEã§ãC#ã¯ããã¼ããã°ã©ãã³ã°ä»¥ä¸ã®çµé¨ã¯ããã¾ããããããã§ãå°ãã¨ãããã¨ã¯ããã¾ããã§ããããã ããæå¾ã®3ç« ã第13ç« : DIã³ã³ãã: Autofacãã第14ç« : DIã³ã³ãã: Simple Injectorãã第15ç« : DIã³ã³ãã: MS.DIï¼Microsoft.Extensions.DependencyInjectionï¼ãã«ã¤ãã¦ã¯ã.NETã®ã©ã¤ãã©ãªã解説ãã¦ããã ããªã®ã§ãããã¯C#ã.NETã«é¢å¿ããªã人ã¯èªã¿é£ã°ãã¦ãããã¨æãã¾ãã
ã¨ããã§ã¯èªåã¯Javaã¡ã¤ã³ã®SEã¨æ¸ãã¾ããããJavaã®ä¸çã§ã¯Spring Frameworkã¨ããã¢ããªã±ã¼ã·ã§ã³ãã¬ã¼ã ã¯ã¼ã¯ãããã¡ã¯ãã¹ã¿ã³ãã¼ãã«ãªãã¤ã¤ããã¾ããå®ã¯ãã®Spring Frameworkã§ãããä¾åæ§ã®æ³¨å ¥ã®èãæ¹ããµãã ãã«åãå ¥ãããã¬ã¼ã ã¯ã¼ã¯ã«ãªã£ã¦ãã¾ããè¦ããã«Javaã§ãä»äºããã以ä¸ã¯ä¾åæ§ã®æ³¨å ¥ããã¯éããããªãå¯è½æ§ãé«ãããã§ããèªåã®ãã£ãªã¢ãä»å¾ã©ããªããã¯ä¸éæã§ãããå½é¢ã¯Javaãå©ç¨ããããªã®ã§ãJavaã¨é¢é£ãæ·±ãä¾åæ§ã®æ³¨å ¥ã«ã¤ãã¦ãã£ããåå¼·ã§ãããã¨ã¯ããã£ãããªã¨æãã¾ãã