Geek_3b0433
2025-07-09
来自广东
type SendConfirmation interface { Send(order *Order) error } type EmailNotifier struct{} func (e *EmailNotifier) Send(order *Order) error { fmt.Println("Sending email confirmation...") return nil } type SMSNotifier struct{} func (s *SMSNotifier) Send(order *Order) error { fmt.Println("Sending SMS confirmation...") return nil } type ValidateOrder struct{} func (v *ValidateOrder) Validate(order *Order) error { // 统一验证逻辑 return nil } 发送订单确认通知 SendConfirmation和处理支付 ProcessPayment,比如支付宝、微信支付、Stripe 等 应该定义成接口,因为存在多种实现, 验证订单 ValidateOrder和扣减库存 DecreaseStock逻辑较为单一,直接依赖具体实现即可。
展开
作者回复: 👍