2013-04-05 144 views
0

需要以下設計建議所需

public class SomeClass 
{ 
    public void AuthoriseSmartPhone(...) 
    { 
     try 
     { 
      Authorise(...) 
     } 
     catch(CustomException) 
     { 
      throw new CustomException("SmartPhone Message") 
     } 
    } 

    public void AuthoriseFeaturePhone(...) 
    { 
     try 
     { 
      Authorise(...) 
     } 
     catch(CustomException) 
     { 
      throw new CustomException("FeaturePhone Message") 
     } 
    } 

    private void Authorise(...) 
    { 
     if(manySuchConditionsCheckedHere)     
      throw new CustomException(""); 

     if(someOtherConditionCheckFails) 
      throw new YetAnotherException("Common Message to both phones"); 
    } 
} 

在代碼設計建議授權是有很多業務規則檢查

的私有方法

授權可以爲智能手機或功能手機

雖然智能手機和功能電話的規則相同,但是在任何情況下返回的信息都不相同。

目前上面的代碼拋出CustomException和同樣是捕獲並重新拋出。希望得到一些關於如何改善這些代碼的設計建議。

+0

可以'Authorise'返回一個bool,您檢查以確定您是否在其他方法中拋出異常? – juharr 2013-04-05 15:25:18

回答

2

一個想法是推出一些loose coupling到你的代碼。

你有一個像AuthoriseSmartPhoneAuthoriseFeaturePhone方法。此代碼不會在您的設計中留下任何擴展空間。如果你有正在使用的參數都實現通用接口的對象,你可以有一個方法:

public void AuthorisePhone(...) 
{ 
    try 
    { 
     if(parameter is a FeaturePhone) { 
      Authorise(...); 
     } 
     else if(...) 
    } 
    catch(CustomException ex) 
    { 
     throw new CustomException("Relevant Message", ex) 
    } 
} 

這樣,你可以調整類中的代碼,但從來沒有改變代碼在課外。這鼓勵了類之間的鬆散耦合,這是一個理想且非常有用的特性。

編輯

我也注意到,如果你採用這種方法,這意味着你可以採用Strategy Pattern在你的代碼。這有助於您有if...else if... else if結構。

+0

+1策略模式建議。我明白你的意思了。 Wrt異常用法克里斯 - 不是拋出和重新拋出不推薦? – 2013-04-05 15:36:26

+0

我相當肯定,在語義上,最好只有一個投擲;所以你的代碼更有意義。在性能方面,我認爲它有一點影響,但除非你騎着大量的物體,否則效果可以忽略不計。 – christopher 2013-04-06 10:31:50

1

我建議使用內部異常只是包裝由Authorise拋出的異常:

public void AuthoriseSmartPhone(...) 
{ 
    try 
    { 
     Authorise(...) 
    } 
    catch(CustomException ex) 
    { 
     throw new CustomException("SmartPhone Message", ex) 
    } 
} 

public void AuthoriseFeaturePhone(...) 
{ 
    try 
    { 
     Authorise(...) 
    } 
    catch(CustomException ex) 
    { 
     throw new CustomException("FeaturePhone Message", ex) 
    } 
} 

private void Authorise(...) 
{ 
    if(condition1)     
     throw new CustomException("Condition 1 Failed"); 
    if(condition2)     
     throw new CustomException("Condition 2 Failed"); 
    if(condition3)     
     throw new CustomException("Condition 3 Failed"); 
} 

這樣你仍然可以準確地確定哪些條件通過內部異常失敗,但與自定義消息外例外。

如果你想避免兩投在同一個班級,你可以簡單地從Authorise返回例外:

public void AuthoriseFeaturePhone(...) 
{ 
    Exception ex = Authorise(...) 
    if (ex != null) 
     throw new CustomException("FeaturePhone Message", ex); 
} 

private Exception Authorise(...) 
{ 
    if(condition1)     
     return new CustomException("Condition 1 Failed"); 
    if(condition2)     
     return new CustomException("Condition 2 Failed"); 
    if(condition3)     
     return new CustomException("Condition 3 Failed"); 
} 

但是請注意,內部異常不會有StackTrace。這也使得創建一個安全的bool TryAuthoriseFeaturePhone(...)方法非常容易,該方法不會拋出異常,但如果手機無法授權,只需返回false。該彈簧想到

+0

如果我錯了,請糾正我......拋出並重新拋出一個類不推薦用法..? – 2013-04-05 15:30:51

+0

@Venub它偶爾可以接受,但如果你想避免它,我在我的答案中包含了一個替代解決方案。 – 2013-04-05 15:34:48

+0

+1我明白你的意思了,我們返回一個例外。我一直在想,就像返回一個錯誤對象,然後採取進一步的行動。 – 2013-04-05 15:41:07