策略模式

策略模式作為一種軟體設計模式，指物件有某個行為，但是在不同的場景中，該行為有不同的實現演算法。比如每個人都要“交個人所得稅”，但是“在美國交個人所得稅”和“在中華民國交個人所得稅”就有不同的算稅方法。策略模式：示例.C++.using namespace std;class StrategyInterfacepublic:virtual void execute() = 0;class ConcreteStrategyA: public StrategyInterfacepublic:virtual void execute()cout « "Called ConcreteStrategyA execute method" « endl;};class ConcreteStrategyB: public StrategyInterfacepublic:virtual void execute()cout « "Called ConcreteStrategyB execute method" « endl;};class ConcreteStrategyC: public StrategyInterfacepublic:virtual void execute()cout « "Called ConcreteStrategyC execute method" « endl;};class Contextprivate:StrategyInterface *_strategy;public:Context(StrategyInterface *strategy):_strategy(strategy)void set_strategy(StrategyInterface *strategy)_strategy = strategy;void execute()_strategy-&gt;execute();};int main(int argc, char *argv[])ConcreteStrategyA concreteStrategyA;ConcreteStrategyB concreteStrategyB;ConcreteStrategyC concreteStrategyC;Context contextA(&amp;concreteStrategyA);Context contextB(&amp;concreteStrategyB);Context contextC(&amp;concreteStrategyC);contextA.execute();contextB.execute();contextC.execute();contextA.set_strategy(&amp;concreteStrategyB);contextA.execute();contextA.set_strategy(&amp;concreteStrategyC);contextA.execute();return 0;Java.//StrategyExample test applicationclass StrategyExample {public static void main(String[] args) {Context context;// Three contexts following different strategiescontext = new Context(new FirstStrategy());context.execute();context = new Context(new SecondStrategy());context.execute();context = new Context(new ThirdStrategy());context.execute();// The classes that implement a concrete strategy should implement this// The context class uses this to call the concrete strategyinterface Strategy {void execute();// Implements the algorithm using the strategy interfaceclass FirstStrategy implements Strategy {public void execute() {System.out.println("Called FirstStrategy.execute()");class SecondStrategy implements Strategy {public void execute() {System.out.println("Called SecondStrategy.execute()");class ThirdStrategy implements Strategy {public void execute() {System.out.println("Called ThirdStrategy.execute()");// Configured with a ConcreteStrategy object and maintains a reference to a Strategy objectclass Context {Strategy strategy;// Constructorpublic Context(Strategy strategy) {this.strategy = strategy;public void execute() {this.strategy.execute();C#.using System;namespace Wikipedia.Patterns.Strategy// MainApp test applicationclass MainAppstatic void Main()Context context;// Three contexts following different strategiescontext = new Context(new ConcreteStrategyA());context.Execute();context = new Context(new ConcreteStrategyB());context.Execute();context = new Context(new ConcreteStrategyC());context.Execute();// The classes that implement a concrete strategy should implement this// The context class uses this to call the concrete strategyinterface IStrategyvoid Execute();// Implements the algorithm using the strategy interfaceclass ConcreteStrategyA : IStrategypublic void Execute()Console.WriteLine( "Called ConcreteStrategyA.Execute()" );class ConcreteStrategyB : IStrategypublic void Execute()Console.WriteLine( "Called ConcreteStrategyB.Execute()" );class ConcreteStrategyC : IStrategypublic void Execute()Console.WriteLine( "Called ConcreteStrategyC.Execute()" );// Configured with a ConcreteStrategy object and maintains a reference to a Strategy objectclass ContextIStrategy strategy;// Constructorpublic Context(IStrategy strategy)this.strategy = strategy;public void Execute()strategy.Execute();ActionScript 3.//invoked from application.initializeprivate function init() : voidvar context:Context;context = new Context( new ConcreteStrategyA() );context.execute();context = new Context( new ConcreteStrategyB() );context.execute();context = new Context( new ConcreteStrategyC() );context.execute();package org.wikipedia.patterns.strategypublic interface IStrategyfunction execute() : void ;package org.wikipedia.patterns.strategypublic final class ConcreteStrategyA implements IStrategypublic function execute():voidtrace( "ConcreteStrategyA.execute(); invoked" );package org.wikipedia.patterns.strategypublic final class ConcreteStrategyB implements IStrategypublic function execute():voidtrace( "ConcreteStrategyB.execute(); invoked" );package org.wikipedia.patterns.strategypublic final class ConcreteStrategyC implements IStrategypublic function execute():voidtrace( "ConcreteStrategyC.execute(); invoked" );package org.wikipedia.patterns.strategypublic class Contextprivate var strategy:IStrategy;public function Context(strategy:IStrategy)this.strategy = strategy;public function execute() : voidstrategy.execute();PHP.class StrategyExample {public function __construct() {$context = new Context(new ConcreteStrategyA());$context-&gt;execute();$context = new Context(new ConcreteStrategyB());$context-&gt;execute();$context = new Context(new ConcreteStrategyC());$context-&gt;execute();interface IStrategy {public function execute();class ConcreteStrategyA implements IStrategy {public function execute() {echo "Called ConcreteStrategyA execute method\n";class ConcreteStrategyB implements IStrategy {public function execute() {echo "Called ConcreteStrategyB execute method\n";class ConcreteStrategyC implements IStrategy {public function execute() {echo "Called ConcreteStrategyC execute method\n";class Context {var $strategy;public function __construct(IStrategy $strategy) {$this-&gt;strategy = $strategy;public function execute() {$this-&gt;strategy-&gt;execute();new StrategyExample;Objective-C.@protocol Strategy@required- (void)print;@end@interface ConcreteStrategyA : NSObject@end@interface ConcreteStrategyB : NSObject@end@interface Context : NSObjectid _strategy;@property (nonatomic, assign) id strategy;-(void)doIt;@end@implementation ConcreteStrategyA- (void)printNSLog(@"Test01");@end@implementation ConcreteStrategyB- (void)printNSLog(@"Test02");@end@implementation Context@synthesize strategy = _strategy;-(void)doIt[self.strategy print];@end