策略模式 C++ 实现

策略模式 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://javaforall.net/110104.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • UVALive 3135–Argus+自己定义优先队列的优先规则「建议收藏」

    UVALive 3135–Argus+自己定义优先队列的优先规则

    2022年2月6日
    55
  • u8和u16是什么意思_u8在哪里声明的

    u8和u16是什么意思_u8在哪里声明的unsignedint32(C语言标准表达方法)2.uint32_t;3.u32;这三种表达式是同一个意思,只是在不用的版本当中,为了兼容旧版本而出现这么多的表达方式。但不管怎么变化,都是基于标准c。主要用处是为了在定义数据类型的时候少写几个符号。unsignedchar=uint8_t=u8unsignedshortint=…

    2022年10月15日
    2
  • mac如何安装pip_mac怎么卸载python

    mac如何安装pip_mac怎么卸载pythonmac下直接安装pip和卸载pip的方法如下:1、pip的安装:输入sudoeasy_installpip就可以安装pip了。验证pip安装是否成功:输入:pip结果找不到文件。尝试输入:pip3-V或者pip3则说明已经安装成功了。2、pip的卸载:输入命令:sudopipuninstallpip然后输入密码后如下图:输入y,回车。即要卸载pip,再次用pip-V或者pip显示已经找不到文件,即卸载成功了。卸载不再做图片演示。注意:输入p

    2022年10月7日
    2
  • js中document.getElementById()用法「建议收藏」

    js中document.getElementById()用法「建议收藏」dom标准里面的 获取当前文档中指定id的元素if(document.getElementById(“regjm”).value!=document.getElementById(“regjm1”).value){  alert(“提示:请输入有效的认证码”);  document.getElementById(“regjm1”).focus();  retur

    2022年7月15日
    24
  • idea2021.11激活[最新免费获取]

    (idea2021.11激活)好多小伙伴总是说激活码老是失效,太麻烦,关注/收藏全栈君太难教程,2021永久激活的方法等着你。https://javaforall.net/100143.htmlIntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,上面是详细链接哦~6EK6WKOHUX-eyJsaWNlbnNlSWQiOi…

    2022年3月28日
    52
  • 初识.Net审计

    初识.Net审计前言对.net认知比较少,学习一下.net的一些简单审计。遇到.net源码能简单审审。基础概念文件类型ASPX.cs是页面后的代码,aspx负责显示,服务器端的动作就是在as

    2021年12月13日
    44

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号