using System;
namespace Pattern
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public abstract class strategy
{
protected string m_oldstr="";
protected string m_newstr="";
public void setOldString(string oldstr)
{
this.m_oldstr =oldstr;
}
public string getNewString()
{
return this.m_newstr;
}
public abstract void replace();
}
public class repA:strategy
{
public repA()
{
}
public override void replace()
{
this.m_newstr =this.m_oldstr+"aaaa";
}
}
public class repB:strategy
{
public repB()
{
}
public override void replace()
{
this.m_newstr =this.m_oldstr+"bbbb";
}
}
}
// 策略模式
// strategy r1=new repA();
// r1.setOldString("good");
// r1.replace();
// Console.WriteLine(r1.getNewString());
//
// repB r2=new repB();
// r2.setOldString("good");
// r2.replace();
// Console.WriteLine(r2.getNewString());
博客展示了策略模式的代码实现。定义了抽象类strategy及两个具体实现类repA和repB,分别实现不同替换逻辑。通过创建实例设置旧字符串、执行替换操作并输出新字符串,体现策略模式的应用。

被折叠的 条评论
为什么被折叠?



