Slide 35
Slide 35 text
©OPENLOGI Inc.
リファクタリングの進め方
35
抽象と具象
● 構造設計は抽象的な話になりがち
● しかし最終的な成果物は具体のコードである
● 抽象と具象のレイヤー間でギャップが⽣まれる
class InventoryAllocator
:
def __init__
(self):
self.inventory = {}
def add_item
(self, item_name
, quantity
):
if item_name in self.inventory:
self.inventory[item_name]
+= quantity
else:
self.inventory[item_name]
= quantity
def allocate_item
(self, item_name
, request_quantity
):
if item_name not in self.inventory:
raise ValueError
("Item not found"
)
if self.inventory[item_name]
< request_quantity:
raise ValueError
("Insufficient stock"
)
self.inventory[item_name]
-= request_quantity
return f"{request_quantity
} units of {item_name
} allocated"