策略模式+简单工厂使用排序算法(c++实现)

#include<iostream>
#include<vector>
#include<string>
using namespace std;

//抽象排序算法   //当成strategy来使用
class AbstractSort {
private:
	vector<int> nums;
public:
	AbstractSort(vector<int> _nums) {

		this->nums = _nums;
	}
	AbstractSort() {};
	virtual ~AbstractSort() {};
	virtual void sort() = 0;

	void print() {
		int n = nums.size();
		for (int i = 0; i < n; i++) {
			cout << nums[i] << " ";
		}
		cout << endl;
	}
	void setNums(vector<int> nums) {
		this->nums = nums;
	}
	vector<int> getNums() {
		return this->nums;
	}
};
//冒泡排序 继承自抽象排序
class BubbleSort :public AbstractSort {
public:
	BubbleSort(vector<int> _nums) :AbstractSort(_nums) {};
	~BubbleSort() {};
	void sort() {
		vector<int> nums = AbstractSort::getNums();
		bubbleSort(nums);
		AbstractSort::setNums(nums);
	}
	void bubbleSort(vector<int> &nums) {
		int n = nums.size();
		for (int i = 0; i < n; i++) {
			bool isSort = true;

			for (int j = 0; j < n - i - 1; j++) {
				if (nums[j] > nums[j + 1]) {
					swap(nums[j], nums[j + 1]);
					isSort = false;
				}
			}
			if (isSort) break;
		}
	}
};
//插入排序 继承自抽象排序
class InsertSort :public AbstractSort {
private:

public:
	InsertSort(vector<int> _nums) :AbstractSort(_nums) {}
	~InsertSort() {}

	void sort() {

		vector<int> nums = AbstractSort::getNums();
		insertSort(nums);
		AbstractSort::setNums(nums);
	}
	void insertSort(vector<int> &nums) {
		int n = nums.size();
		for (int i = 0; i < n; i++) {
			int val = nums[i];
			int j = 0;
			for (j = i - 1; j >= 0 and nums[j] > val; j--) {
				nums[j + 1] = nums[j];
			}
			nums[j + 1] = val;
		}
	}
};
//选择排序 继承自抽象排序
class SelectionSort :public AbstractSort {

public:
	SelectionSort(vector<int> _nums) :AbstractSort(_nums) {};
	~SelectionSort() {};

	void sort() {
		vector<int> nums = AbstractSort::getNums();
		selectionSort(nums);
		AbstractSort::setNums(nums);
	}
	void selectionSort(vector<int> &nums) {
		int n = nums.size();
		for (int i = 0; i < n; i++) {
			int min = i;
			for (int j = i + 1; j < n; j++) {
				if (nums[j] < nums[min]) min = j;
			}
			swap(nums[i], nums[min]);
		}
	}
};

//简单工厂
class SortSimpleFactory {
public:
	static AbstractSort *CreateSortMethod(string flag, vector<int> nums) {
		if (flag == "BubbleSort") {
			return new BubbleSort(nums);
		}
		
		else if (flag == "InsertSort") {
			return new InsertSort(nums);
		}
		
		else if (flag == "SelectionSort") {
			return new SelectionSort(nums);
		}
		else {
			return NULL;
		}
	}
};
//简单工厂和策略模式的结合
class SortContex {
	AbstractSort *SortStrategy = NULL;
public:
	SortContex(string flag,vector<int> nums) {
		if (flag == "BubbleSort") {
			SortStrategy = new BubbleSort(nums);
		}
		
		else if (flag == "InsertSort") {
			SortStrategy = new InsertSort(nums);
		}
	
		else if (flag == "SelectionSort") {
			SortStrategy = new SelectionSort(nums);
		}
	}
	void sort() {
		SortStrategy->sort();
	}
	void print(){
		SortStrategy->print();
	}
};

//简单工厂测试
//在简单工厂模式下 客户端需要认识两个类
//一个是工厂类 一个是抽象排序类
void testSortSimpleFactory(vector<int> nums) {
	SortSimpleFactory *factory = new  SortSimpleFactory;
	AbstractSort *sort = NULL;

	vector<string> useSort = { "BubbleSort","InsertSort" ,"SelectionSort" };
	for (string s : useSort) {
		cout << s << ":" << endl;
		sort = factory->CreateSortMethod(s, nums);
		sort->print();
		sort->sort();
		sort->print();
		cout << endl;
		delete sort;
	}
	delete factory;
}
/*
	策略模式和简单工厂的结合
	客户端只需要认识一个类SortContex
	实例化一个SortContex 然后调用其sort和print方法就可以使用
	降低耦合
*/
void testStrategy(vector<int> nums) {
	
	vector<string> useSort = { "BubbleSort","InsertSort" ,"SelectionSort" };
	for (string s : useSort) {
		cout << s << ":" << endl;
		SortContex *sort = new SortContex(s,nums);
		sort->print();
		sort->sort();
		sort->print();
		cout << endl;
		delete sort;
	}
}


int main()
{
	cout << "input the length of nums: ";
	int n;
	cin >> n;
	cout << endl;
	vector<int> nums;
	//随机数放入nums中
	for (int i = 0; i < n; i++) {
		nums.push_back(rand() % n);
	}
	testSortSimpleFactory(nums);		//简单工厂模式测试
	testStrategy(nums);
	system("pause");
	return 0;
}


 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值