使用策略模式,如何以不同的参数对待不同的execute方法?具有不同方法签名的策略模式
3例策略
public function execute(string $param1, string $param2)
{
// Do something specific to this method
//
// Do some generic things across all strategies
//
}
public function execute(string $param1)
{
// Do something specific to this method
//
// Do some generic things across all strategies
//
}
public function execute()
{
// Do something specific to this method
//
// Do some generic things across all strategies
}
的都做很具体的东西,但需要这种不同的参数,然后他们做一些通用的,每一个战略将做。
你如何找出这些参数的每个功能需求?参数选择必须是策略的一部分,而不是界面的一部分。 –
是否有可能在构建策略时传递所有必需的数据并从策略界面中删除参数?如果你能做到这一点,那么也许可以将算法的通用部分移到抽象基本策略。或者让你的策略接口期望一个数据包(1个模型/对象),并让策略实现有选择地使用它的一部分。 – Reasurria