Chinaunix首页 | 论坛 | 博客
  • 博客访问: 499790
  • 博文数量: 1496
  • 博客积分: 79800
  • 博客等级: 大将
  • 技术积分: 9940
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-09 13:22
文章分类

全部博文(1496)

文章存档

2011年(1)

2008年(1495)

我的朋友

分类:

2008-09-09 13:26:07

摘要:
本文通过简单通俗的例子, 告诉我们如何判断代码的稳定性和代码中的异类, 并且如何重构此类代码.

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

这是一个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这个方法也要改.
好,改为如下的样子:
    
class Shape {      
       final static int TYPELINE = 0;
       final static int TYPERECTANGLE = 1;
       final static int TYPECIRCLE = 2;
       final static int TYPETRIANGLE = 3;
       int shapeType;  
       Point p1;       
       Point p2;       
       //三角形的第三个点.
       Point p3;       
       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;
                case Shape.TYPETRIANGLE:
                    graphics.drawLine(shapes[i].getP1(), shapes[i].getP2());
                    graphics.drawLine(shapes[i].getP2(), shapes[i].getP3());
                    graphics.drawLine(shapes[i].getP3(), shapes[i].getP1());
                    break;
               }       
           }           
       }               
  }
                  

    如果以后要支持更多的形状,这些类又要改动……,这可不是什么好事情!
   理想情况下,我们希望当一个类,一个方法或其他的代码设计完以后,就不用再做修改了。它们应该稳定到不用修改就可以重用。 
    现在的情况恰好相反!
    每当我们增加新的形状,都得修改Shape这个类,跟CADApp里面的drawShapes方法。

    怎么让代码稳定(也就是无需修改)?这个问题是个好问题!不过老规矩,先不说,我们以行动回答。
    我们先看看另外一个方法: 当给你一段代码,你怎么知道它是稳定的?


 

[1]    

【责编:Kittoy】

--------------------next---------------------

阅读(128) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~