当前位置:首页 > 技术文章 > 正文内容

动手写一个策略模式(Java版)_策略模式java if else

策略模式的定义与特点

策略(Strategy)模式的定义:该模式定义了一系列算法,并将每个算法封装起来,使它们可以相互替换,且算法的变化不会影响使用算法的客户。策略模式属于对象行为模式,它通过对算法进行封装,把使用算法的责任和算法的实现分割开来,并委派给不同的对象对这些算法进行管理。

策略模式的主要优点如下。

  1. 多重条件语句不易维护,而使用策略模式可以避免使用多重条件语句,如 if...else 语句、switch...case 语句。
  2. 策略模式提供了一系列的可供重用的算法族,恰当使用继承可以把算法族的公共代码转移到父类里面,从而避免重复的代码。
  3. 策略模式可以提供相同行为的不同实现,客户可以根据不同时间或空间要求选择不同的。
  4. 策略模式提供了对开闭原则的完美支持,可以在不修改原代码的情况下,灵活增加新算法。
  5. 策略模式把算法的使用放到环境类中,而算法的实现移到具体策略类中,实现了二者的分离。


其主要缺点如下。

  1. 客户端必须理解所有策略算法的区别,以便适时选择恰当的算法类。
  2. 策略模式造成很多的策略类,增加维护难度。

策略模式的应用场景

策略模式在很多地方用到,如 Java SE 中的容器布局管理就是一个典型的实例,Java SE 中的每个容器都存在多种布局供用户选择。在程序设计中,通常在以下几种情况中使用策略模式较多。

  1. 一个系统需要动态地在几种算法中选择一种时,可将每个算法封装到策略类中。
  2. 一个类定义了多种行为,并且这些行为在这个类的操作中以多个条件语句的形式出现,可将每个条件分支移入它们各自的策略类中以代替这些条件语句。
  3. 系统中各算法彼此完全独立,且要求对客户隐藏具体算法的实现细节时。
  4. 系统要求使用算法的客户不应该知道其操作的数据时,可使用策略模式来隐藏与算法相关的数据结构。
  5. 多个类只区别在表现行为不同,可以使用策略模式,在运行时动态选择具体要执行的行为。

策略模式的结构与实现

策略模式是准备一组算法,并将这组算法封装到一系列的策略类里面,作为一个抽象策略类的子类。策略模式的重心不是如何实现算法,而是如何组织这些算法,从而让程序结构更加灵活,具有更好的维护性和扩展性,现在我们来分析其基本结构和实现方法。

1. 模式的结构

  1. 抽象策略(Strategy)类:定义了一个公共接口,各种不同的算法以不同的方式实现这个接口,环境角色使用这个接口调用不同的算法,一般使用接口或抽象类实现。
  2. 具体策略(Concrete Strategy)类:实现了抽象策略定义的接口,提供具体的算法实现。
  3. 环境(Context)类:持有一个策略类的引用,最终给客户端调用。

示例1通过接口实现

/**
 * @describe 抽象策略(Strategy)类,用于定义具体的算法,
 * Used to define Algorithms
 * @author 79197
 *
 */
public interface StrategyInterface {
	
	//策略方法
	public void strategyMethod();
	
}
//具体策略类A specifice strategy class A
public class StrategyA implements StrategyInterface{

	@Override
	public void strategyMethod() {
		System.out.println("this is StrategyA;");
	}

}
//具体策略类A specifice strategy class B
public class StrategyB implements StrategyInterface{

	@Override
	public void strategyMethod() {
		System.out.println("this is StrategyB;");
	}

}
/**
 * @describe 这个类主要用于引用策略类,创建具体的策略对象,为客户端提供调用接口
 * This class is mainly used to reference strategy(policy) classes;
 * Create a specific policy object
 * provide calling interface for client
 * @author 79197
 *
 */
public class ContextInterface {
	
	//聚合,引用策略对象
	private StrategyInterface strategyInterfaceObj;
	public StrategyInterface getStrategyInterfaceObj() {
		return strategyInterfaceObj;
	}
	public void setStrategyInterfaceObj(StrategyInterface strategyInterfaceObj) {
		this.strategyInterfaceObj = strategyInterfaceObj;
	}
	
