空对象模式
在空对象模式(Null Object Pattern)中,一个空对象取代 NULL 对象实例的检查。Null 对象不是检查空值,而是反应一个不做任何动作的关系。这样的 Null 对象也可以在数据不可用的时候提供默认的行为。
在空对象模式中,我们创建一个指定各种要执行的操作的抽象类和扩展该类的实体类,还创建一个未对该类做任何实现的空对象类,该空对象类将无缝地使用在需要检查空值的地方。
实现
我们将创建一个定义操作(在这里,是客户的名称)的 AbstractCustomer 抽象类,和扩展了 AbstractCustomer 类的实体类。工厂类 CustomerFactory 基于客户传递的名字来返回 RealCustomer 或 NullCustomer 对象。
NullPatternDemo,我们的演示类使用 CustomerFactory 来演示空对象模式的用法。
 
步骤 1
创建一个抽象类。
AbstractCustomer.java
public abstract class AbstractCustomer {
   protected String name;
   public abstract boolean isNil();
   public abstract String getName();
}
步骤 2
创建扩展了上述类的实体类。
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;
   }
}
步骤 3
创建 CustomerFactory 类。
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();
   }
}
步骤 4
使用 CustomerFactory,基于客户传递的名字,来获取 RealCustomer 或 NullCustomer 对象。
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());
   }
}
步骤 5
执行程序,输出结果:
Customers Rob Not Available in Customer Database Julie Not Available in Customer Database
 
       
Siskin.xu
sis***@sohu.com
Python 代码:
from abc import abstractmethod, ABCMeta # 创建一个抽象类 class AbstractCustomer(metaclass = ABCMeta): name = "" @abstractmethod def isNil(self): pass @abstractmethod def getName(self): pass # 创建扩展了AbstractCustomer的实体类 class RealCustomer(AbstractCustomer): def __init__(self, inName): self.name = inName def getName(self): return self.name def isNil(self): return Falseclass NullCustomer(AbstractCustomer): def getName(self): return "Not Available in Customer Database" def isNil(self): return True # 创建CustomerFactory类 class CustomerFactory(): names = ["Rob", "Joe", "Julie"] @staticmethod def getCustomer(inName): for aName in CustomerFactory.names: if aName.upper() == inName.upper(): return RealCustomer(inName) return NullCustomer()# 调用输出 if __name__ == '__main__': customer1 = CustomerFactory.getCustomer("Rob") customer2 = CustomerFactory.getCustomer("Bob") customer3 = CustomerFactory.getCustomer("Julie") customer4 = CustomerFactory.getCustomer("Laura") print("Customers") print(customer1.getName()) print(customer2.getName()) print(customer3.getName()) print(customer4.getName())Siskin.xu
sis***@sohu.com
liangfang
lia***ang@vip.163.com
PHP代码:
<?php abstract class AbstractCustomer{ protected $name; public abstract function isNil(); public abstract function getName(); } class RealCustomer extends AbstractCustomer { public function __construct($name) { $this->name = $name; } public function getName() { return $this->name; } public function isNil() { return false; } } class NullCustomer extends AbstractCustomer { public function getName() { return "Not Available in Customer Database"; } public function isNil() { return true; } } class CustomerFactory { #PHP中final不能修饰变量,但可以是类和方法 public static $names = ["Rob", "Joe", "Julie"]; public static function getCustomer($name){ //foreach更方便,如果说有点学习点地方就是静态变量必须用静态方法调用,self::$value for ($i = 0; $i < count(self::$names); $i++) { if (self::$names[$i] == $name){ return new RealCustomer($name); } } return new NullCustomer(); } } class NullPatternDemo { public static function main() { $customer1 = CustomerFactory::getCustomer("Rob"); $customer2 = CustomerFactory::getCustomer("Bob"); $customer3 = CustomerFactory::getCustomer("Julie"); $customer4 = CustomerFactory::getCustomer("Laura"); echo "Customers".PHP_EOL; echo $customer1->getName().PHP_EOL; echo $customer2->getName().PHP_EOL; echo $customer3->getName().PHP_EOL; echo $customer4->getName().PHP_EOL; } } NullPatternDemo::main(); /* * 执行程序,输出结果: localhost:bin fangliang$ php /Users/fangliang/Downloads/test.php Customers Rob Not Available in Customer Database Julie Not Available in Customer Database * * */liangfang
lia***ang@vip.163.com