diff --git a/22.StrategyPattern/1.Picture/策略模式UML图.png b/22.StrategyPattern/1.Picture/策略模式UML图.png new file mode 100644 index 0000000..fb9a11a Binary files /dev/null and b/22.StrategyPattern/1.Picture/策略模式UML图.png differ diff --git a/22.StrategyPattern/1.Picture/策略模式实例UML图.png b/22.StrategyPattern/1.Picture/策略模式实例UML图.png new file mode 100644 index 0000000..d42cf66 Binary files /dev/null and b/22.StrategyPattern/1.Picture/策略模式实例UML图.png differ diff --git a/22.StrategyPattern/2.Code/Context.h b/22.StrategyPattern/2.Code/Context.h new file mode 100644 index 0000000..a2923ef --- /dev/null +++ b/22.StrategyPattern/2.Code/Context.h @@ -0,0 +1,44 @@ +#ifndef __CONTEXT_H__ +#define __CONTEXT_H__ + +#include "Strategy.h" +#include + +// +class Context +{ +public: + Context(){ + arr = NULL; + N = 0; + } + Context(int iArr[], int iN){ + this->arr = iArr; + this->N = iN; + } + void setSortStrategy(Strategy* iSortStrategy){ + this->sortStrategy = iSortStrategy; + } + void sort(){ + this->sortStrategy->sort(arr, N); + printf(" "); + this->print(); + } + void setInput(int iArr[], int iN){ + this->arr = iArr; + this->N = iN; + } + void print(){ + for (int i = 0; i < N; i++){ + printf("%3d ", arr[i]); + } + printf("\n"); + } + +private: + Strategy* sortStrategy; + int* arr; + int N; +}; + +#endif // __CONTEXT_H__ \ No newline at end of file diff --git a/22.StrategyPattern/2.Code/Strategy.h b/22.StrategyPattern/2.Code/Strategy.h new file mode 100644 index 0000000..c610fd4 --- /dev/null +++ b/22.StrategyPattern/2.Code/Strategy.h @@ -0,0 +1,87 @@ +#ifndef __STRATEGY_H__ +#define __STRATEGY_H__ + +#include + +// +class Strategy +{ +public: + Strategy(){} + virtual void sort(int arr[], int N) = 0; +}; + +// ԣð +class BubbleSort :public Strategy +{ +public: + BubbleSort(){ + printf("ð\n"); + } + void sort(int arr[], int N){ + for (int i = 0; iarr[j + 1]){ + int tmp = arr[j]; + arr[j] = arr[j + 1]; + arr[j + 1] = tmp; + } + } + } + } +}; + +// ԣѡ +class SelectionSort :public Strategy +{ +public: + SelectionSort(){ + printf("ѡ\n"); + } + void sort(int arr[], int N){ + int i, j, k; + for (i = 0; i= 0; j--) + { + if (arr[i]>arr[j]){ + break; + } + } + int temp = arr[i]; + for (int k = i - 1; k > j; k--){ + arr[k + 1] = arr[k]; + } + arr[j + 1] = temp; + } + } +}; + +#endif \ No newline at end of file diff --git a/22.StrategyPattern/2.Code/main.cpp b/22.StrategyPattern/2.Code/main.cpp new file mode 100644 index 0000000..01f3bfd --- /dev/null +++ b/22.StrategyPattern/2.Code/main.cpp @@ -0,0 +1,28 @@ +#include "Context.h" +#include +#include + +int main() +{ + Context* ctx = new Context(); + int arr[] = { 10, 23, -1, 0, 300, 87, 28, 77, -32, 2 }; + ctx->setInput(arr, sizeof(arr)/sizeof(int)); + printf("룺"); + ctx->print(); + + // ð + ctx->setSortStrategy(new BubbleSort()); + ctx->sort(); + + // ѡ + ctx->setSortStrategy(new SelectionSort()); + ctx->sort(); + + // + ctx->setSortStrategy(new InsertSort()); + ctx->sort(); + + printf("\n\n"); + system("pause"); + return 0; +} \ No newline at end of file diff --git a/README.md b/README.md index a352a24..1ab3adb 100644 --- a/README.md +++ b/README.md @@ -97,4 +97,8 @@ Jungle设计模式系列 24.设计模式(二十四)——状态模式 -博客地址:https://blog.csdn.net/sinat_21107433/article/details/102966121 \ No newline at end of file +博客地址:https://blog.csdn.net/sinat_21107433/article/details/102966121 + +25.设计模式(二十五)——策略模式 + +博客地址:https://blog.csdn.net/sinat_21107433/article/details/102984862 \ No newline at end of file