美文网首页
重构读书笔记-9_7-Introduce_Null_Object

重构读书笔记-9_7-Introduce_Null_Object

作者: MR_Model | 来源:发表于2019-07-14 22:48 被阅读0次

重构第九章

7.Introduce Null Object(引入Null对象)

你需要再三检查[某物是否为null value]。将null value(无效值)替换为null object(无效物)。

Example:

class Site...
    Customer getCustomer() {
        return _customer;
    }
    Customer _customer;

class Customer...
    public String getName() {...}
    public BillingPlan getPlan() {...}
    public PaymentRistory getHistory() {...}
class PaymentHistory...
    int getWeekDelinquentInLastYear();

Customer customer = site.getCustomer();
BillingPlan plan;
if(customer == null) plan = BillingPlan.basic();
else plan = customer.getPlan(); 
String customerName;
if(customer == null) customerName = "occupant";
else customerName = customer.getName();
int weeksDelinquent;
if(customer == null) weeksDelinquent = 0;
else weeksDelinquent = customer.getHistory().getWeeksDelinquentInLastYear();

End:

class NullPaymentHistory extends PaymentHistory...
    int getWeeksDelinquentInLastYear() {
        return 0;
    }
class NullCustomer...
    boolean isNull() {
        return true;
    }
    public BillingPlan getPlan() {
        return BillingPlan.basic();
    }
    public String getName() {
        return "occupant";
    }
    class NullCustoemr...
        public PaymentHistory getHistory() {
            return PaymentHistory.newNull();
        }
class Customer...
    public boolean isNull() {
        return false;
    }
    public String getName() {...}
    public BillingPlan getPlan() {...}
    public PaymentRistory getHistory() {...}

class PaymentHistory...
    int getWeekDelinquentInLastYear();

    Customer customer = site.getCustomer();
    BillingPlan plan = customer.getPlan();  
    String customerName = customer.getName();
    int weeksDelinquent = customer.getHistory().getWeeksDelinquentInLastYear();

Conclusion:

Introduce Null Object(引入Null对象)将Null Object作为class的一个子类,这样可以直接通过多态机制,来区分空对象和其他对象的区别,去除了多余的条件式,使得程序简洁一些;同时这项手法,可以让你对不同的空值所要进行的操作进行一个分类,即不同的空值有着不同的反应,而不需要添加新的条件式和型别码。

注意

重构必须在有单元测试的情况下,保证之前的功能修改后不收影响。切记!!!

相关文章

  • 重构读书笔记-9_7-Introduce_Null_Object

    重构第九章 7.Introduce Null Object(引入Null对象) 你需要再三检查[某物是否为null...

  • 重构:读书笔记

    重构读书笔记 第一章 重构,第一个案例 第二章 重构原则 2.1 何为重构 重构(名词):对软件内部结构的一种调整...

  • 重构读书笔记

    title: 重构读书笔记date: 2019/11/05 重构:对软件内部结构的一种调整,目的是在不改变软件可观...

  • 《重构》读书笔记

    《重构》读书笔记 总览 第一部分 第一章从实例程序出发,展示设计的缺陷,对其重构可以了解重构的过程和方法。 第二部...

  • 《危机与重构:唐帝国及其地方诸侯》读书笔记

    《危机与重构:唐帝国及其地方诸侯》读书笔记 李碧妍先生的《危机与重构:唐帝国及其地方诸侯》一书。是以唐代后期的藩镇...

  • 《重构--改善既有代码的设计》读书笔记

    《重构--改善既有代码的设计》读书笔记 1为什么重构有用 所有有意义的事情总结下来,都是完成了一个有用的功能,或者...

  • 个人技术文章系列汇总(csdn)

    Java基础 Effective Java读书笔记 java 几种加载驱动的方法 《重构改善既有代码的设计》代码的...

  • 《重构》读书笔记

    chapter 1 重构,第一个案例 1.1 什么时候需要重构 需要为程序添加一个特性,但代码结构无法使自己方便的...

  • 重构-读书笔记

    有一本书叫做《重构 改善既有代码的设计 》,个人感觉写的还蛮不错的,在读这本书时候做的一些读书笔记,分享给大家: ...

  • 重构-读书笔记

    重构 概念:在不改变代码外在行为的前提下,对代码做出修改,以改进程序的内部结构。重构技术就是以微小的步伐修改程序。...

网友评论

      本文标题:重构读书笔记-9_7-Introduce_Null_Object

      本文链接:https://www.haomeiwen.com/subject/dflwkctx.html