策略模式 strategy


策略模式:
定义算法族,分别封装起来,让他们可以互相替换,从而使算法的变化独立与算法的使用者。

类图


      比如说有一个类,需要用到排序,而排序方式自然是多种多样的,显然在这个类中直接用一个方法进行排序是不太好的做法,因为它不能做到在不同情况下使用不同的算法排序。我们怎样应对这中不同的需求呢。我们可以把不同的排序方法封装到不同的类(排序类)中,这些类实现一个共同的接口(排序接口)。  "使用排序的类"拥有"排序接口"类型的引用。在我们需要更换排序方法是我们可以是用一个Set方法把具体的排序对象与这个引用关联。

示例:
下面来做一个可以随意更换算法的Strategy,并且通过泛型可以支持所有数据类型的排序。

  class  Strategy < T >   where  T : IComparable
    
{
        ISort
<T> isort;
        T[] data;
        
public void Sort()
        
{
            isort.Sort(data);
        }

        
public Strategy(ISort<T> isort, T[] data)
        
{
            
this.isort = isort;
            
this.data = data;
        }


        
public void DoSomething()
        
{
            Console.WriteLine(
"I working now ..");
        }


        
public void Print()
        
{
            
foreach (T st in data)
            
{
                Console.Write(st 
+ "\t");
            }

            Console.WriteLine();
        }

        
public void SetSorter(ISort<T> isorter)
        
{
            
this.isort = isorter;
        }

    }

如果我通过Strategy 类本身来排序。那么我们必须定义多个不同名称的方法,不利于客户调用。并且一旦要使用新的算法都必须修改Strategy,也不能动态的改变算法。

  interface  ISort < T >   where  T:IComparable
    
{
        
void Sort(T[] t);
    }


class  SortC < T > :ISort < T >   where  T:IComparable
    
{
        
// 选择排序
        public  void Sort(T[] data)
        
{
            
for (int i = 0; i < data.Length; i++)
            
{
                
int lowIndex = i;
                
for (int j = data.Length - 1; j > i; j--)
                
{
                    
if (data[j] .CompareTo(data[lowIndex])==1)
                    
{
                        lowIndex 
= j;
                    }

                }

                Swaper
<T>.Swap(ref data[i],  ref data[lowIndex]);
            }

        }

    }

  class  Swaper < T >   where  T:IComparable
    
{
        
static T temp = default(T);
        
public static void Swap(ref T left ,ref T right)
        
{
            temp 
= left;
            left 
= right;
            right 
= temp;
        }

    }

下面两个是其他算法:
Code
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->  public class SortB<T> : ISort<T> where T : IComparable
    
{
        
public void Sort(T[] data)
        
{
            quickSort(data, 
0, data.Length - 1);
        }

        
private void quickSort(T[] data, int i, int j)
        
{
            
int pivotIndex = (i + j) / 2;
          
            Swaper
<T>.Swap( ref data[pivotIndex], ref data[j]);

            
int k = partition(data, i - 1, j, data[j]);
      
            Swaper
<T>.Swap(ref data[k], ref data[j]);

            
if ((k - i) > 1) quickSort(data, i, k - 1);
            
if ((j - k) > 1) quickSort(data, k + 1, j);

        }

        
private int partition(T[] data, int l, int r, T pivot)
        
{
            
do
            
{
                
while (data[++l].CompareTo(pivot)==1) ;
                
while ((r != 0&& data[--r].CompareTo( pivot)==-1) ;
              
                Swaper
<T>.Swap(ref data[l], ref data[r]);
            }

            
while (l < r);
            Swaper
<T>.Swap(ref data[l], ref data[r]);
            
return l;
        }


    }

 

Code
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> class SortA<T>:ISort<T> where T:IComparable
    
{
        
public void Sort(T[] t)
        
{
            
for (int i = 0; i < t.Length-1; i++)
            
{
                
for (int j = i; j < t.Length; j++)
                
{
                    
if (t[i].CompareTo(t[j]) == 1)
                    
{
                        Swaper
<T>.Swap(ref t[i], ref t[j]);
                    }

                }

            }

          
        }

    }

  static   void  Main( string [] args)
        
{
            
int[] t = new int[] 12,34,3543,5,454,4};

            Strategy
<int> strateg = new Strategy<int>(new SortA<int>(),t);
            Console.WriteLine(
"支持int排序的Strategy");
            strateg.Sort();
            strateg.Print();
            strateg.DoSomething();

            strateg.SetSorter(
new SortB<int>());
            strateg.Sort();
            strateg.Print();


            
//Console.WriteLine("支持double排序Strategy");
            
//double[] dt = new double[]{12.2,12.3,23.3,34,-1};
            
//Strategy<double> ds = new Strategy<double>(new SortA<double>(),dt);
            
//ds.Sort();
            
//ds.Print();
            
//strateg.DoSomething();
            
//ds.SetSorter(new SortB<double>());
            
//ds.Sort();
            
//ds.Print();
            Console.ReadLine();
        }

这样支持各种算法支持各种数据类型的排序做好了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值