策略模式 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;    
}

总结:无。

原文链接: https://www.cnblogs.com/snake-hand/archive/2013/06/12/3132925.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    策略模式 C++ 实现

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/91946

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月10日 上午1:25
下一篇 2023年2月10日 上午1:26

相关推荐