	//策略方法的具体实现
	//specific implements class of strategy method
	public void strategyMethod() {
		strategyInterfaceObj.strategyMethod();
	}
}

示例2通过抽象类实现

//抽象策略,该类中定义策略方法,供子类重定义
abstract class StrategyAbstract {
	//策略方法
	abstract void strategyMethod();
}
//具体策略方法,specific policy method
public class StrategyAbstractA extends StrategyAbstract{
	
	//具体的策略方法 specific method of strategy class
	@Override
	void strategyMethod() {
		System.out.println("this is StrategyAbstractA;");
	}

}
//this is specific implements class,provide specific policy method
public class StrategyAbstractB extends StrategyAbstract{

	@Override
	void strategyMethod() {
		// TODO Auto-generated method stub
		System.out.println("this is StrategyAbstractB;");
	}
	
}
/**
 * 环境类 context class (Environment)
 * @describe 这个类主要用于引用策略类,创建具体的策略对象,为客户端提供调用接口
 * This class is mainly used to reference policy classes;strategy
 * Create specific policy object
 * provide calling interface for client
 * @author 79197
 *
 */
public class ContextAbstract {
	
	StrategyAbstract strategyAbstractObj;

	public StrategyAbstract getStrategyAbstractObj() {
		return strategyAbstractObj;
	}
	public void setStrategyAbstractObj(StrategyAbstract strategyAbstractObj) {
		this.strategyAbstractObj = strategyAbstractObj;
	}
	
	public void strategyMethod() {
		strategyAbstractObj.strategyMethod();
	}
	
}

测试类

public class StrategyTest {

	public static void main(String[] args) {
		
		//通过接口的方式实现Implemented by interface class
		ContextInterface obj01=new ContextInterface();
		StrategyInterface objA=new StrategyA();
		obj01.setStrategyInterfaceObj(objA);
		obj01.strategyMethod();
		
		StrategyInterface objB=new StrategyB();
		obj01.setStrategyInterfaceObj(objB);
		obj01.strategyMethod();
		
		//通过抽象类实现 Implemented by abstract class
		ContextAbstract obj2=new ContextAbstract();
		StrategyAbstract objAbstractA=new StrategyAbstractA();
		obj2.setStrategyAbstractObj(objAbstractA);
		obj2.strategyMethod();
		
		StrategyAbstract objAbstractB=new StrategyAbstractB();
		obj2.setStrategyAbstractObj(objAbstractB);
		obj2.strategyMethod();
		
		
	}

}

相关文章

Linux连不上网?远程卡?这篇网络管理指南你不能错过!

大家好!今天咱们聊个所有Linux用户都躲不开的“老大难”——网络管理。我猜你肯定遇到过这些崩溃时刻:新装的Linux系统连不上Wi-Fi,急得直拍桌子;远程服务器SSH连不上,提示“Connecti...

如何用CHAT配置linux的远程连接?(linux 远程cp)

问CHAT:配置linux的远程连接 1.下载ssh 2.启动ssh服务 3.查看ssh服务状态 4.设置ssh服务开机自启动 5.设置windows的cmd下ssh 6.通过cmd的ssh命令远程到...

linux系统日志查看命令(linux系统查看日志的详细信息)

cattail -f日 志 文 件 说 明/var/log/message 系统启动后的信息和错误日志,是Red Hat Linux中最常用的日志之一/var/log/secure 与安全相关的日志信...

linux 查看磁盘信息的常用命令(linux中查看磁盘的命令)

在Linux系统中,排查磁盘问题是一个非常重要的任务,以下是一些常用的Linux排查磁盘问题的命令:df:查看磁盘分区的使用情况,可以查看磁盘分区的总容量、已用容量、可用容量等。du:查看文件或目录的...

linux安装FTP(linux安装ftp服务器的步骤)

1、 在nkftp目录下安装ftp,进入到nkftp里面[root@localhost bin]#cd /data/nkftp执行安装命令:[root@localhost nkftp]# rpm -i...

Linux常用文件操作命令(linux基本文件操作)

ls命令在Linux维护工作中,经常使用ls这个命令,这是最基本的命令,来写几条常用的ls命令。先来查看一下使用的ls版本 # ls --version ls (GNU coreutils) 8.4...