Strategy模式学习笔记

 

     定义一系列算法,把它们一个个封装起来,并且使它们可以互相替换.该模式使得算法可以独立于使用它的客户而变化 .

                                                                                    ----------<<设计模式>>GoF

 “Strategy模式”的出现,是为了提供一套相互之间可灵活替换的算法,在不影响上层接口的情况下,用户可以自由选择不同的算法完成逻辑。

    注意:这里的算法的替换是完全的算法的替换,也就是说对每一个封装的算法都可以独立去完成客户的任务,而不需要其他算法的协助.

在.NET Framwork中的ArrayList对象组合了IComparer,其实IComparer就是和Strategy.

我们可以根据需要自定义个排序的类,此类只需实现IComparer即可,这样客户代码就可以进行各个排序算法的相互切换.

 

using  System;
using  System.Collections ;

namespace  StrategyArrayListSort
{
    
/// <summary>
    
/// ComparePerson 的摘要说明。
    
/// </summary>

    public class ComparePersonCredit:IComparer
    
{
        
public ComparePersonCredit()
        
{
            
        }

        
IComparer 成员
    }

}

using  System;

namespace  StrategyArrayListSort
{
    
/// <summary>
    
/// Person 的摘要说明。
    
/// </summary>

    public class Person
    
{
        
private int credit;
        
private int age;
        
private string name;
        
private int account=0;

        
public Person(int credit,int age ,string name)
        
{
            
this.credit = credit;
            
this.age =age;
            
this.name =name;
        }

        
public int GetCredit()
        
{
            
return credit;
        }

        
public string GetName()
        
{
            
return name;
        }

        
public int GetAge()
        
{
            
return age;
        }

        
public void  LoanMoney()
        
{
            
if (credit >=100)
                account
+= 10000;
        }

    }

}

using  System;
using  System.Collections ;


namespace  StrategyArrayListSort
{
    
/// <summary>
    
/// Class1 的摘要说明。
    
/// </summary>

    class MainApp
    
{
        
/// <summary>
        
/// 应用程序的主入口点。
        
/// </summary>

        [STAThread]
        
static void Main(string[] args)
        
{
            ArrayList personList 
= new ArrayList();
            Person liu 
= new Person(100,23,"liu");
            Person yu 
= new Person(120,24,"yu");
            Person ma 
= new Person(90,22,"ma");
            Person su 
= new Person(100,25,"su");

            personList.Add(liu);
            personList.Add(yu);
            personList.Add(ma);
            personList.Add(su);

            personList.Sort(
new ComparePersonCredit());
            
//personList.Sort(new ComparePersonAge());

            
foreach ( Person person in personList)
            
{
                Console.WriteLine(person.GetName());
            }

            Console.ReadLine();

        }

    }

}

 

由上代码可以看出,我们还可以去定义个类ComparePersonAge来比较Person,客户代码可以很方便的去替换掉原来的排序算法ComparePersonCredit.

Strategy模式的几个要点:

Strategy及其子类为组件提供了一系列可重用的算法,从而可以使得类型在运行时方便地根据需要在各个算法之间进行切换,所谓算法的封装,支持算法的变化.

Strategy模式消除了条件判断语句,也就是在解耦合.

一般含有条件判断的地方就需要Strategy模式去解决变化.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值