给定 3 个价格因素,什么是价格逻辑的最佳方法
Given 3 factors for price, what is best way to do logic for price
我是一名初级开发人员(没有 CS 背景的训练营),目前从事产品定价工作。我正在向这个伟大的社区寻求建议。
价格基于 3 个多层次因素:
颜色:白色、蓝色、红色、绿色
尺码:小号、中号、大号
Material:棉、丝、人造丝
我试过的方法:If-else 控制流程,但知道我们将很快添加更多材料和颜色,这个 if-else 会更长更慢。
if color == "white" and material == "cotton"
price = 5 if size == "small" || size == "medium"
price = 6 if size == "large"
elsif color == "white" and material == "rayon"
price = 6 if size == "small" || size == "medium"
price = 7 if size == "large"
.
.
.
end
考虑到以下因素的最佳实施或定价方法是什么
以后会增加更多关卡和因素吗?
任何能为我指出解决方案的建议都将不胜感激。
我认为除了 oop 的观点之外,如果我对你的理解正确,那么你可以想出一个像这样的散列对象:
[
{
color=>"white",
material=>"Cotton",
size=>"medium",
price=>5
}, ...
]
然后通过遍历列表来满足对每个参数的请求,并且在将来添加更多参数时不需要更改代码。
但这是最基本的方法,你应该想出变体对象,然后是一个令人满意的函数,使它更通用。
我在这里做出商业假设,但我猜测不同的材料、颜色等会影响价格。即,给定一个基本产品(比如 t-shirt),它的基本价格为 5,然后选择一些 materials/sizes 会增加成本。
如果是这样的话,我可能会建议实现一个 PriceModifierPolicy
对象,它定义常量,并使用传入的符号 + 基价来“确定”总价。即
class PriceModifierPolicy
COLOR_PRICE_MODIFIER_MAP = {
white: 1,
gray: 1,
black: 2
}
MATERIAL_PRICE_MODIFIER_MAP = {
cotton: 0,
rayon: 1
}
SIZE_PRICE_MODIFIER_MAP = {
small: 0,
medium: 0,
large: 1
}
def initialize(base_price, color, material, size)
@base_price = base_price
@color = color
@material = material
@size = size
end
def compute_final_price!
final_price = @base_price + color_price_modifier
final_price += material_price_modifier
final_price += size_price_modifier
final_price
end
def color_price_modifier
COLOR_PRICE_MODIFIER_MAP[@color]
end
def material_price_modifier
MATERIAL_PRICE_MODIFIER_MAP[@material]
end
def size_price_modifier
SIZE_PRICE_MODIFIER_MAP[@size]
end
end
这种方法的好处是您可以将 base_price
替换为您要定价的产品,然后将其实现为可继承的 BasePriceModifierPolicy contains/can 查询知识(来自数据库) ,关于给定产品的给定定价修饰符。即
class BasePriceModifierPolicy
COLOR_PRICE_MODIFIER_MAP = {
white: 1,
gray: 1,
black: 2
}
MATERIAL_PRICE_MODIFIER_MAP = {
cotton: 0,
rayon: 1
}
SIZE_PRICE_MODIFIER_MAP = {
small: 0,
medium: 0,
large: 1
}
def initialize(product, color, material, size)
@base_price = product
@color = color
@material = material
@size = size
end
def compute_final_price!
final_price = @base_price + color_price_modifier
final_price += material_price_modifier
final_price += size_price_modifier
final_price
end
def color_price_modifier
raise NotImplementedError
end
def material_price_modifier
raise NotImplementedError
end
def size_price_modifier
raise NotImplementedError
end
end
class ShirtPriceModifierPolicy < BasePriceModifierPolicy
COLOR_PRICE_MODIFIER_MAP = {
white: 3,
gray: 2,
black: 5
}
MATERIAL_PRICE_MODIFIER_MAP = {
cotton: 1,
rayon: 2
}
SIZE_PRICE_MODIFIER_MAP = {
small: 1,
medium: 2,
large: 3
}
def color_price_modifier
COLOR_PRICE_MODIFIER_MAP[@color]
end
def material_price_modifier
MATERIAL_PRICE_MODIFIER_MAP[@material]
end
def size_price_modifier
SIZE_PRICE_MODIFIER_MAP[@size]
end
end
如果您不需要支持不同的产品,只需 ever-expanding 个 add-ons 列表。然后结帐the decorator pattern.
我是一名初级开发人员(没有 CS 背景的训练营),目前从事产品定价工作。我正在向这个伟大的社区寻求建议。
价格基于 3 个多层次因素:
颜色:白色、蓝色、红色、绿色 尺码:小号、中号、大号 Material:棉、丝、人造丝
我试过的方法:If-else 控制流程,但知道我们将很快添加更多材料和颜色,这个 if-else 会更长更慢。
if color == "white" and material == "cotton"
price = 5 if size == "small" || size == "medium"
price = 6 if size == "large"
elsif color == "white" and material == "rayon"
price = 6 if size == "small" || size == "medium"
price = 7 if size == "large"
.
.
.
end
考虑到以下因素的最佳实施或定价方法是什么 以后会增加更多关卡和因素吗?
任何能为我指出解决方案的建议都将不胜感激。
我认为除了 oop 的观点之外,如果我对你的理解正确,那么你可以想出一个像这样的散列对象:
[
{
color=>"white",
material=>"Cotton",
size=>"medium",
price=>5
}, ...
]
然后通过遍历列表来满足对每个参数的请求,并且在将来添加更多参数时不需要更改代码。 但这是最基本的方法,你应该想出变体对象,然后是一个令人满意的函数,使它更通用。
我在这里做出商业假设,但我猜测不同的材料、颜色等会影响价格。即,给定一个基本产品(比如 t-shirt),它的基本价格为 5,然后选择一些 materials/sizes 会增加成本。
如果是这样的话,我可能会建议实现一个 PriceModifierPolicy
对象,它定义常量,并使用传入的符号 + 基价来“确定”总价。即
class PriceModifierPolicy
COLOR_PRICE_MODIFIER_MAP = {
white: 1,
gray: 1,
black: 2
}
MATERIAL_PRICE_MODIFIER_MAP = {
cotton: 0,
rayon: 1
}
SIZE_PRICE_MODIFIER_MAP = {
small: 0,
medium: 0,
large: 1
}
def initialize(base_price, color, material, size)
@base_price = base_price
@color = color
@material = material
@size = size
end
def compute_final_price!
final_price = @base_price + color_price_modifier
final_price += material_price_modifier
final_price += size_price_modifier
final_price
end
def color_price_modifier
COLOR_PRICE_MODIFIER_MAP[@color]
end
def material_price_modifier
MATERIAL_PRICE_MODIFIER_MAP[@material]
end
def size_price_modifier
SIZE_PRICE_MODIFIER_MAP[@size]
end
end
这种方法的好处是您可以将 base_price
替换为您要定价的产品,然后将其实现为可继承的 BasePriceModifierPolicy contains/can 查询知识(来自数据库) ,关于给定产品的给定定价修饰符。即
class BasePriceModifierPolicy
COLOR_PRICE_MODIFIER_MAP = {
white: 1,
gray: 1,
black: 2
}
MATERIAL_PRICE_MODIFIER_MAP = {
cotton: 0,
rayon: 1
}
SIZE_PRICE_MODIFIER_MAP = {
small: 0,
medium: 0,
large: 1
}
def initialize(product, color, material, size)
@base_price = product
@color = color
@material = material
@size = size
end
def compute_final_price!
final_price = @base_price + color_price_modifier
final_price += material_price_modifier
final_price += size_price_modifier
final_price
end
def color_price_modifier
raise NotImplementedError
end
def material_price_modifier
raise NotImplementedError
end
def size_price_modifier
raise NotImplementedError
end
end
class ShirtPriceModifierPolicy < BasePriceModifierPolicy
COLOR_PRICE_MODIFIER_MAP = {
white: 3,
gray: 2,
black: 5
}
MATERIAL_PRICE_MODIFIER_MAP = {
cotton: 1,
rayon: 2
}
SIZE_PRICE_MODIFIER_MAP = {
small: 1,
medium: 2,
large: 3
}
def color_price_modifier
COLOR_PRICE_MODIFIER_MAP[@color]
end
def material_price_modifier
MATERIAL_PRICE_MODIFIER_MAP[@material]
end
def size_price_modifier
SIZE_PRICE_MODIFIER_MAP[@size]
end
end
如果您不需要支持不同的产品,只需 ever-expanding 个 add-ons 列表。然后结帐the decorator pattern.