1. 程式人生 > >設計模式與XML(四)策略模式(C++)

設計模式與XML(四)策略模式(C++)

一、實驗目的及要求

1、掌握行為型模式的概念。

2、掌握備忘錄模式、觀察者模式、狀態模式、策略模式、模板模式、訪問者模式的構造方式及使用情景。

二、實驗裝置(環境)

   1、   軟體需求: Dev-Cpp5.4, Rational Rose / Microsoft Visio

2、   硬體需求: Pentium III 450以上的CPU處理器,1G以上的記憶體,2G的自由硬碟空間

三、實驗內容

1、某系統提供了一個用於對陣列資料進行操作的類,該類封裝了對陣列的常見操作,如查詢陣列元素、對陣列元素進行排序等。現以排序操作為例,使用策略模式設計該陣列操作類,使得客戶端可以動態地更換排序演算法,可以根據需要選擇氣泡排序或選擇排序或插入排序,也能夠靈活地增加新的排序演算法。

四、實驗步驟與結果

練習一

1.策略模式設計結構圖UML圖:

2.實驗結果截圖:

3.程式碼分析

Context.h

#ifndef _CONTEXT_H_
#define _CONTEXT_H_
class Strategy;
/**
*這個類是Strategy模式的關鍵,也是Strategy模式和Template模式的根本區別所在。
*Strategy通過“組合”(委託)方式實現演算法(實現)的異構,而Template模式則採取的是繼承的方式
*這兩個模式的區別也是繼承和組合兩種實現介面重用的方式的區別
*/
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)
{
	//氣泡排序
	for(int i = 0;i < n-1;i++){  
		//比較兩個相鄰的元素   
		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<<"氣泡排序法"<<endl;  
}
choiceSort::choiceSort()
{
}
choiceSort::~choiceSort()
{
cout<<"~choiceSort....."<<endl;
}
void choiceSort::AlgrithmInterface(int arr[], int n)
{
    //選擇排序 
	for(int i = 0;i < n-1; i++){  
		int m = i;  
        for(int j = i + 1;j < n;j++){  
            //如果第j個元素比第m個元素小,將j賦值給m   
			if(arr[j] < arr[m]){  
                m = j;  
            }  
        }  
        //交換m和i兩個元素的位置   
		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<<"選擇排序法"<<endl;

}
insertSort::insertSort()
{
}
insertSort::~insertSort()
{
cout<<"~insertSort....."<<endl;
}
void insertSort::AlgrithmInterface(int arr[], int n)
{

    //插入排序
	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<<"插入排序法"<<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;

}