java中的策略设计模式

时间:2020-02-23 14:37:25  来源:igfitidea点击:

在本教程中,我们将看到Java中的战略设计模式。

策略设计模式允许我们在运行时更改算法实现.Strategy设计模式提供多个算法和客户端可以根据其组合的帮助选择算法。

策略设计模式示例

让我们在简单的例子的帮助下了解这一点。
假设我们想要实现多个排序算法。
我们需要根据输入排序类型对列表进行排序(合并排序和快速排序)。
请注意,应提供将来添加新的排序类型。

创建一个名为"sortingtype.java"的枚举,如下所示。

package org.igi.theitroad.designpattern;
public enum SortingType {
	MERGE_SORT,QUICK_SORT;
}

创建名为sortingManager的服务类如下所示:

package org.igi.theitroad.designpattern;
 
import java.util.List;
 
public class SortingManager {
 
	List list;
	
	public SortingManager(List list) {
		super();
		this.list = list;
	}
 
	public void sortListBasedOnType(SortingType sortingType)
	{
		System.out.println("===================================");
		System.out.println("Sorting List based on Type");
		System.out.println("===================================");
		
		if("MERGE_SORT".equalsIgnoreCase(sortingType.toString()))
		{
			sortListUsingMergeSort();
		}
		else if("QUICK_SORT".equalsIgnoreCase(sortingType.toString()))
		{
			sortListUsingQuickSort();
		}
	}
	
	private void sortListUsingMergeSort()
	{
		System.out.println("Sorting List using merge sort");
	}
	
	private void sortListUsingQuickSort()
	{
		System.out.println("Sorting List using quick sort");
	}
}

创建一个名为"sortingmain.java"的主类,该类将调用sortingManager进行排序列表。

package org.igi.theitroad.designpattern;
 
import java.util.Arrays;
import java.util.List;
 
public class SortingMain {
 
	public static void main(String[] args) {
		List list = Arrays.asList(new Integer[]{44,5,3,5,5,64,3});
		
		SortingManager sm=new SortingManager(list);
 
		//Sorting using merge sort
		sm.sortListBasedOnType(SortingType.MERGE_SORT);
 
		System.out.println();
 
          //Sorting using quick sort
		sm.sortListBasedOnType(SortingType.QUICK_SORT);
 
	}
 
}

输出:

===================================
Sorting List based on Type
===================================
Sorting List using merge sort===================================
Sorting List based on Type
===================================
Sorting List using quick sort

正如我们所看到的,这是一个简单的代码,它正常工作。
我们已经测试了代码,并发现我们能够根据排序类型排序列表。

现在,我们需要创建一个更多的排序类型:eacp sort.if我们注意到,我们需要更改如下:1)我们需要在枚举排序类型中进行更改。

package org.igi.theitroad.designpattern;
public enum SortingType {
	MERGE_SORT,QUICK_SORT,HEAP_SORT;
}

2)我们需要在我们已经测试的SortingManager类中进行更改。

package org.igi.theitroad.designpattern;
 
import java.util.List;
 
public class SortingManager {
 
	List list;
	
	public SortingManager(List list) {
		super();
		this.list = list;
	}
 
	public void sortListBasedOnType(SortingType sortingType)
	{
		System.out.println("===================================");
		System.out.println("Sorting List based on Type");
		System.out.println("===================================");
		
		if("MERGE_SORT".equalsIgnoreCase(sortingType.toString()))
		{
			sortListUsingMergeSort();
		}
		else if("QUICK_SORT".equalsIgnoreCase(sortingType.toString()))
		{
			sortListUsingQuickSort();
		}
        else if("HEAP_SORT".equalsIgnoreCase(sortingType.toString()))
		{
			sortListUsingHeapSort();
		}
	}
	
	private void sortListUsingMergeSort()
	{
		System.out.println("Sorting List using merge sort");
	}
	
	private void sortListUsingQuickSort()
	{
		System.out.println("Sorting List using quick sort");
	}
    private void sortListUsingHeapSort()
	{
		System.out.println("Sorting List using heap sort");
	}
}

现在,我们可以使用如下堆栈的列表排序列表。

package org.igi.theitroad.designpattern;
 
import java.util.Arrays;
import java.util.List;
 
public class SortingMain {
 
	public static void main(String[] args) {
		List list = Arrays.asList(new Integer[]{44,5,3,5,5,64,3});
		
		SortingManager sm=new SortingManager(list);
 
		//Sorting using merge sort
		sm.sortListBasedOnType(SortingType.MERGE_SORT);
 
		System.out.println();
 
		//Sorting using quick sort
		sm.sortListBasedOnType(SortingType.QUICK_SORT);
                
                System.out.println();
 
         //Sorting using heap sort
		sm.sortListBasedOnType(SortingType.HEAP_SORT);
 
	}
 
}

