I. purpose and requirements of the experiment
1. Master the concept of behavioral model.
2. Master the construction and usage of memo mode, observer mode, state mode, strategy mode, template mode and visitor mode.
II. Experimental equipment (environment)
1. Software requirements: Dev-Cpp5.4, Rational Rose / Microsoft Visio
2. Hardware requirements: CPU processor above Pentium III 450, memory above 1G, free hard disk space of 2G
III. experiment content
1. A system provides a class to operate on array data, which encapsulates common operations on array, such as finding array elements, sorting array elements, etc. Now taking the sorting operation as an example, the array operation class is designed by using the policy mode, so that the client can dynamically change the sorting algorithm, select bubble sorting or select sorting or insert sorting according to the needs, and also can flexibly add new sorting algorithm.
IV. experimental steps and results
Exercise 1
1. UML diagram of policy pattern design structure:
2. Screenshot of experimental results:
3. Code analysis
Context.h
#ifndef _CONTEXT_H_ #define _CONTEXT_H_ class Strategy; /** *This class is the key to the Strategy pattern and the fundamental difference between the Strategy pattern and the Template pattern. *Strategy The heterogeneous algorithm (Implementation) is realized by "composition" (delegation), while the Template pattern is inherited *The difference between these two patterns is also the difference between inheritance and combination of two ways to realize interface reuse */ class Context { public: Context(Strategy* stg); ~Context(); void DoAction(int arr[],int n); protected: private: Strategy* _stg; }; #endif //~_CONTEXT_H_
Context.cpp
#include "Context.h" #include "Strategy.h" #include <iostream> using namespace std; Context::Context(Strategy* stg) { _stg = stg; } Context::~Context() { if (!_stg) delete _stg; } void Context::DoAction(int arr[],int n) { _stg->AlgrithmInterface(arr,n); }
Strategy.h
#ifndef _STRATEGY_H_ #define _STRATEGY_H_ class Strategy { public: Strategy(); virtual ~Strategy(); virtual void AlgrithmInterface(int arr[], int n) = 0; protected: private: }; class bubbleSort:public Strategy { public: bubbleSort(); virtual ~bubbleSort(); void AlgrithmInterface(int arr[], int n); protected: private: }; class choiceSort:public Strategy { public: choiceSort(); virtual ~choiceSort(); void AlgrithmInterface(int arr[], int n); protected: private: }; class insertSort:public Strategy { public: insertSort(); virtual ~insertSort(); void AlgrithmInterface(int arr[], int n); protected: private: }; #endif //~_STRATEGY_H_
Strategy.cpp
#include "Strategy.h" #include <iostream> using namespace std; Strategy::Strategy() { } Strategy::~Strategy() { cout<<"~Strategy....."<<endl; } void Strategy::AlgrithmInterface(int arr[], int n) { } bubbleSort::bubbleSort() { } bubbleSort::~bubbleSort() { cout<<"~bubbleSort....."<<endl; } void bubbleSort::AlgrithmInterface(int arr[], int n) { //Bubble sort for(int i = 0;i < n-1;i++){ //Compare two adjacent elements for(int j = 0;j < n-i-1;j++){ if(arr[j] > arr[j+1]){ int t = arr[j]; arr[j] = arr[j+1]; arr[j+1] = t; } } } for(int i=0;i<n;i++) { cout<<arr[i]<<' '; } cout<<"bubble sort "<<endl; } choiceSort::choiceSort() { } choiceSort::~choiceSort() { cout<<"~choiceSort....."<<endl; } void choiceSort::AlgrithmInterface(int arr[], int n) { //Selection sort for(int i = 0;i < n-1; i++){ int m = i; for(int j = i + 1;j < n;j++){ //If the j-th element is smaller than the m-th element, assign J to M if(arr[j] < arr[m]){ m = j; } } //Exchange the positions of m and i elements if(i != m){ int t = arr[i]; arr[i] = arr[m]; arr[m] = t; } } for(int i=0;i<n;i++) { cout<<arr[i]<<' '; } cout<<"Selective sorting"<<endl; } insertSort::insertSort() { } insertSort::~insertSort() { cout<<"~insertSort....."<<endl; } void insertSort::AlgrithmInterface(int arr[], int n) { //Insertion sort for(int i = 1;i < n-1;i++){ int temp = arr[i]; int j = i - 1; while(temp < arr[j]){ arr[j+1] = arr[j]; j--; if(j == -1){ break; } } arr[j+1] = temp; } for(int i=0;i<n;i++) { cout<<arr[i]<<' '; } cout<<"Insert sort"<<endl; }
main.cpp
#include "Context.h" #include "Strategy.h" #include <iostream> using namespace std; int main(int argc,char* argv[]) { int i; int a[] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 10}; Strategy* psA; psA = new bubbleSort(); Context* pcA = new Context(psA); pcA->DoAction(a,10); if (NULL != pcA) delete pcA; Strategy* psB; psB = new choiceSort(); Context* pcB = new Context(psB); pcB->DoAction(a,10); if (NULL != pcB) delete pcB; Strategy* psC; psC = new insertSort(); Context* pcC = new Context(psC); pcC->DoAction(a,10); if (NULL != pcC) delete pcC; return 0; }