That way I want to stay

BlogJava 首页 新随笔 联系 聚合 管理
  55 Posts :: 1 Stories :: 41 Comments :: 0 Trackbacks
  http://www.blogjava.net/Files/Wingel/第3章%20除去代码异味.rar
                第3章 除去代码异味 


    异味这个词,可能有点抽象,我们先看一下下面的例子

    这是一个CAD系统. 现在,它已经可以画三种形状了:线条,长方形,跟圆.
先认真的看一下下面的代码:

    class Shape {                                                                                  
       final static int TYPELINE = 0;                                                              
       final static int TYPERECTANGLE = 1;                                                         
       final static int TYPECIRCLE = 2;                                                            
       int shapeType;                                                                              
       //线条的开始点
       //长方形左下角的点
       //圆心
       Point p1;                                                                                   
       //线条的结束点
       //长方形的右上角的点
       //如果是圆的话,这个属性不用
       Point p2;                                                                                   
       int radius;                                                                                 
    }                                                                                              
    class CADApp {                                                                                 
       void drawShapes(Graphics graphics, Shape shapes[]) {                                        
           for (int i = 0; i < shapes.length; i++) {                                               
               switch (shapes[i].getType()) {                                                      
                  case Shape.TYPELINE:                                                             
                      graphics.drawLine(shapes[i].getP1(), shapes[i].getP2());                     
                      break;                                                                       
                  case Shape.TYPERECTANGLE:                                                        
                      //画四条边
                      graphics.drawLine(...);                                                      
                      graphics.drawLine(...);                                                      
                      graphics.drawLine(...);                                                      
                      graphics.drawLine(...);                                                      
                      break;                                                                       
                  case Shape.TYPECIRCLE:                                                           
                      graphics.drawCircle(shapes[i].getP1(), shapes[i].getRadius());               
                      break;                                                                       
               }                                                                                   
           }                                                                                       
       }                                                                                           
    }                                                                                              


    代码都是一直在改变的,而这也是上面的代码会碰到的一个问题.

    现在我们有一个问题: 如果我们需要支持更多的形状(比如三角形), 那么肯定要改动Shape这个类, CADApp里面的drawShapes这个方法也要改.
好,改为如下的样子:
  
......


    如果我们想要支持更多的图形(比如:三角形),上面没有一个类需要修改。我们只需要创建一个新的类Triangle就行了。

具体的内容在上面的下载链接里面的pdf文件里,看pdf比较舒服。

http://www.blogjava.net/Files/Wingel/第3章%20除去代码异味.rar
posted on 2006-11-27 21:23 Wingel 阅读(1991) 评论(3)  编辑  收藏 所属分类: 敏捷开发

Feedback

# re: 敏捷开发的必要技巧----消除代码异味 2006-11-28 10:36 Slash
好文啊  回复  更多评论
  

# re: 敏捷开发的必要技巧----消除代码异味 2006-11-28 15:02 peace
你发的是什么内容啊?  回复  更多评论
  

# re: 敏捷开发的必要技巧----消除代码异味 2006-11-28 15:04 Slash
rar里面是个pdf啊。
我发的是一本书  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: