2013-04-05 77 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