超载和策略模式
我正在尝试编写电子邮件传递系统。我有这个枚举,对于所有用户,在过去3个月零6个月中登录的用户:
class Receiver(enum.Enum):
ALL = "ALL"
THREE_MONTHS = "THREE_MONTHS"
SIX_MONTHS = "SIX_MONTHS"
问题是,我正在尝试实施策略模式,因此我需要执行类型检查。我知道默认情况下这是不可能的,但是...任何想法都将不胜感激。
我需要看起来像:
def send_to(Receiver.ALL):
pass
def send_to(Receiver.THREE_MONTHS):
pass
def send_to(Receiver.SIX_MONTHS):
pass
是的,我有 Java背景。
并且通用发件人将相应地传递值 :
@api_view(["POST"])
def send_email(request):
email_to = request.data["emailTo"]
send_to(email_to)
注意:i 不希望在检查或开关情况下写多个。它与鲍勃叔叔的开放式原理完全矛盾,因为如果有其他情况,则必须修改该方法。绝对不可接受。
I'm trying to write an email delivery system. I have this enum, for all users, users that were logged in the last 3 months and 6 months:
class Receiver(enum.Enum):
ALL = "ALL"
THREE_MONTHS = "THREE_MONTHS"
SIX_MONTHS = "SIX_MONTHS"
The problem is, I'm trying to implement a Strategy pattern, so I need to enforce type checking. I know it is impossible by default, but ... any idea would be appreciated.
I need it to look like:
def send_to(Receiver.ALL):
pass
def send_to(Receiver.THREE_MONTHS):
pass
def send_to(Receiver.SIX_MONTHS):
pass
Yeah, I have Java background.
And the general sender would pass the value accordingly:
@api_view(["POST"])
def send_email(request):
email_to = request.data["emailTo"]
send_to(email_to)
Note: I do not want to write multiple if checks or switch cases. It totally contradicts with Open Closed principle of Uncle Bob, since if there is gonna be additional case, then the method must be modified. Absolutely unacceptable.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Python没有功能过载。 do 具有的是头等函数对象。
创建
dict
将映射接收器
值映射到适当的函数。Python doesn't have function overloading. What it does have is first-class function objects.
Create a
dict
that mapsReceiver
values to the appropriate function.