科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道基础软件敏捷开发的必要技巧:慎用继承(2)

敏捷开发的必要技巧:慎用继承(2)

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

如果还不清楚的话,我们就这样举例吧,比如,现在我们已经有了2000个参会者,在两个数据库中存放,其中数据库A的participants表里面存放了1000个参会者,数据库B的participants这个表存放了1000个参会者。

作者:中国IT实验室 来源:中国IT实验室 2007年8月23日

关键字: java 继承

  • 评论
  • 分享微博
  • 分享邮件

  如果还不清楚的话,我们就这样举例吧,比如,现在我们已经有了2000个参会者,在两个数据库中存放,其中数据库A的participants表里面存放了1000个参会者,数据库B的participants这个表存放了1000个参会者。DBTable顶多只能描述一个数据库里面的一张表,也就是1000个参会者,而participants则可以完全的描述这2000年参会者的信息。前面可以当作数据库的数据表在系统中的代表,而后者表示的应该包含更多业务逻辑的一个域对象。(原谅这边我只能用域对象这样的词来断开这样的混淆。)

  因此,我们可以判断,ParticipantsInDB跟DBTable之间不应该有什么继承的关系。ParticipantsInDB不能继承DBTable这个类了。于是,现在ParticipantsInDB也没有getCount这个方法了。可是ParticipantsInDB还需要DBTable类里面的其他方法啊,那怎么办?所以现在我们让ParticipantsInDB里面引用了一个DBTable:                                  
                                                                          

    public class DBTable {                                                                         
       private Connection conn;                                                                    
       private String tableName;                                                                  
       public DBTable(String tableName) {                                                          
           this.tableName = tableName;                                                            
           this.conn = ...;                                                                        
       }                                                                                          
       public void clear() {                                                                      
           PreparedStatement st = conn.prepareStatement("DELETE FROM "+tableName);                
           try {                                                                                  
               st.executeUpdate();                                                                
           }finally{                                                                              
               st.close();                                                                        
           }                                                                                      
       }                                                                                          
       public int getCount() {                                                                    
           PreparedStatement st = conn.prepareStatement("SELECT COUNT(*) FROM "+tableName);
           try {                                                                                  
               ResultSet rs = st.executeQuery();                                                  
               rs.next();                                                                          
               return rs.getInt(1);
          }finally{            
               st.close();      
          }                    
       }                      
       public String getTableName() {
          return tableName;    
       }                      
       public Connection getConn() {
          return conn;        
       }                      
   }                          
  
   public class ParticipantsInDB {
       private DBTable table;  
       public ParticipantsInDB() {
          table = new DBTable("participants");
       }                      
       public void addParticipant(Participant part) {
          ...                  
       }                      
       public void deleteParticipant(String participantId) {
          setDeleteFlag(participantId, true);
       }                      
       public void restoreParticipant(String participantId) {
          setDeleteFlag(participantId, false);
       }                      
       private void setDeleteFlag(String participantId, boolean b) {
          ...                  
       }                      
       public void reallyDelete() {
          PreparedStatement st = table.getConn().prepareStatement(
                              "DELETE FROM "+
                              table.getTableName()+
                              " WHERE deleteFlag=true");
          try {                
              st.executeUpdate();
          }finally{            
              st.close();      
          }                    
       }                      
       public void clear() {  
          table.clear();      
       }                      
       public int countParticipants() {
          PreparedStatement st = table.getConn().prepareStatement(
                              "SELECT COUNT(*) FROM "+
                              table.getTableName()+
                              " WHERE deleteFlag=false");
          try {                
              ResultSet rs = st.executeQuery();
              rs.next();      
              return rs.getInt(1);
          }finally{            
              st.close();      
          }                    
       }                      
   }
                          


    ParticipantsInDB不再继承DBTable。代替的,它里面有一个属性引用了一个DBTable对象,然后调用这个DBTable的clear, getConn, getTableName 等等方法。

代理(delegation)    
  
  其实我们这边可以看一下ParticipantsInDB的clear方法,这个方法除了直接调用DBTable的clear方法以外,什么也没做。或者说,ParticipantsInDB只是做为一个中间介让外界调用DBTable的方法,我们管这样传递调用的中间介叫“代理(delegation)”。            

  现在,之前有bug的那部分代码就编译不过了:

    ParticipantsInDB partsInDB = ...;                                                              
    Participant kent = new Participant(...);                                                      
    Participant paul = new Participant(...);                                                      
    partsInDB.clear();                                                                            
    partsInDB.addParticipant(kent);                                                                
    partsInDB.addParticipant(paul);                                                                
    partsInDB.deleteParticipant(kent.getId());                                                    
    //编译出错:因为在ParticipantsInDB里面已经没有getCount这个方法了!
    System.out.println("There are "+partsInDB.getCount()+ "participants");        

                

  总结一下:首先,我们发现,ParticipantsInDB 和 DBTableIn之间没有继承关系。然后我们就将“代理”来取代它们的继承。“代理”的优点就是,我们可以控制DBTable的哪些方法可以“公布(就是设为public)”(比如clear方法)。如果我们用了继承的话,我们就没得选择,DBTable里面的所有public方法都要对外公布!

查看本文来源
    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章