【发布时间】:2020-12-27 06:51:14
【问题描述】:
我有一个应用程序,用户可以在其中更改某些设置。用户可以更改(设置)和读取(获取)他们的设置,这是一个类似映射的结构。
我创建了一个 UserSettingsInteractor 对象,它实现了更改和读取设置的用例,在 Python 中它看起来像:
class UserSettingsInteractor:
def __init__(self, ...): ...
def set_foo(self, user_id: int, value): ...
def get_foo(self, user_id: int): ...
def set_bar(self, user_id: int, value): ...
def get_bar(self, user_id: int): ...
...
其中的好处是现有设置foo 和bar 是明确的。但是编写 setter 和 getter 会变得非常冗长。可以概括为:
class UserSettingsInteractor:
def __init__(self, ...): ...
def set(self, user_id: int, setting: str, value): ...
def get(self, user_id: int, setting: str): ...
现在客户端代码知道设置是如何表示的,它是一个键值对。不是让代码更脆弱吗?
哪种方法更好?
【问题讨论】:
标签: design-patterns architecture software-design clean-architecture