簡體   English   中英

我們可以重構這些方法嗎?

[英]Can we refactor these methods?

我的課程實現方法如下:

void methodOne() {
    try {
        getHelper().doActionOne();
    } catch ( Exception ex ) {
        throw new CustomException( ex );
    }
}

void methodTwo() {
    try {
        getHelper().doActionTwo();
    } catch ( Exception ex ) {
        throw new CustomException( ex );
    }
}

void methodThree() {
    try {
        getHelper().doActionThree();
    } catch ( Exception ex ) {
        throw new CustomException( ex );
    }
}


void methodFour;
void methodFive;
...

有一個更好的方法嗎? 這些代碼讓我感到不舒服。

編輯:抱歉不清楚的例子。 我正在用Hibernate實現GenericDao類,真正的代碼是這樣的:

class GenericDaoImpl<T, PK> {

    PK create( T object ) {
        try {
           getSession().save( object );
        } catch( Exception ex ) {
           throw new DataAccessLayerException( ex );// wrap any exception to my exception
        }
    }

   T read( PK id ) {
       try {
           getSession().get( T.class, id );
       } catch ( Exception ex ) {
           throw new DataAccessLayerException( ex );
       }

   }

  void update( T object );
  void delete( T object );

}

只是一個基本的建議,但你可以將其重構成類似“命令模式”的東西。 此模式允許您將某些功能封裝到實現單個方法的類中。 該類可以被實例化並傳遞到另一個要執行的類,而執行器類不必知道或關心它正在做什么,它只需要調用execute()。 如果操作需要參數,則實現Command的類可以包括可以在構造函數中設置的字段/屬性,也可以包含標准屬性設置器。

創建一個這樣的界面(我的Java生銹了,所以這可能不是100%有效的語法):

public interface Command
{
    public void execute();
}

public class ActionOne implements Command
{
    public void execute()
    {
        // do actionOne...
    }
}

public class ActionTwo implements Command
{
    public void execute()
    {
        // do actionTwo...
    }
}

// etc. for more actions

然后創建執行操作的類,調用代碼只需要傳入正確的Command實現類。

public class Executor
{

    public void executeCommand(Command command)
    {
        try
        {
            // Put any boilerplate code here (logging, auditing, etc.)
            command.execute();
        }
        catch (Exception ex)
        {
            // Put general error handling code here.  If you're just catching and rethrowing, consider not catching the Exception at this level.  If it's a checked exception, add a throws clause to the method.
            throw new CustomException();
        }
    }
}

是的,你總是可以重構代碼。 唯一的問題是你是否要重構以使其變得更好或更糟。 這段代碼奇怪的提示是一個很好的提示,它可以做得更好。

這似乎是多態性的良好候選者。 使用一個共享方法嘗試五個不同的類,而不是五種不同的方法。 界面將它們連接在一起。

public interface DoIt {
  public void doIt();
}

public class One implements DoIt {
  public void doIt() {
    // code which was previously in getHelper.doActionOne();
  }
}

public class Two implements DoIt {
  public void doIt() {
    // code which was previously in getHelper.doActionTwo();
  }
}

...

public class Five implements DoIt {
  public void doIt() {
    // code which was previously in getHelper.doActionFive();
  }
}

現在唯一的事情是為情境創建正確的類,並調用其doIt()方法。

此工具由Spring Framework以及許多其他工具提供。 首先,它有一個特定的HibernateTemplate ,它將每個特定於Hibernate的異常映射到未經檢查的相關Spring異常。 其次,它提供了一個AOP服務來在方法級別轉換異常,因此您可以指定一次映射並在多個服務之間統一應用它們。

雖然我不會將Spring用於這一功能,但它對構建應用程序有很大的好處,而且我已經使用它多年了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM