{"id":2137,"date":"2022-02-22T16:35:40","date_gmt":"2022-02-22T08:35:40","guid":{"rendered":"https:\/\/www.zeroheart.xyz\/wordpress\/?p=2137"},"modified":"2022-02-22T16:35:40","modified_gmt":"2022-02-22T08:35:40","slug":"%e7%a9%ba%e5%af%b9%e8%b1%a1%e6%a8%a1%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/www.zeroheart.xyz\/wordpress\/?p=2137","title":{"rendered":"\u7a7a\u5bf9\u8c61\u6a21\u5f0f"},"content":{"rendered":"\n<p>\u5728\u7a7a\u5bf9\u8c61\u6a21\u5f0f\uff08Null Object Pattern\uff09\u4e2d\uff0c\u4e00\u4e2a\u7a7a\u5bf9\u8c61\u53d6\u4ee3 NULL \u5bf9\u8c61\u5b9e\u4f8b\u7684\u68c0\u67e5\u3002Null \u5bf9\u8c61\u4e0d\u662f\u68c0\u67e5\u7a7a\u503c\uff0c\u800c\u662f\u53cd\u5e94\u4e00\u4e2a\u4e0d\u505a\u4efb\u4f55\u52a8\u4f5c\u7684\u5173\u7cfb\u3002\u8fd9\u6837\u7684 Null \u5bf9\u8c61\u4e5f\u53ef\u4ee5\u5728\u6570\u636e\u4e0d\u53ef\u7528\u7684\u65f6\u5019\u63d0\u4f9b\u9ed8\u8ba4\u7684\u884c\u4e3a\u3002<\/p>\n\n\n\n<p>\u5728\u7a7a\u5bf9\u8c61\u6a21\u5f0f\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u6307\u5b9a\u5404\u79cd\u8981\u6267\u884c\u7684\u64cd\u4f5c\u7684\u62bd\u8c61\u7c7b\u548c\u6269\u5c55\u8be5\u7c7b\u7684\u5b9e\u4f53\u7c7b\uff0c\u8fd8\u521b\u5efa\u4e00\u4e2a\u672a\u5bf9\u8be5\u7c7b\u505a\u4efb\u4f55\u5b9e\u73b0\u7684\u7a7a\u5bf9\u8c61\u7c7b\uff0c\u8be5\u7a7a\u5bf9\u8c61\u7c7b\u5c06\u65e0\u7f1d\u5730\u4f7f\u7528\u5728\u9700\u8981\u68c0\u67e5\u7a7a\u503c\u7684\u5730\u65b9\u3002<\/p>\n\n\n\n<p>\u4e0a\u9762\u8bf4\u7684\u6bd4\u8f83\u62bd\u8c61\uff0c\u5b9e\u9645\u4e0a\u5c31\u662f\u5728\u9700\u8981\u8fd4\u56de\u7a7a\u7684\u5730\u65b9\uff0c\u53ef\u4ee5\u8fd4\u56de\u4e00\u4e2a\u7a7a\u5bf9\u8c61\uff0c\u8fd9\u6837\u5b50\u6bd4null\u6765\u7684\u4f18\u96c5\u4e00\u4e9b\uff0c\u540c\u65f6\u53ef\u4ee5\u63d0\u4f9b\u4e00\u4e9b\u9ed8\u8ba4\u7684\u884c\u4e3a\uff0c\u6bd4\u5982\u7a7a\u7528\u6237\u5bf9\u8c61\uff0c\u53ef\u4ee5\u7ed9\u4e00\u4e2a\u9ed8\u8ba4\u7684\u5934\u50cf\u4e4b\u7c7b\u7684\uff0c\u4e5f\u53ef\u4ee5\u9632\u6b62\u7a7a\u6307\u9488\u5f02\u5e38\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\u6b65\u9aa4 1\n\u521b\u5efa\u4e00\u4e2a\u62bd\u8c61\u7c7b\u3002\n\nAbstractCustomer.java\npublic abstract class AbstractCustomer {\n   protected String name;\n   public abstract boolean isNil();\n   public abstract String getName();\n}\n\n\n\u6b65\u9aa4 2\n\u521b\u5efa\u6269\u5c55\u4e86\u4e0a\u8ff0\u7c7b\u7684\u5b9e\u4f53\u7c7b\u3002\n\nRealCustomer.java\npublic class RealCustomer extends AbstractCustomer {\n \n   public RealCustomer(String name) {\n      this.name = name;    \n   }\n   \n   @Override\n   public String getName() {\n      return name;\n   }\n   \n   @Override\n   public boolean isNil() {\n      return false;\n   }\n}\nNullCustomer.java\npublic class NullCustomer extends AbstractCustomer {\n \n   @Override\n   public String getName() {\n      return \"Not Available in Customer Database\";\n   }\n \n   @Override\n   public boolean isNil() {\n      return true;\n   }\n}\n\n\n\u6b65\u9aa4 3\n\u521b\u5efa CustomerFactory \u7c7b\u3002\n\nCustomerFactory.java\npublic class CustomerFactory {\n   \n   public static final String[] names = {\"Rob\", \"Joe\", \"Julie\"};\n \n   public static AbstractCustomer getCustomer(String name){\n      for (int i = 0; i &lt; names.length; i++) {\n         if (names[i].equalsIgnoreCase(name)){\n            return new RealCustomer(name);\n         }\n      }\n      return new NullCustomer();\n   }\n}\n\n\n\u6b65\u9aa4 4\n\u4f7f\u7528 CustomerFactory\uff0c\u57fa\u4e8e\u5ba2\u6237\u4f20\u9012\u7684\u540d\u5b57\uff0c\u6765\u83b7\u53d6 RealCustomer \u6216 NullCustomer \u5bf9\u8c61\u3002\n\nNullPatternDemo.java\npublic class NullPatternDemo {\n   public static void main(String[] args) {\n \n      AbstractCustomer customer1 = CustomerFactory.getCustomer(\"Rob\");\n      AbstractCustomer customer2 = CustomerFactory.getCustomer(\"Bob\");\n      AbstractCustomer customer3 = CustomerFactory.getCustomer(\"Julie\");\n      AbstractCustomer customer4 = CustomerFactory.getCustomer(\"Laura\");\n \n      System.out.println(\"Customers\");\n      System.out.println(customer1.getName());\n      System.out.println(customer2.getName());\n      System.out.println(customer3.getName());\n      System.out.println(customer4.getName());\n   }\n}\n\n\u4ee5\u4e0a\u662f\u4e00\u4e2a\u4f8b\u5b50\uff0c\u62bd\u8c61\u7c7b\uff0c\u4e24\u4e2a\u5b9e\u73b0\uff0c\u4e00\u4e2a\u662f\u666e\u901a\u5bf9\u8c61\uff0c\u4e00\u4e2a\u662f\u7a7a\u5bf9\u8c61\u3002\n<\/pre>\n\n\n\n<p>\u53c2\u8003\uff1a<a href=\"https:\/\/www.runoob.com\/design-pattern\/null-object-pattern.html\">\u7a7a\u5bf9\u8c61\u6a21\u5f0f | \u83dc\u9e1f\u6559\u7a0b (runoob.com)<\/a><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u7a7a\u5bf9\u8c61\u6a21\u5f0f\uff08Null Object Pattern\uff09\u4e2d\uff0c\u4e00\u4e2a\u7a7a\u5bf9\u8c61\u53d6\u4ee3 NULL \u5bf9\u8c61\u5b9e\u4f8b\u7684\u68c0\u67e5\u3002Null  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[131],"tags":[],"_links":{"self":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/2137"}],"collection":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2137"}],"version-history":[{"count":7,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/2137\/revisions"}],"predecessor-version":[{"id":2144,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/2137\/revisions\/2144"}],"wp:attachment":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2137"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2137"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2137"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}