当前位置:网站首页>空对象模式(3.14-3.20)
空对象模式(3.14-3.20)
2022-04-21 06:35:00 【看水是水】
介绍
在空对象模式(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 boolean isNil() {
return false;
}
@Override
public String getName() {
return name;
}
}
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
版权声明
本文为[看水是水]所创,转载请带上原文链接,感谢
https://blog.csdn.net/yuhuochongsheng_/article/details/124026021
边栏推荐
- 动态规划解决0/1背包问题
- 文件系统结构分析与数据恢复
- Voisins bgp
- 龙讯系列视频转换,LT9211、LT8918,功能:lvds转BT656,lvds转mipi(CSI\DSI)RGB转MIPI(DSI\CSI) BT656\601\1120转HDMI1.4\DVI
- Use Baidu map POI to crawl the required data
- China Resources micro power amplifier cs3850eo, 2 × 40W class D audio power amplifier circuit, replacement: Zhipu core cs8673, tas5780 and tas5754 of Ti, domestic power amplifier
- Echars thermal map custom colors
- IPV4-IGP
- IP组播基本概念
- Cjc5340,ADC,DAC,替换cs5340,数模转换器,100dB192KHz多位音频A/D转换器,音频Codec100dB192KHz多位音频A/D转换器
猜你喜欢

Convergencewarning: Free failed to converse, increase the number of iterations Solutions

Nodered connection database

NP, OSPF stub area

IP组播基本概念

类和对象的相关知识

NP and OSPF default routes

Knowledge of classes and objects

Longxun series: lt8911, transfer chip for converting LVDS / Mipi DSI signal into EDP signal, lt8619, HDMI / MHL signal into LVDS / RGB signal

有关imap-tools模块实现邮件移动的问题

【WPF】转换器Converter
随机推荐
What is websocket
Knowledge of classes and objects
MySQL simple operation statement
Blue Bridge Cup -- sequence sequencing problem
Cjc5340, ADC, DAC, replacement cs5340, digital to analog converter, 100db192khz multi bit audio A / D converter, audio codec100db192khz multi bit audio A / D converter
Use of checkbox in thymleaf engine
BGP automatic route aggregation
蓝桥杯——数列排序问题
NP、OSPF邻居邻接关系
NP and OSPF monitoring and commissioning
NP, OSPF stub area
Solve the problem of Chinese garbled code
【WPF】自定义控件
蓝桥杯——十六进制与十进制之间的转换
光华芯音频Codec大全CJC4344、CJC8988、CJC5340、CJC6811光华芯音频codec、CJC4344、CJC8988、替换顺芯ES8388、替换cs5340
宏晶微MS9288、模拟RGB(VGA)转HDMI,HDMI发射器,音频解码器,内置MCU
金融信息安全实训——22/4/18
NP, OSPF Stub Area
NP、OSPF 故障排除
【C#】重塑矩阵(交错数组)