Strategy Pattern
Kullanılan algoritma runtime de sürekli değişiyorsa strategy pattern kullanılması uygun olur. Algoritmalar bir interface ile encapsulate edilerek runtime da değiştirilebilir hale getirilir. Algoritmaları kullanan objenin bu değişiklerden haberi olmaz. Ayrıca başka bir algoritma interface i implement ederek kolayca sisteme eklebilir.
UML :
Java Örneği:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | //StrategyExample test application class StrategyExample { public static void main(String[] args) { Context context; // Three contexts following different strategies context = new Context(new ConcreteStrategyAdd()); int resultA = context.executeStrategy(3,4); context = new Context(new ConcreteStrategySubtract()); int resultB = context.executeStrategy(3,4); context = new Context(new ConcreteStrategyMultiply()); int resultC = context.executeStrategy(3,4); } } // The classes that implement a concrete strategy should implement this // The context class uses this to call the concrete strategy interface Strategy { int execute(int a, int b); } // Implements the algorithm using the strategy interface class ConcreteStrategyAdd implements Strategy { public int execute(int a, int b) { System.out.println("Called ConcreteStrategyAdd's execute()"); return a + b; // Do an addition with a and b } } class ConcreteStrategySubtract implements Strategy { public int execute(int a, int b) { System.out.println("Called ConcreteStrategySubtract's execute()"); return a - b; // Do a subtraction with a and b } } class ConcreteStrategyMultiply implements Strategy { public int execute(int a, int b) { System.out.println("Called ConcreteStrategyMultiply's execute()"); return a * b; // Do a multiplication with a and b } } // Configured with a ConcreteStrategy object and maintains a reference to a Strategy object class Context { private Strategy strategy; // Constructor public Context(Strategy strategy) { this.strategy = strategy; } public int executeStrategy(int a, int b) { return strategy.execute(a, b); } } |
Php Örneği:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | <?php class StrategyExample { public function __construct() { $context = new Context(new ConcreteStrategyA()); $context->execute(); $context = new Context(new ConcreteStrategyB()); $context->execute(); $context = new Context(new ConcreteStrategyC()); $context->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 { private $strategy; public function __construct(IStrategy $strategy) { $this->strategy = $strategy; } public function execute() { $this->strategy->execute(); } } new StrategyExample(); ?> |
Kaynak: wikipedia