クリーンアーキテクチャについて調べてみた
313 views
ユースケース(Use Case)の例: オンライン注文
オンライン注文というユースケースを考えてみましょう。これはユーザーが商品をオンラインで注文する場合のシナリオです。
class OnlineOrderUseCase:
def __init__(self, payment_service):
self.payment_service = payment_service
def place_order(self, user_id, items):
# 注文のバリデーションや在庫チェックなどの処理
total_amount = self.calculate_total_amount(items)
self.payment_service.process_payment(user_id, total_amount)
# 注文をデータベースに保存する処理
def calculate_total_amount(self, items):
# 商品の合計金額を計算する処理
pass
# 使用例
class PaymentService:
def process_payment(self, user_id, amount):
# 支払い処理を行う
pass
payment_service = PaymentService()
order_use_case = OnlineOrderUseCase(payment_service)
order_use_case.place_order(user_id=123, items=[item1, item2])
この例では、オンライン注文ユースケースがオーダー処理全体をカプセル化しており、商品のバリデーション、支払い処理、注文の保存などを行います。
サービス(Service)の例: 商品在庫管理
商品在庫管理というサービスを考えてみましょう。これはショップ内の商品在庫を管理するための役割を持ちます。
class InventoryManagementService:
def __init__(self, inventory_repository):
self.inventory_repository = inventory_repository
def update_stock(self, product_id, quantity):
# 商品在庫を更新する処理
pass
# 使用例
class InventoryRepository:
def update_stock(self, product_id, quantity):
# データベースで在庫情報を更新する処理
pass
inventory_repository = InventoryRepository()
inventory_management_service = InventoryManagementService(inventory_repository)
inventory_management_service.update_stock(product_id=456, quantity=10)
この例では、商品在庫管理サービスが在庫管理のビジネスロジックをカプセル化しており、商品在庫の更新などを行います。
こうした例を通じて、ユースケースとサービスが異なる概念であることがわかるかと思います。ユースケースは特定のシナリオや機能を、ユーザーやアクターの視点から抽象化している一方、サービスは特定の機能を提供するためのコンポーネントとして存在します。
Page 1 of 1.
すぺぺぺ
本サイトの作成者。
プログラムは趣味と勉強を兼ねて、のんびり本サイトを作っています。
フレームワークはdjango。
ChatGPTで自動プログラム作成に取り組み中。
https://www.osumoi-stdio.com/novel/