策略模式
思想
定义了算法家族,分别封装起来,让他们之间可见相互替换,此模式让算法的变化,不会影响到使用算法的客户
场景—>商场促销
C++
策略模式
/*策略模式*/
#include<iostream>
#include<string>
using namespace std;
class Strategy {
public:
virtual void AlgorithmInterface()=0;
};
class ConcreteStrategyA:public Strategy {
public:
void AlgorithmInterface() override {
printf("算法A");
}
};
class ConcreteStrategyB:public Strategy {
public:
void AlgorithmInterface() override {
printf("算法B");
}
};
class ConcreteStrategyC:public Strategy {
public:
void AlgorithmInterface() override {
printf("算法C");
}
};
class Context {
public:
Strategy *strategy;
Context(Strategy *strategy) {
this->strategy=strategy;
}
void ContextInterface() {
strategy->AlgorithmInterface();
}
};
//客户端
int main() {
Context *context=nullptr;
context=new Context(new ConcreteStrategyA);
context->ContextInterface();
context=new Context(new ConcreteStrategyB);
context->ContextInterface();
context=new Context(new ConcreteStrategyC);
context->ContextInterface();
}
策略模式与简单工厂模式结合
/*策略模式与简单工厂模式结合*/
#include<iostream>
#include<string>
using namespace std;
class Strategy {
public:
virtual void AlgorithmInterface()=0;
};
class ConcreteStrategyA:public Strategy {
public:
void AlgorithmInterface() override {
printf("算法A");
}
};
class ConcreteStrategyB:public Strategy {
public:
void AlgorithmInterface() override {
printf("算法B");
}
};
class ConcreteStrategyC:public Strategy {
public:
void AlgorithmInterface() override {
printf("算法C");
}
};
class Context {
public:
Strategy *strategy;
Context(char tpye) {
switch(tpye) {
case 'A':
strategy=new ConcreteStrategyA;
break;
case 'B':
strategy=new ConcreteStrategyB;
break;
case 'C':
strategy=new ConcreteStrategyC;
break;
}
}
void ContextInterface() {
strategy->AlgorithmInterface();
}
};
int main() {
Context *context=nullptr;
//使得算法与和客户端彻底的隔离
context=new Context('A');
context->ContextInterface();
}
GO
策略模式
package main
import "fmt"
type Strategy interface {
AlgorithmInterface()
}
type ConcreteStrategyA struct {
}
type ConcreteStrategyB struct {
}
type ConcreteStrategyC struct {
}
func (ca *ConcreteStrategyA) AlgorithmInterface() {
fmt.Println("Algorithm A")
}
func (cb *ConcreteStrategyB) AlgorithmInterface() {
fmt.Println("Algorithm B")
}
func (cc *ConcreteStrategyC) AlgorithmInterface() {
fmt.Println("Algorithm C")
}
type Context struct {
strategy Strategy
}
func (context *Context) ContextInterface() {
context.strategy.AlgorithmInterface()
}
//客户端
func main(){
var context Context
context.strategy=new(ConcreteStrategyA)
context.ContextInterface()
context.strategy=new(ConcreteStrategyB)
context.ContextInterface()
context.strategy=new(ConcreteStrategyC)
context.ContextInterface()
}
策略模式与简单工厂模式结合
package main
import "fmt"
type Strategy interface {
AlgorithmInterface()
}
type ConcreteStrategyA struct {
}
type ConcreteStrategyB struct {
}
type ConcreteStrategyC struct {
}
func (ca *ConcreteStrategyA) AlgorithmInterface() {
fmt.Println("Algorithm A")
}
func (cb *ConcreteStrategyB) AlgorithmInterface() {
fmt.Println("Algorithm B")
}
func (cc *ConcreteStrategyC) AlgorithmInterface() {
fmt.Println("Algorithm C")
}
type Context struct {
strategy Strategy
}
func (context *Context) ContextInterface() {
context.strategy.AlgorithmInterface()
}
func (context *Context) init(s string) {
switch s {
case "A":
context.strategy = new(ConcreteStrategyA)
case "B":
context.strategy = new(ConcreteStrategyB)
case "C":
context.strategy = new(ConcreteStrategyC)
}
}
//客户端
func main() {
var context Context
context.init("A")
context.ContextInterface()
}