`
jacky1118sally
  • 浏览: 13365 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

switch(clean code)

阅读更多
看了《clean code》 关于 switch ,很有感触,给大家分享分享
首先 有这样的例子:
public Money calculatePay(Employee e)   throws InvalidEmployeeType {  
 switch (e.type) {     
  case COMMISSIONED:       
    return calculateCommissionedPay(e);     
  case HOURLY:        
    return calculateHourlyPay(e);     
  case SALARIED:        
    return calculateSalariedPay(e);      
  default:        
    throw new InvalidEmployeeType(e.type);  
  }  
} 

Payroll  这个对象 需要 根据 员工的不同来计算他们的工资,所以有这样的一个方法,返回 Money  用switch来区别type 然后做不同处理,这样也无可厚非。但是仔细想 确实是有问题的,首先雇员的类别有可能增加,那么这个switch 需要修改,而且会有 诸如isPayday(Employee e, Date date), 或 deliverPay(Employee e, Money pay)   这样的结构出现。确实是很头痛的事情。

看看书上是怎样 解决的
public abstract class Employee { 
   public abstract boolean isPayday();  
   public abstract Money calculatePay();  
   public abstract void deliverPay(Money pay); 
 }
  
 public interface EmployeeFactory {   
 public Employee makeEmployee(EmployeeRecord r) throws   InvalidEmployeeType;  
} 

public class EmployeeFactoryImpl implements EmployeeFactory{         

  public Employee makeEmployee(EmployeeRecord r) throws InvalidEmployeeType {    
   switch (r.type) {        
     case COMMISSIONED:          
       return new CommissionedEmployee(r) ;       
     case HOURLY:         
       return new HourlyEmployee(r);      
     case SALARIED:     
        return new SalariedEmploye(r);   
      default:         
         throw new InvalidEmployeeType(r.type);      
    }  } 
 }  
} 


使用工厂模式创建对象,创建不同对象,对于不同type的 区分,放到工厂下层,而不在上面体现,如此函数的 不同层级就很明确,而且使用Employee 抽象来完成 那几个动作。
设计 真的是体现了高手的 技术,层次,抽象,模型,这些虽然有时候仅仅没多少代码,但是都是精华,需要好好学习,像大师们学习。

分享到:
评论

相关推荐

    Write Clean Code Write Clean Code Write Clean Code Write Clean Code

    《Write Clean Code》是微软出版的一本专注于C语言编程实践的书籍,旨在帮助开发者提高代码质量,编写出更清晰、更可维护的程序。书中的内容涵盖了C编程的多个核心领域,包括基本语法、错误处理、内存管理、数据结构...

    cleanCode

    《Clean Code:C#编程实践解析》 在编程领域,代码质量是衡量软件工程水平的重要标准之一。"Clean Code",即整洁代码,是每个专业程序员追求的目标。它不仅关乎程序的功能实现,更关乎代码的可读性、可维护性和团队...

    clean-code-smells-and-heuristics

    《开发入门:clean-code-smells-and-heuristics》 在软件开发领域,代码质量是衡量项目成功与否的重要标准之一。"Clean Code"(整洁代码)是每个开发者应该追求的目标,而"Smells"(代码坏味道)和"Heuristics"...

    Guidelines for writing clean and fast code in MATLAB

    尽管MATLAB支持`switch`语句,但在大多数情况下,使用逻辑表达式和条件判断(`if-else`结构)更为灵活且易于维护。 #### 三、编写高效的代码 ##### 3.1 使用性能分析器 MATLAB提供了强大的性能分析工具,可以用来...

    Guidelines for writing clean and fast code in MATLAB.zip

    7. **逻辑运算**:使用逻辑索引和布尔数组来筛选和操作数据,而不是条件语句(如`if`和`switch`)。 8. **内存管理**:及时释放不再使用的变量,避免内存泄漏。理解MATLAB的内存管理机制,比如工作空间中的变量是...

    fundamentos-em-c-sharp-com-clean-code:带有简洁代码的免费 C# 基础课程

    【C#语言基础与Clean Code实践】 C#(读作"C sharp")是一种面向对象的编程语言,由微软公司开发并广泛应用于Windows平台、Web应用、游戏开发以及移动应用等领域。C#以其强大的功能和易读性,成为了现代软件开发的...

    clean-code-net:一组C#Roslyn分析器,以提高代码正确性

    确保在switch语句中涵盖所有可能的情况,以进行枚举和模式匹配。 考虑示例: enum TestEnum { Case1 , Case2 , Case3 } switch ( TestEnum . Case1 ) { case Test . TestEnum . Case1 : { break ; } case ...

    01450A (1).pdf

    mechanical switch so that a clean signal can feed other circuitry. The application makes use of the Configurable Logic Cell (CLC) peripheral to produce fast switching on the output (if desired). If ...

    acpi控制笔记本风扇转速

    Additional cleanup and optimizations for the new Table Manager code. AcpiEnable will now fail if all of the required ACPI tables are not loaded (FADT, FACS, DSDT). BZ 477 Added #pragma pack(8/4) to ...

    Amplify Shader Editor1.5.4 最新版

    INTUITIVE and UX-focused, the workspace is familiar, clean and flexible. Dock it, or use it in a second monitor, it’s prepared to fit your layout requirements. MINIMALISTIC toolbars and menus have ...

    Medical Chatbot Agent - using Langchain.rar

    In this code I am using GPT-4, but you have the flexibility to switch to any other supported model. Streamlit GUI: A clean and intuitive user interface built with Streamlit, making it accessible for ...

    robin源程序

    switch (c) { case 'q': cleanup_termios(0); break; case 'b': tcsendbreak(fd, 0); break; default: write(fd, &c, 1); break; } return; } ``` 此函数处理输入的转义字符。例如,当接收到字符`'q'`...

    三大开发工具快捷键大全

    - **CTRL+F7**:清理项目(Clean Project)。 - **CTRL+O**:打开一个文件(Open File)。 - **CTRL+SHIFT+O**:打开最近使用的文件(Open Recent File)。 - **CTRL+SHIFT+C**:显示类视图(Show Class View)。 - ...

    BobBuilder_app

    You can switch to the SortedDictionary in the code at any time if you wish and it makes no difference to the overall code other than you can remove the sorting in the page splits. I also tried an ...

    《SCJP学习指南》chap5

    // code that may throw an exception } catch (Exception e) { // handle the exception } finally { // clean up resources } ``` ### 断言(Assertions) 断言是一种调试工具,用于检查程序中假定为真的条件...

    c语言深度解剖 解说c语言

    - Steve Maguire 的《Write Clean Code》:强调编写清晰、易于维护的代码的重要性。 - Steve McConnell 的《Code Complete, Second Edition》:提供了大量编程实践方面的指导。 3. **调试技巧**:学会使用调试工具...

    Amplify Shader Editor 1.5.7

    • Full source-code • Support for Xbox One/PS4/Switch • Custom Node API • Shader Templates • Shader Functions • Multi-window Support • Intuitive, familiar node interface • Extensive node ...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    Another useful rule of thumb: it's typically not cost effective to inline functions with loops or switch statements (unless, in the common case, the loop or switch statement is never executed)....

    java编码规范

    - 《Clean Code: A Handbook of Agile Software Craftsmanship》 - 《Effective Java》 - Oracle官方Java文档和其他权威资料。 以上内容详细阐述了Java编码规范的关键点,涵盖了从代码风格、命名规则、声明、表达式...

Global site tag (gtag.js) - Google Analytics