Design Pattern 简明教程

Design Patterns - Null Object Pattern

在 Null 对象模式中,一个 null 对象替代了 NULL 对象实例的检查。Null 对象反映一种不采取任何操作的关系,而不是放置对 null 值的 if 检查。如果没有数据,也可以使用 Null 对象提供默认行为。

In Null Object pattern, a null object replaces check of NULL object instance. Instead of putting if check for a null value, Null Object reflects a do nothing relationship. Such Null object can also be used to provide default behaviour in case data is not available.

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

In Null Object pattern, we create an abstract class specifying various operations to be done, concrete classes extending this class and a null object class providing do nothing implemention of this class and will be used seemlessly where we need to check null value.

Implementation

我们将创建一个定义操作的 AbstractCustomer 抽象类。此处是客户的名字和扩展 AbstractCustomer 类的具体类。工厂类 CustomerFactory 根据传递给它的客户名返回 RealCustomer 或 NullCustomer 对象。

We are going to create a AbstractCustomer abstract class defining opearations. Here the name of the customer and concrete classes extending the AbstractCustomer class. A factory class CustomerFactory is created to return either RealCustomer or NullCustomer objects based on the name of customer passed to it.

NullPatternDemo(我们的演示类)将使用 CustomerFactory 来演示空对象模式。

NullPatternDemo, our demo class, will use CustomerFactory to demonstrate the use of Null Object pattern.

null pattern uml diagram

Step 1

创建一个抽象类。

Create an abstract class.

AbstractCustomer.java

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

Step 2

创建一个扩展上述类的具体类。

Create concrete classes extending the above class.

RealCustomer.java

public class RealCustomer extends AbstractCustomer {

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

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

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

NullCustomer.java

public class NullCustomer extends AbstractCustomer {

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

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

Step 3

创建 CustomerFactory 类。

Create CustomerFactory Class.

CustomerFactory.java

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();
   }
}

Step 4

根据传递给它的客户名来使用 CustomerFactory 获取 RealCustomer 或 NullCustomer 对象。

Use the CustomerFactory to get either RealCustomer or NullCustomer objects based on the name of customer passed to it.

NullPatternDemo.java

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());
   }
}

Step 5

验证输出。

Verify the output.

Customers
Rob
Not Available in Customer Database
Julie
Not Available in Customer Database