Java设计模式——空对象模式(Null Object Pattern)
来源:互联网 发布:拳皇咆哮源码 编辑:程序博客网 时间:2024/05/20 16:42
空对象模式简介
在空对象模式(Null Object Pattern)中,一个空对象取代 NULL 对象实例的检查。Null 对象不是检查空值,而是反应一个不做任何动作的关系。这样的 Null 对象也可以在数据不可用的时候提供默认的行为。
在空对象模式中,我们创建一个指定各种要执行的操作的抽象类和扩展该类的实体类,还创建一个未对该类做任何实现的空对象类,该空对象类将无缝地使用在需要检查空值的地方。
代码实现
创建一个定义操作(在这里,是客户的名称)的 AbstractCustomer 抽象类,和扩展了 AbstractCustomer 类的实体类。工厂类 CustomerFactory 基于客户传递的名字来返回 RealCustomer 或 NullCustomer 对象。
NullPatternDemo,演示类使用 CustomerFactory 来演示空对象模式的用法。
UML类图
第一步:创建一个抽象类
AbstractCustomer.java
public abstract class AbstractCustomer { protected String name; public abstract boolean isNil(); public abstract String getName();}
第二步:创建扩展了上述类的实体类
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; }}
第三步:创建 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(); }}
第四步:使用 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()); }}
第五步:验证输出
CustomersRobNot Available in Customer DatabaseJulieNot Available in Customer Database
阅读全文
0 0
- Java设计模式——空对象模式(Null Object Pattern)
- 设计模式:空对象模式(Null Object Pattern)
- 【设计模式】空对象模式(Null Object Pattern)
- 被遗忘的设计模式——空对象模式(Null Object Pattern)
- 被遗忘的设计模式——空对象模式(Null Object Pattern)
- Java 空对象设计模式(Null Object Pattern) 讲解
- Java Null Object Pattern(空对象模式)
- [翻译]空对象设计模式(Null Object Design Pattern)
- 空对象模式(Null Object Pattern)
- 设计模式学习—空对象模式(Null Object Design Pattern)
- 【设计模式 - 21】之空对象模式(Null Object)
- 设计模式之空对象(Null Object)模式
- 【设计模式】传输对象模式(Transfer Object Pattern)
- 设计模式--行为模式--NULL Object--Java
- 设计模式学习—数据访问对象模式(Data Access Object Design Pattern)
- 设计模式学习—传输对象模式(Transfer Object Design Pattern)
- Java Transfer Object Pattern(传输对象模式)
- Null Object 设计模式
- leetcode
- 关于SVN汉化失败
- hdu1108 最小公倍数
- 2.6Java继承(抽象类,接口,final)
- bzoj 4424: Cf19E Fairy dfs
- Java设计模式——空对象模式(Null Object Pattern)
- 第二章
- bzoj3207: 花神的嘲讽计划Ⅰ( 主席树 (动态开点) + hash )
- 卷积神经网络(CNN)相关知识以及数学推导
- 第一章习题
- 解决问题需要参考的文章
- cookie存储
- 通过IDEA将代码推送至不同的远程库
- Android 自定义View画圆