首页 > 开发 > .Net > 正文

一次重构导向设计模式的实践(.NET)

2020-02-03 15:56:32
字体:
来源:转载
供稿:网友
  • 本文来源于网页设计爱好者web开发社区http://www.html.org.cn收集整理,欢迎访问。
  • 代码仅仅是说明问题,和实际的有所不同
    在项目开发过程中,有这样的需求:定义一个查询窗体使用datagrid显示列表
    双击grid后打开指定记录的编辑页面,窗体类为formsearchentity于是这么写了
    private void grid_doubleclick(object sender,system.eventargs e)
    {
    string entityid = 双击记录的id字段值; //这个有固定的办法
    formentity frmentity = new formentity(entityid);
    ........
    frmentity.show();
    }
    其中的formentity就是对业务实体的编辑界面,在构造函数中传入一个id,然后
    加载该记录的相关数据,在这里不作重点解释。

    接下来有要在查询界面上添加一个按钮“go”,执行的动作和grid双击是一样的,就是
    在grid中选中记录,点击go打开实体的操作界面。

    这样,就使用重构中的extract method手法:
    private void grid_doubleclick(object sender,system.eventargs e)
    {
    string entityid = 双击记录的id字段值;
    openentityform(entityid);

    }
    private void btngo_click(object sender,system.eventargs e )
    {
    string entityid = 双击记录的id字段值;
    openentityform(entityid);
    }
    private void openentityform(string entityid)
    {
    formentity frmentity = new formentity(entityid);
    ........
    frmentity.show();
    }

    到现在看来,这样作有什么用呢?直接在go的click时间中调用grid的doubleclick不就行了吗?事实上extract method不仅仅是防止重复代码
    同时也可以提高代码的可重用性,作用在下面会看到。

    现在,又要对另一个的表进行同样的操作,那就再定义一个窗体,把上面的代码改改就成了,但是就出现了重复代码,这是不好的味道。那么这样作:把openentityform方法改为virtual,同时声明为protected,里面的代码都去掉
    protected void openentityform(string entityid)
    {

    }
    把窗体更名为formsearchentitybase再重新写一个类formsearchentitya来继承formsearchentitybase,override父类的openentityform方法

    protected override void openentityform(string entityid)
    {
    formentitya frmentitya = new formentitya(entityid);
    ........
    frmentitya.show();
    }

    实体b的查询界面也一样formsearchentityb继承自formsearchentitybase,override父类的openentityform方法
    protected override void openentityform(string entityid)
    {
    formentityb frmentityb = new formentityb(entityid);
    ........
    frmentityb.show();
    }
    这样,如果后面还有相同的需求,就从formsearchentitybase继承一个类,override父类的openentityform方法就可以了

    现在,来看看templatemethod模式
    意图:
    定义一个操作中的算法的骨架,而将一些步骤延迟到子类中。
    适用性:
    一次性实现一个算法的不变的部分,并将可变的行为留给子类来实现。
    各子类中公共的行为应被提取出来并集中到一个公共父类中以避免代码重复
    控制子类扩展

    例子代码:
    namespace templatemethod_designpattern
    {
    using system;

    class algorithm
    {
    public void doalgorithm()
    {
    console.writeline("in doalgorithm");

    // do some part of the algorithm here

    // step1 goes here
    console.writeline("in algorithm - doalgostep1");
    // . . .

    // step 2 goes here
    console.writeline("in algorithm - doalgostep2");
    // . . .

    // now call configurable/replacable part
    doalgostep3();

    // step 4 goes here
    console.writeline("in algorithm - doalgostep4");
    // . . .

    // now call next configurable part
    doalgostep5();
    }

    virtual public void doalgostep3()
    {
    console.writeline("in algorithm - doalgostep3");
    }

    virtual public void doalgostep5()
    {
    console.writeline("in algorithm - doalgostep5");
    }
    }

    class customalgorithm : algorithm
    {
    public override void doalgostep3()
    {
    console.writeline("in customalgorithm - doalgostep3");
    }

    public override void doalgostep5()
    {
    console.writeline("in customalgorithm - doalgostep5");
    }
    }

    /// <summary>
    /// summary description for client.
    /// </summary>
    public class client
    {
    public static int main(string[] args)
    {
    customalgorithm c = new customalgorithm();

    c.doalgorithm();

    return 0;
    }
    }
    }


    再来对比下上面对窗体类进行改动的代码和templatemethod的例子代码,这就是一个templatemethod模式了

    有一种观点说在设计期使用设计模式常会导致过渡设计,目前的敏捷方法和重构都渐渐提倡代码演化和重构达到设计模式。
    我也是在写完代码后才发现这已经是一个模式了。


    发表评论 共有条评论
    用户名: 密码:
    验证码: 匿名发表