我将描述我想要做的事情,希望有人能告诉我这是什么设计模式或指出更好的替代方案。
我有一个方法来完成一堆涉及近似的复杂工作。虽然可以计算出结果而不使用近似,但这需要更多的工作。
我想要做的是得到一些与实现内部操作相关的比较。我的想法是传入一个对象来执行这个额外的工作,并存储关于比较的信息。
我认为我想从以下内容开始:
我有一个方法来完成一堆涉及近似的复杂工作。虽然可以计算出结果而不使用近似,但这需要更多的工作。
我想要做的是得到一些与实现内部操作相关的比较。我的想法是传入一个对象来执行这个额外的工作,并存储关于比较的信息。
我认为我想从以下内容开始:
class Classifier(object):
...
def classify(self, data):
# Do some approximate stuff with the data and other
# instance members.
to
class TruthComparer(object):
def compute_and_store_stats(self, data, accessory_data):
# Do the exact computation, compare to the approximation,
# and store it.
def get_comparison_stats(self):
# Return detailed and aggregate information about the
# differences between the truth and the approximation.
class Classifier(object):
...
def classify(self, data, truth_comparer=None):
# Do some approximate stuff with the data and other
# instance members.
# Optionally, do exact stuff with the data and other
# instance members, storing info about differences
# between the exact computation and the approximation
# in the truth_comparer
if truth_comparer is not None:
truth_comparer.compute_and_store_stats(data,
[self._index, self._model],
intermediate_approximation)
我不想在classify
方法内联做这些比较,因为我认为这些比较不适合该方法或对象的工作。
那么,这是哪种设计模式呢?你能提供一种替代方案吗?