Strategy pattern

04-08-11 zujiyun

I have a question about Strategy pattern, I use the example( in this website to talk about the question ;

My question is that why Strategy pattern uses the class RepTempRuleSolve (called Context in GOF), I know that RepTempRuleSolve is used by client program to decide which stratgy to use, for example:

RepTempRuleSolve solver=new RepTempRuleSolve(new RepTempRuleTwo());

the code here makes the decision to use the strategy implementation RepTempRuleTwo, but why the code does not use the stategy implementation without using RepTempRuleSolve ?

for a new client code example here:

RepTempRule strategy = new RepTempRuleTwo();
strategy.replace(site, context);

In the second example, the client code uses the strategy implementation with using the RepTempRuleSolve; In both examples, the client code also needs to make the decision which strategy implementation to be used, so what's the the benefits of use the class RepTempRuleSolve ?


2004-08-13 14:49

RepTempRuleSolver 其实类似中间者,解耦客户端和算法策略选择,RepTempRuleSolver可以使用工厂模式、IoC之类模式实现。