解耦模块与优化依赖注入策略
- 单一职责原则:确保每个模块只负责一项明确的功能。例如,如果有一个模块既处理用户认证又处理用户数据存储,将其拆分为两个独立模块:
UserAuthenticator
和 UserDataStore
。这样每个模块功能清晰,依赖关系自然减少。
- 接口与抽象类:使用接口(Ruby 中可通过模块实现类似接口功能)或抽象类定义模块间的交互。例如,定义一个
PaymentGateway
接口模块,不同的支付实现(如 StripePayment
、PayPalPayment
)都实现这个接口。调用模块依赖这个接口而不是具体实现类,降低耦合。
module PaymentGateway
def process_payment(amount, card_info)
raise NotImplementedError
end
end
class StripePayment
include PaymentGateway
def process_payment(amount, card_info)
# Stripe 支付处理逻辑
end
end
- 依赖注入容器:引入依赖注入容器,如
Dry::Container
。在应用启动时,将所有依赖项注册到容器中。例如:
require 'dry/container'
container = Dry::Container.new
container.register(:user_repository) { UserRepository.new }
container.register(:user_service) { UserService.new(container[:user_repository]) }
- 构造函数注入:在模块初始化时通过构造函数传递依赖。例如:
class OrderProcessor
def initialize(payment_gateway, inventory_service)
@payment_gateway = payment_gateway
@inventory_service = inventory_service
end
def process_order(order)
if @payment_gateway.process_payment(order.amount, order.payment_info)
@inventory_service.decrease_stock(order.items)
end
end
end
处理循环依赖
- 引入中间层:如果模块A依赖模块B,模块B又依赖模块A,可以引入一个中间模块C。模块A和模块B都依赖模块C,由模块C来协调A和B之间的交互。例如,A是订单模块,B是库存模块,C可以是一个协调订单和库存操作的调度模块。
- 延迟加载:在Ruby中,可以使用
lazy
方法延迟加载依赖。例如:
class A
def initialize
@b = -> { require_relative 'b'; B.new }
end
def use_b
@b.call.do_something
end
end
class B
def initialize
@a = -> { require_relative 'a'; A.new }
end
def use_a
@a.call.do_something_else
end
end
- 重构设计:深入分析循环依赖背后的业务逻辑,重新设计模块结构。可能是模块划分不合理,将相关功能整合到一个模块,或者重新梳理业务流程,打破循环依赖。