IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

二十六、空对象模式 ( Null Object Pattern )

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

文章永久连接:https://tech.souyunku.com/?p=2703

在空对象模式(Null Object Pattern)中,一个空对象取代 NULL 对象实例的检查

Null 对象不是检查空值,而是反应一个不做任何动作的关系,这样的 Null 对象也可以在数据不可用的时候提供默认的行为。

在空对象模式中,我们创建一个指定各种要执行的操作的抽象类和扩展该类的实体类,还创建一个未对该类做任何实现的空对象类,该空对象类将无缝地使用在需要检查空值的地方

实现

我们将创建一个定义操作(在这里,是客户的名称)的 AbstractCustomer 抽象类,和扩展了 AbstractCustomer 类的实体类

工厂类 CustomerFactory 基于客户传递的名字来返回 RealCustomerNullCustomer 对象

NullPatternDemo ,我们的演示类使用 CustomerFactory 来演示空对象模式的用法

img_1.png

范例

1. 创建一个抽象类 AbstractCustomer

// author: 搜云库技术团队(tech.souyunku.com)
// Copyright © 2015-2065 tech.souyunku.com. All rights reserved.

package com.souyunku.tech.gof;


public abstract class AbstractCustomer
{
   protected String name;
   public abstract boolean isNil();
   public abstract String getName();
}

2. 创建扩展了上述类的实体类

RealCustomer

// author: 搜云库技术团队(tech.souyunku.com)
// Copyright © 2015-2065 tech.souyunku.com. All rights reserved.

package com.souyunku.tech.gof;


class RealCustomer extends AbstractCustomer
{

   public RealCustomer(String name) {
      this.name = name;     
   }

   @Override
   public String getName() {
      return name;
   }

   @Override
   public boolean isNil() {
      return false;
   }
}

NullCustomer

// author: 搜云库技术团队(tech.souyunku.com)
// Copyright © 2015-2065 tech.souyunku.com. All rights reserved.

package com.souyunku.tech.gof;


public class NullCustomer extends AbstractCustomer {

   @Override
   public String getName() {
      return "Not Available in Customer Database";
   }

   @Override
   public boolean isNil() {
      return true;
   }
}

3. 创建 CustomerFactory

// author: 搜云库技术团队(tech.souyunku.com)
// Copyright © 2015-2065 tech.souyunku.com. All rights reserved.

package com.souyunku.tech.gof;


public class CustomerFactory {

   public static final String[] names = {"Rob", "Joe", "Julie"};

   public static AbstractCustomer getCustomer(String name){
      for (int i = 0; i < names.length; i++) {
         if (names[i].equalsIgnoreCase(name)){
            return new RealCustomer(name);
         }
      }
      return new NullCustomer();
   }
}

4. 使用 CustomerFactory ,基于客户传递的名字,来获取 RealCustomerNullCustomer 对象

// author: 搜云库技术团队(tech.souyunku.com)
// Copyright © 2015-2065 tech.souyunku.com. All rights reserved.

package com.souyunku.tech.gof;


public class NullPatternDemo {
   public static void main(String[] args) {

      AbstractCustomer customer1 = CustomerFactory.getCustomer("Rob");
      AbstractCustomer customer2 = CustomerFactory.getCustomer("Bob");
      AbstractCustomer customer3 = CustomerFactory.getCustomer("Julie");
      AbstractCustomer customer4 = CustomerFactory.getCustomer("Laura");

      System.out.println("Customers");
      System.out.println(customer1.getName());
      System.out.println(customer2.getName());
      System.out.println(customer3.getName());
      System.out.println(customer4.getName());
   }
}

编译运行以上 Java 范例,输出结果如下

$ javac -d . src/main/com/souyunku/tech/gof/NullPatternDemo.java
$ java  com.souyunku.tech.gof.NullPatternDemo
Customers
Rob
Not Available in Customer Database
Julie
Not Available in Customer Database

干货推荐

本站推荐:精选优质专栏

附录:设计模式:系列文章


Warning: A non-numeric value encountered in /data/wangzhan/tech.souyunku.com.wp/wp-content/themes/dux/functions-theme.php on line 1154
赞(78) 打赏



未经允许不得转载:搜云库技术团队 » 二十六、空对象模式 ( Null Object Pattern )

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码
IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

评论 抢沙发

大前端WP主题 更专业 更方便

联系我们联系我们

觉得文章有用就打赏一下文章作者

微信扫一扫打赏

微信扫一扫打赏


Fatal error: Uncaught Exception: Cache directory not writable. Comet Cache needs this directory please: `/data/wangzhan/tech.souyunku.com.wp/wp-content/cache/comet-cache/cache/https/tech-souyunku-com/index.q`. Set permissions to `755` or higher; `777` might be needed in some cases. in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php:367 Stack trace: #0 [internal function]: WebSharks\CometCache\Classes\AdvancedCache->outputBufferCallbackHandler() #1 /data/wangzhan/tech.souyunku.com.wp/wp-includes/functions.php(5109): ob_end_flush() #2 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(303): wp_ob_end_flush_all() #3 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(327): WP_Hook->apply_filters() #4 /data/wangzhan/tech.souyunku.com.wp/wp-includes/plugin.php(470): WP_Hook->do_action() #5 /data/wangzhan/tech.souyunku.com.wp/wp-includes/load.php(1097): do_action() #6 [internal function]: shutdown_action_hook() #7 {main} thrown in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php on line 367