码迷,mamicode.com
首页 > 其他好文 > 详细

设计模式(32)-----行为型模式-----空对象设计模式

时间:2018-11-08 00:10:51      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:this   static   int   行为型   lob   string   设计模式   package   空值   

在空对象模式(Null Object Pattern)中,一个空对象取代 NULL 对象实例的检查。Null 对象不是检查空值,而是反应一个不做任何动作的关系。这样的 Null 对象也可以在数据不可用的时候提供默认的行为。

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

 

我们将创建一个定义操作(在这里,是客户的名称)的 AbstractCustomer 抽象类,和扩展了 AbstractCustomer 类的实体类。工厂类 CustomerFactory 基于客户传递的名字来返回 RealCustomer 或 NullCustomer 对象。

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

 

步骤 1

创建一个抽象类。

package com.DesignPatterns.ap.NullObject1;

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

 

步骤 2

创建扩展了上述类的实体类。

package com.DesignPatterns.ap.NullObject1;

public class RealCustomer extends AbstractCustomer {
    
    public RealCustomer(String name) {
       this.name = name;    
    }
    
    @Override
    public String getName() {
       return name;
    }
    
    @Override
    public boolean isNil() {
       return false;
    }
 }

 

public class NullCustomer extends AbstractCustomer {
    
    @Override
    public String getName() {
       return "没有发现该名字的人";
    }
  
    @Override
    public boolean isNil() {
       return true;
    }
 }

 

步骤 3

创建 CustomerFactory 类。

 

package com.DesignPatterns.ap.NullObject1;

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

 

 

 

 

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("name:"+customer1.getName()+"。======isNil:"+customer1.isNil());
       System.out.println("name:"+customer2.getName()+"。======isNil:"+customer2.isNil());
       System.out.println("name:"+customer3.getName()+"。======isNil:"+customer3.isNil());
       System.out.println("name:"+customer4.getName()+"。======isNil:"+customer4.isNil());
    }
 }

 

 

Customers
name:Rob。======isNil:false
name:没有发现该名字的人。======isNil:true
name:Julie。======isNil:false
name:没有发现该名字的人。======isNil:true

 

设计模式(32)-----行为型模式-----空对象设计模式

标签:this   static   int   行为型   lob   string   设计模式   package   空值   

原文地址:https://www.cnblogs.com/qingruihappy/p/9926464.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!