设计模式之策略模式

一、策略模式定义

参考Gang of Four's (GOF) Design Patterns书里的定义:Define a family of algorithms, encapsulate each one, and make them interchangeable. [The] Strategy [pattern] lets the algorithm vary independently from clients that use it

策略模式(Strategy)属于对象行为型设计模式,主要是定义算法族,分别封装起来,让他们之间可以项目替换,此模式让算法的变化独立于使用算法的客户

二、核心概念和用法

    策略模式主要用到了三个核心的OOP概念:封装、继承和多态。还包含了一些基本的设计原则:1)封装变化。2)多用组合,少用继承。3)针对接口编程,不 针对实现编程。

    此模式的适用场景是:当一个类的某些行为将会经常发生变化或者在运行时需要改变时,可以使用策略模式。

    这个模式的核心是把类的变化行为提取为一个接口,然后将类的变化部分用这个接口对象代替,也即声明为这个接口的一个属性,然后增加一个设置这个接口的方 法,以便以后修改这个类的具体行为,这样这个类可以不用管这个变化行为的具体实现,将实现委托给这个接口的具体实现。而关于这个接口的一系列实现,可以看 成是一族算法。这样就实现了将类中变化的部分封装,算法的使用跟算法的实现分离,算法被提取出来之后还可以更好的进行复用。

模式参与者

下面快速浏览一下这个模式主要参与者:

  • Context超类

定义了所有需要继承这个类的子类的属性跟方法,并且定义了改变这个父类变化行为 的方法

  • 算法接口

定义了具体行为的方法声明,由这个接口的实现类来具体实现接口定义的行为方法。

  • 具体算法类

一组具体的算法接口实现类,代表不同的行为。

  • 客户类(Client Class)

负责创建Context类的实例,并负责创建算法的实现的实例,然后赋给 Context类实例的正确算法实现对象。

四、核心模式图表

  策略模式diagram

五、一个简单的策略模式的实现

//StrategyExample test application

class StrategyExample {

public static void main(String[] args) {

Context context;

// Three contexts following different strategies
context = 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 strategy
interface Strategy {

void execute();

}

// Implements the algorithm using the strategy interface
class 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 object
class Context {

Strategy strategy;

// Constructor
public Context(Strategy strategy) {
this.strategy = strategy;
}

public void execute() {
this.strategy.execute();
}

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值