Strategy Pattern với Dictionary
Dùng dict để map các hàm theo key, tiện xử lý logic thay vì if-elif
dài dòng:
def add(x, y): return x + y
def sub(x, y): return x - y
operations = {
"add": add,
"sub": sub
}
result = operations["add"](3, 4) # 7
Không có bình luận