#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;
}