输出:

===================================
Sorting List based on Type
===================================
Sorting List using merge sort===================================
Sorting List based on Type
===================================
Sorting List using quick sort===================================
Sorting List based on Type
===================================
Sorting List using heap sort

正如我们所看到的,我们必须在许多我们已经测试的地方修改,我们需要再次重新测试所有功能。

策略模式示例

让我们看看策略模式如何能够解决问题。
创建sortingmanager.java如下。

package org.igi.theitroad.strategy;
 
import java.util.List;
 
public class SortingManager {
 
	SortingStrategy sortingStrategy;
	List<Integer> list;
 
	public SortingManager(List<Integer> list,SortingStrategy sortingStrategy) {
		super();
		this.list = list;
		this.sortingStrategy=sortingStrategy;
	}
 
	public void sortList()
	{
		System.out.println("===================================");
		System.out.println("Sorting List based on Type");
		System.out.println("===================================");
 
		sortingStrategy.sort(list);
	}
 
	public SortingStrategy getSortingStrategy() {
		return sortingStrategy;
	}
 
	public void setSortingStrategy(SortingStrategy sortingStrategy) {
		this.sortingStrategy = sortingStrategy;
	}
 
	public List<Integer> getList() {
		return list;
	}
 
	public void setList(List<Integer> list) {
		this.list = list;
	}
 
	
}

创建一个名为sortingstrategy的接口,如下所示。

package org.igi.theitroad.strategy;
 
import java.util.List;
 
public interface SortingStrategy {
	void sort(List<Integer> list);
}

创建名为"Mergestrategy.java"的类,用于使用Merge排序进行排序。

package org.igi.theitroad.strategy;
 
import java.util.List;
 
public class MergeSortStrategy implements SortingStrategy {
 
	@Override
	public void sort(List list)
	{
		System.out.println("Sorting List using merge sort");
	}
 
}

创建名为"Quicksort Strategy.java"的类,用于使用快速排序进行排序。

package org.igi.theitroad.strategy;
 
import java.util.List;
 
public class QuickSortStrategy implements SortingStrategy {
 
	@Override
	public void sort(List list) {
		System.out.println("Sorting List using quick sort");
	}
 
}

现在让我们创建一个主类SortingMain.java。

package org.igi.theitroad.strategy;
 
import java.util.Arrays;
import java.util.List;
 
public class SortingMain {
 
	public static void main(String[] args) {
		List<Integer> list = Arrays.asList(new Integer[]{44,5,3,5,5,64,3});
		MergeSortStrategy mergeSortStrategy=new MergeSortStrategy();
		SortingManager sm=new SortingManager(list,mergeSortStrategy);
		sm.sortList();
		
		System.out.println();
		
		QuickSortStrategy quickSort=new QuickSortStrategy();
		sm.setSortingStrategy(quickSort);
		sm.sortList();
	}
}

输出:

===================================
Sorting List based on Type
===================================
Sorting List using merge sort===================================
Sorting List based on Type
===================================
Sorting List using quick sort

假设我们想要使用堆排序排序。
我们需要在下面创建以下更改:创建另一个名为"heapoportstrategy.java"的类。

package org.igi.theitroad.strategy;
import java.util.List;
 
public class HeapSortStrategy implements SortingStrategy {
	@Override
	public void sort(List list) {
		System.out.println("Sorting using heap sort");
	}
}

更改SortingMain.java以添加调用代码。

package org.igi.theitroad.strategy;
 
import java.util.Arrays;
import java.util.List;
 
public class SortingMain {
 
	public static void main(String[] args) {
		List<Integer> list = Arrays.asList(new Integer[]{44,5,3,5,5,64,3});
		MergeSortStrategy mergeSortStrategy=new MergeSortStrategy();
		SortingManager sm=new SortingManager(list,mergeSortStrategy);
		sm.sortList();
		
		System.out.println();
		
		QuickSortStrategy quickSort=new QuickSortStrategy();
		sm.setSortingStrategy(quickSort);
		sm.sortList();
		
		System.out.println();
		
		HeapSortStrategy heapSort=new HeapSortStrategy();
		sm.setSortingStrategy(heapSort);
		sm.sortList();
	}
}

输出:

===================================
Sorting List based on Type
===================================
Sorting List using merge sort===================================
Sorting List based on Type
===================================
Sorting List using quick sort===================================
Sorting List based on Type
===================================
Sorting using heap sort

正如我们所看到的,我们没有制作任何已测试的SortingManager。
我们只是添加了新的"Heaporort Strategy",它使我们能够使用堆排序。