我正在使用Ruby和OO开始学习,并且我已经接受了关于OO的测试。
这是一个商店场景,我必须创建类,方法,调用...... 目前我已创建课程付款和产品类(实体,会员,数字)
class Payment
attr_reader :authorization_number, :amount, :invoice, :order, :payment_method, :paid_at, :shipping
def initialize(attributes = {})
@authorization_number, @amount = attributes.values_at(:authorization_number, :amount)
@invoice, @order = attributes.values_at(:invoice, :order)
@payment_method = attributes.values_at(:payment_method)
@shipping.extend Shipping
end
def pay(paid_at = Time.now)
@amount = order.total_amount
@authorization_number = Time.now.to_i
@invoice = Invoice.new(order.get_address, order.get_address, order)
@paid_at = paid_at
order.close(@paid_at)
extend
def paid?
!paid_at.nil?
end
def prepare_shipping(order)
???
end
end
class Product
attr_reader :name, :description, :type
def initialize(name, description, type)
@name = name
@description = description
@type = type
end
end
class Digital < Product
include DigitalProduct
def initialize(name, description, type)
super(name, description, type)
end
def matches?(query)
query=="digital"
end
end
class Physical < Product
def initialize(name, description, type)
super(name, description, type)
end
def matches?(query)
query=="physical"
end
end
class Membership < Product
attr_reader :membership_status
include DigitalProduct
def initialize(name, description, type)
super(name, description, type)
end
def matches?(query)
query=="membership"
end
def activate_membership()
@membership_status = true;
end
end
这是我的问题:
1.如果产品是实物,我必须生成运输标签。
1.1如果它是一本书,我必须通知该产品没有税。
2.如果产品是会员资格,我必须激活签名并通过电子邮件通知买家。
我做得很好,但现在我希望你的意见知道在OO技术(SOLID)之后创建上述点的最佳方法是什么。
ps:我试图创建,但我的解决方案真的很差,很多ifs。 我认为有更好的方法来实现
如果您需要更多信息请告诉我,我还有其他课程。
抱歉我的英语不好。
我已经开发了这个解决方案,我认为这很好......也许你可以给我一个反馈意见。
因此,要解决有关运费和折扣规则的问题,我创建了2个模块Shipping
和Discount
module Discount
def prepare_discount(order)
total_discount = 0
order.get_items.each do |item|
total_discount += item.product.discount
end
return total_discount
end
def discount
if matches?("physical")
return discount_for_physical_product
elsif matches?("digital")
return discount_for_digital_product
elsif matches?("membership")
return discount_for_membership_product
elsif matches?("default")
return discount_for_default_product
end
end
def discount_for_physical_product
price_discount_for_physical_product = 0
return price_discount_for_physical_product
end
def discount_for_digital_product
price_discount_for_digital_product = 10.00
return price_discount_for_digital_product
end
def discount_for_default_product
price_discount_for_digital_product = 10.00
return price_discount_for_digital_product
end
def discount_for_membership_product
price_discount_for_membership_product = 0
return price_discount_for_membership_product
end
end
module Shipping
def prepare_shipping(order)
order.get_items.each do |item|
item.product.shipping
end
end
def shipping
if matches?("physical")
shipping_for_physical_product
elsif matches?("digital")
shipping_for_digital_product
elsif matches?("membership")
shipping_for_membership_product
elsif matches?("default")
shipping_for_default_product
end
end
def shipping_for_physical_product
case @type
when :book
create_shipping_label
notify_buyer_product_without_taxes
else
create_shipping_label
end
end
def shipping_for_digital_product
notify_buyer_via_email
end
def shipping_for_membership_product
notify_buyer_via_email
activate_membership
end
def shipping_for_default_product
create_shipping_label
notify_buyer_via_email
end
def create_shipping_label
puts "Creating shipping label ..."
end
def notify_buyer_product_without_taxes
puts "Product exempt from taxes, as provided in the Constitution Art. 150, VI, d."
end
def notify_buyer_via_email
puts "Sending email ..."
end
end
这种方式Product
和Order
可以扩展两个模块并实现逻辑。