2004年十二月月 发布的文章

学习重构

DMC采用驱动开发的方式,这就意味着重构“Refactoring”是我要学习的对象。早在大三的时候就已经把那本经典的“Refactoring Improving the Design of Existing Code”英文版买到手了,但就是在买回来后的第n天,它就被“打入冷宫”了。

* What Is Refactoring?
Refactoring is the art of safely improving the design of existing code. [1]

* 什么时候进行Refactoring?
在TDD开发中,当我们的应用代码顺利通过测试集后,在保持应用代码对外行为(behavior)保持不变的前提下,对应用代码进行Refactoring。

另外一种情况就是对测试集代码进行Refactoring。

* Refactoring vs Pattern
在[2]这本书中作者提到“我们应该把模式作为重构的目标,我们要渐进的引入模式”。原因是“有些人滥用模式,他们把任何一个需求都看成是模式的组合,他们一开始就用模式来进行设计,他们已经陷入到模式的滥用中”。

* Refactoring流程
在[1]中给出了一个程式化的refactoring流程:
While smells remain:
 - Choose the worst smell.
 - Select a refactoring that will address the smell.
 - Apply the refactoring.
注意:这里refactoring是个持续的过程,直到the code without smell

* Refactoring实践
Refactoring的例子很多,这里举一个常见的例子。

Considering the following code:
class Shape {
    //0 – Circle 1- Rectangle 2- Triangle
    private int type;
    public String getType(){
        switch(type){
            case 0:
                return “Circle”;
            case1:
                return “Rectangle”;
            case2:
                return “Triangle”;
            default:
                return “Unknown”;
        }
    }
}

我们按照Refactoring的流程,首先要发现代码中的bad smell。
 - 使用switch结构限制了Shape类的扩展,一旦要加入新的Shape类型,getType函数就得修改。
 - 还有一点是Shape使用private member variable type来区分不同的Shape类型,这样是不合理的,我们完全可以用”Polymorphism”来代替。

下面是重构后的代码:
public abstract class Shape{
    public abstract String getType();
}
public Circle extends Shape{
    public String getType(){
        return “Circle”;
}
}
public Rectangle extends Shape{
    public String getType(){
        return “Rectangle”;
}
}
public Circle extends Shape{
    public String getType(){
        return “Circle”;
}
}
Ok, now the code above feels nice.^_^
书还没看完,就先说到这了。

参考资料:
1、《Refactoring workbook》
2、《Test-Driven Development – A practical guide》

如何编写类中的setter和getter

在effective java中有一item叫”保护性拷贝”,今天又看了许多部门里的代码,发现很多代码都与该item“相违”,晚上和toidi_xu讨论这个问题有些收获。

Considering the following code:
public class Box {    
    private int length;
    public void setLength(int length){
        this.length = length;
    }        
    public int getLength(){
        return this.length;
    }
}

public class TestJava {
    private String id;
    private int idx;
    private Box box;
    public void setId(String id){
        this.id = id;
    }
    public String getId(){
        return this.id;
    }
    public void setIdx(int idx){
        this.idx = idx;
    }
    public int getIdx(){
        return this.idx;
    }
    public void setBox(Box box){
        this.box = box;
    }
    public Box getBox(){
        return this.box;
    }
    public static void main(String[] args) {
        TestJava tj = new TestJava();
        String id = "tony";
        int idx = 5;
        Box aBox = new Box();
        aBox.setLength(7);

        tj.setId(id);
        tj.setIdx(idx);
        tj.setBox(aBox);
        
        System.out.println(tj.getId());
        System.out.println(tj.getIdx());
        System.out.println(tj.getBox().getLength());

        id = "bai";
        idx = 6;
        aBox.setLength(8);
        System.out.println(tj.getId());
        System.out.println(tj.getIdx());
        System.out.println(tj.getBox().getLength());
    }
}
//output:
tony
5
7
tony
5
8

对于TestJava类中的3个类型(String, int, Box)的成员变量我们编写了相同的setter和getter,但结果是Box类型的成员变量居然不通过setBox就被修改了,而String和int类型在外部不能被修改。这是为什么呢。在effective java中曾经说过String类和Number类都是immutable的。任何对String or Number类对象的操作都会copy出一个不同于原object的object,而原来的object的状态并未被修改。在上面的例子中Box不是immutable class所以被外部修改了。

为了使client只能通过Box提供的setLength来修改,我们必须作保护性的copy。修改如下:
public void setBox(Box box){
    this.box = new Box();
    this.box.setLength(box.getLength());
}
public Box getBox(){
    Box aBox = new Box();
    aBox.setLength(this.box.getLength());
    return aBox;
}

修改后的output:
tony
5
7
tony
5
7

这样我们在TestJava中就出现两种setter和getter的样式,我们在写代码的时候该使用哪种呢?在实践中“为类中immutable class类型(如String和数值Number类)的field member写setter\getter时,我们不需要提供defensive copy;在为其它非immutable class类型(如上例中的Box类)的field member写setter\getter时,建议考虑defensive copy,以防止client对你的代码的恶意破坏”。所以上面的代码还有另一种改法就是将Box写成immutable class。

这篇blog就当作是对effective java中“defensive copy”一节的细化和补充吧。

如发现本站页面被黑,比如:挂载广告、挖矿等恶意代码,请朋友们及时联系我。十分感谢! Go语言第一课 Go语言精进之路1 Go语言精进之路2 商务合作请联系bigwhite.cn AT aliyun.com

欢迎使用邮件订阅我的博客

输入邮箱订阅本站,只要有新文章发布,就会第一时间发送邮件通知你哦!

这里是 Tony Bai的个人Blog,欢迎访问、订阅和留言! 订阅Feed请点击上面图片

如果您觉得这里的文章对您有帮助,请扫描上方二维码进行捐赠 ,加油后的Tony Bai将会为您呈现更多精彩的文章,谢谢!

如果您希望通过微信捐赠,请用微信客户端扫描下方赞赏码:

如果您希望通过比特币或以太币捐赠,可以扫描下方二维码:

比特币:

以太币:

如果您喜欢通过微信浏览本站内容,可以扫描下方二维码,订阅本站官方微信订阅号“iamtonybai”;点击二维码,可直达本人官方微博主页^_^:
本站Powered by Digital Ocean VPS。
选择Digital Ocean VPS主机,即可获得10美元现金充值,可 免费使用两个月哟! 著名主机提供商Linode 10$优惠码:linode10,在 这里注册即可免费获 得。阿里云推荐码: 1WFZ0V立享9折!


View Tony Bai's profile on LinkedIn
DigitalOcean Referral Badge

文章

评论

  • 正在加载...

分类

标签

归档



View My Stats