
# 实体
class Order
attr_reader :id, :total
def initialize(id, total)
@id = id
@total = total
end
def payable
total >= 200 ? total - total / 10 : total
end
end
# 网关
module Catalog
def price(sku)
raise NotImplementedError, "#{self.class}#price"
end
end
# 仓储
module Orders
def save(order)
raise NotImplementedError, "#{self.class}#save"
end
def count
raise NotImplementedError, "#{self.class}#count"
end
end
# 输出端口
module Report
def say(text)
raise NotImplementedError, "#{self.class}#say"
end
end
# 用例:只索取所需部件,从不自己构建
class PlaceOrder
def initialize(catalog, orders, report)
@catalog = catalog
@orders = orders
@report = report
@next_id = 1001
end
def place(items)
total = 0
items.each do |sku, count|
price = @catalog.price(sku)
unless price
@report.say("no #{sku}")
return
end
total += price * count
end
order = Order.new(@next_id, total)
@next_id += 1
@orders.save(order)
text = "order #{order.id} for #{order.payable}"
@report.say(text)
end
end
# 适配器
class MemoryCatalog
include Catalog
def price(sku) = {"pen" => 30, "lamp" => 190}[sku]
end
class MemoryOrders
include Orders
def initialize
@rows = []
end
def save(order) = @rows << order
def count = @rows.size
end
class ConsoleReport
include Report
def say(text) = puts(text)
end
class SilentReport
include Report
def say(text) = nil
end
# 组合根:唯一同时看到每一层的地方,
# 也是唯一构建适配器的地方。
# 两种现成的构建都是类方法,
# 因为组合根也会交出它组装好的各个部分
class ShopApp
attr_reader :use_case, :orders
def initialize(catalog, orders, report)
@orders = orders
@use_case = PlaceOrder.new(catalog, orders, report)
end
def self.loud
new(MemoryCatalog.new, MemoryOrders.new,
ConsoleReport.new)
end
def self.quiet
new(MemoryCatalog.new, MemoryOrders.new,
SilentReport.new)
end
end
# 客户端代码
app = ShopApp.loud
app.use_case.place({"pen" => 2, "lamp" => 1})
app.use_case.place({"chair" => 1})
puts "saved #{app.orders.count}"
# 第二个根以不同方式构建同一个用例,
# 场景和实体都对此一无所知
night = ShopApp.quiet
night.use_case.place({"lamp" => 1})
night.use_case.place({"pen" => 2})
puts "saved at night #{night.orders.count}"
# 根之外不构建任何适配器:客户端索取现成的应用就能得到,
# 而且两种构建得到的场景是同一个类
puts "use case #{app.use_case.class}"
same = app.use_case.class == night.use_case.class
puts "same class #{same}"
# printed: order 1001 for 225
# no chair
# saved 1
# saved at night 2
# use case PlaceOrder
# same class true
A composition root is the only place where the layers are connected and the adapters are built; the rest of the application only asks for its parts.Read more问题。用例需要一个价格目录、一个订单存储,以及一种报告结果的途径。如果用例自己创建这些部件,它就会指明外层的适配器,并依赖于它们。测试也无法为它提供别的部件。如果程序的每个部分都自行构建自己的部件,就没有人能看清应用程序是如何连接起来的。 思路。组合根是唯一一个连接各层、构建适配器的地方。适配器是实现端口的外层代码,而端口是内层声明的接口。用例只通过端口索取自己的部件,从不自己构建它们。组合根同时了解每一层,这正是它的职责。其余所有代码都遵守依赖规则:内层代码从不了解外层代码。这不需要任何框架或容器:组合根就是程序启动处的普通代码。 如何阅读示例。用例索取三个端口:一个查询价格的网关、一个保存订单的仓库,以及一个用于报告的输出端口。外层为它们提供了适配器:内存中的目录和存储,一个会打印的报告,还有一个什么也不说的报告。示例把应用程序构建了两次。第一次构建中,报告会打印:客户端代码下了订单 1001,应付 225,又下了一个含未知商品的订单。第二次构建使用静默的报告,因此它的订单什么也不打印。只有已保存订单的数量表明它们确实被保存了。两次构建中的用例完全相同。 何时使用。每个应用程序都有一个组合根,位于它的启动处;测试可以有自己的小型组合根,使用假的适配器。没有端口的短脚本不需要组合根。 常见错误。在用例内部创建适配器,哪怕只有一次。这样用例就依赖于外层,组合根也不再能展示应用程序是如何连接起来的。 |