在大多数示例中,策略模式通常适用于压缩算法/编解码器算法,这些算法可能具有完全不同的代码。
但是,就我而言,我想重构为策略模式的统计算法有 50% 的代码相似或完全相同。我想知道人们如何处理这个问题?如果我使用策略模式,可能会有 50% 的代码被复制,这是不希望的。如果不重构,代码最终会包含许多 if-else 集来处理不同类型的统计算法。如何评估权衡?其他可能的解决方案是什么?
这是我试图优雅地解决的一个案例。假设我有一个可枚举(它可能是一个相当大的可枚举,我只想枚举一次)。还可以说,如果序列中返回的对象与操作的某些条件匹配,我想要运行特定的操作。
在函数式语言中,我可以设置一系列匹配项,当找到匹配项时将执行这些匹配项。我想要在 C# 中实现类似的功能。如果可能的话使用 LINQ。
我最接近的是使用策略模式和一个简单的规则引擎,该引擎按顺序调用每个注册的策略,直到找到匹配项。有没有更简单的方法?
我想做的一件事是。
myEnum.Match((item)=>item.MatchesCondition, (item)=>ExecuteFunction(item))
.Match((item)=>item.MatchesSomeOtherCondition, (item)=>ExecuteSomeOtherFunction(item));
Run Code Online (Sandbox Code Playgroud) 我有几个问题:遵循我在该问题上得到的完美答复Businesslogic dependent on model attribute。
我计划使用策略模式,我想知道应该将类文件放在哪里(哪个文件夹中)?他们应该放在模型文件夹中吗?另外,我有一个默认行为,我是否仍然需要创建一个 Interface 类并从中派生我的默认策略,或者我将我的默认策略定义为基本策略?
我有一个父类型
public class IObject{}
Run Code Online (Sandbox Code Playgroud)
并且可以有很多子类(甚至是未来的新子类)
public class Object1 extends IObject{}
public class Object2 extends IObject{}
public class Object3 extends IObject{}
...
public class ObjectN extends IObject{}
Run Code Online (Sandbox Code Playgroud)
然后根据这些对象的类型我必须做不同的操作。
public class StrategyForObject1(){void do{}}
public class StrategyForObject2(){void do{}}
public class StrategyForObject3(){void do{}}
...
public class StrategyForObjectN(){void do{}}
Run Code Online (Sandbox Code Playgroud)
所以我想从我的 Context 类:
public Conext {
IObject o;
public void setObject(IObject o) {
this.o = o;
}
void logic() {
if (o instanceOf Object1) {
new StrategyForObject1().do();
}
if (o instanceOf Object2) {
new StrategyForObject2().do();
} …Run Code Online (Sandbox Code Playgroud) java design-patterns strategy-pattern visitor-pattern genetic-algorithm
我正在寻找一种在 Rust 中实现策略模式的方法。我希望实现类似于此伪代码的东西:
class StrategyBase {
Data data;
def abstract methodAWorkingOnData();
def abstract methodBWorkingOnData();
}
class StrategyImplOne {
def methodAWorkingOnData()
... method code ...
def methodBWorkingOnData()
... method code ....
}
class StrategyImplTwo {
def methodAWorkingOnData()
... method code ...
def methodBWorkingOnData()
... method code ....
}
Run Code Online (Sandbox Code Playgroud)
我期望我可以有一个带有两个特征的结构,但我找不到实现这种模式的方法。
在C#中为模板/抽象类的构造函数实现策略的最佳方法是什么?我有几个类都基于解析构造函数中的字符串.解析是在静态方法中完成的,该方法创建键值对的列表,并且对于所有类都是通用的,但是某些字段对于所有类也是通用的 - 因此我使用抽象模板类.
问题是我没有看到继承抽象基类的构造函数实现的方法.否则,我会在基类中实现构造函数策略,并强制在一些抽象方法中处理列表.
编辑:添加模板类的不工作代码
public abstract class XXXMessageTemplate
{
public XXXMessageTemplate(string x) // implementation for the constructor
{
Parse(x);//general parse function
CommonFields();//filling common properties
HandlePrivateProperties();//fill individual properties
HandlePrivateStructures();//fill individual structures
}
abstract void HandlePrivateProperties();
abstract void HandlePrivateStructures();
}
The actual messages should not implement any constructor and only implement the HandlePrivateProperties and HandlePrivateStructures functions.
Run Code Online (Sandbox Code Playgroud) 我是ColdFusion的新手并使用ColdFusion MX(7),我有兴趣实现策略模式.但我不知道什么是开始实施的最佳方式.
我正在尝试将维基百科的策略模式示例转换为现实生活中的问题,但我不确定我的场景是否确实需要这种模式。
假设我们有一项服务可以从多个客户端获取更新,并且需要进行一些处理并向前推进这些更新,具体取决于它们的大小。我希望服务尽可能简单,并对未来的更新格式(来自其他客户端)开放。
我想我可以通过使用这种模式让更新本身决定如何进行处理和前进,并让服务尽可能简单。像这样的东西:
public class Service {
void processUpdate(Update myUpdate) {
myUpdate.process();
myUpdate.moveForward();
}
}
Run Code Online (Sandbox Code Playgroud)
我错了吗 ?如何(在哪里...)为每次更新分配策略?
语境
我正在尝试在 Python 2.7 中实现策略模式的一些变体。我希望能够实例化“my_strategy”基类,但在运行时在“score”方法的不同实现之间切换。我将在“my_strategy”中有许多常用方法,但有一堆“score”实现。主要说明了我想如何使用它。当然,这里的评分实现是虚拟的。
我尝试过的(即到目前为止我的代码)
策略.py:
from algo_one import *
#from algo_two import *
class my_strategy ( object ):
def __init__(self, candidate = ""):
self.candidate = candidate
self.method = 'default'
self.no = 10
self._algo = algo_one
def set_strategy(self, strategy='default'):
self.strategy = strategy
if self.strategy == 'algo_one':
self._algo = algo_one
elif self.strategy == 'algo_two':
# self._algo = algo_two
pass
else:
self._algo = None
def score(self, *args):
if len(args) > 0:
self.candidate = args[0]
self._algo.score(self.candidate)
if __name__ == "__main__": …Run Code Online (Sandbox Code Playgroud) 我正在尝试用C++实现策略模式,而我在查找正确的语法时遇到了麻烦.这是我想要做的一个(相当简单的)java代码示例:
public static interface Processor<T> {
public void process(T toProcess);
}
public static class Printer implements Processor<String> {
@Override
public void process(String toProcess) {
System.out.println(toProcess);
}
}
public static class StringHandler {
public void handleData(Processor<String> processor) {
processor.process("Hello World!");
}
}
public static void main(String... args) throws Exception {
new StringHandler().handleData(new Printer());
}
Run Code Online (Sandbox Code Playgroud)
尝试使用C++等价物:
#include <string>
using namespace std;
template <typename T>class Processor {
public:
virtual void process(T rhs) = 0;
};
class Printer : public Processor<string*> {
public:
void …Run Code Online (Sandbox Code Playgroud) strategy-pattern ×10
c# ×2
java ×2
algorithm ×1
c++ ×1
coldfusion ×1
constructor ×1
inheritance ×1
interface ×1
linq ×1
python ×1
rust ×1
templates ×1