策略模式 C++ 實現

#include<iostream>
#include<algorithm>
#include<string>
/*
  問題:和工廠模式的本質區別? 
*/ 
using namespace std;
/*abstract sort class --- strategy class*/
class super_sort
{ public:
  typedef int ElemType;
  void swap(ElemType *a,ElemType*b)
      {
        ElemType tmp;
        tmp=*a;
        *a=*b;
        *b=tmp;
       }
        
    
  virtual void sort(int *a,int l, int r)
  { }    
};

/* concrete sort --- concret strategy */

/*quicksort*/
class quick_sort: public super_sort
{ private: 
  typedef int ElemType;

int partition(int *a,int l,int r)
{ElemType v=a[r];
 int i=l-1;int j=r;
 while(i<j)
 {
 while(a[++i]>v&&i<r) ;
 while(j>l&&a[--j]<v) ;
 if(i>=j)  
   break;
  swap(&a[i],&a[j]);
 }
  swap(&a[i],&a[r]);
  
 return i;
}

void quicksort(int *a,int l,int r)
{ int i;
  if(l>=r) return;
  i=partition(a,l,r);
  quicksort(a,l,i-1);
  quicksort(a,i+1, r);
   
}
 public: 
  void sort(int *a, int l, int r)
  {
    quicksort(a,l,r);
         
  }    
    
};

/*insert sort*/ 

class insert_sort: public super_sort
{
  private:
  typedef int ElemType;
  void insertsort(ElemType *a,int l,int r) 
  {
    int i;
     for(i=1;i<r;i++)
	    if(a[i]<a[0]) swap(&a[0],&a[i]);
	     
	    for(i=l+2;i<r;i++)
	    {
	      int j=i;ElemType v=a[i];
		  while(v<a[j-1])
		  {a[j]=a[j-1];j--;}
		   a[j]=v;
	  
	     }
  }
  
  public:
        void sort(int *a,int l, int r)
        {
            insertsort(a,l,r);
        }  
};

/*direct sort*/

class direct_sort: public super_sort
{
  private:
void selectsort(int *a,int l,int r)
{int i,j;
 int flag=1;
  for(i=l;i<r;i++)
 {   int min=i;
	 for(j=i+1;j<10;j++)
	 {   
		 if(a[j]<a[min])
               min=j;
			   
	 }
	 swap(&a[i],&a[min]);
	 
 }
}
 
 public:
     void sort(int *a, int l, int r)
     {
          selectsort(a,l,r);  
     }    
            
};

/*context class*/

class context
{ 
  private:
  super_sort *my_sort;
  public:
     context(){};   
     void select_sort(int num,int *a, int l, int r)
     {
       switch(num)
       {
         case 1:
                my_sort = new quick_sort();
                break;    
        case 2:
            my_sort = new insert_sort();
            break;
        case 3:
            my_sort = new direct_sort();
            break;
        
        default:
            cout << "Erro input, please check. " << endl;
            
        }
        my_sort->sort(a, l, r); 
     }
     
    void sort_list()
    {
      cout <<"1 -------- quicksort ---------"<< endl;    
      cout <<"2 -------- insertsort ---------"<< endl;
      cout <<"3 -------- directsort ---------"<< endl;
      cout << "Input your choice: "<< endl;
    }
};

/*
   策略模式 和 簡單工廠模式的 區別:
   簡單工廠模式讓客戶端識別兩個類,而策略模式只讓客戶端識別一個類;
   耦合更加低。
               
*/

/*client class*/
int main()
{   int num;
    int a[10] = {1,20,12,0,4,54,22,11,78,8};
    context my_selection;
    my_selection.sort_list();
    cin >> num;
    my_selection.select_sort(num,a,0,9);
    cout <<"-------- result ---------"<< endl;
    for(int i = 0; i < 10; i++)
      cout <<a[i]<< endl;
    
   system("pause"); 
   return 0;    
}

總結:無。
發佈了53 篇原創文章 · 獲贊 5 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章