当前位置:网站首页>Empty object mode (3.14-3.20)
Empty object mode (3.14-3.20)
2022-04-22 05:18:00 【Look, water is water】
Introduce
In empty object mode (Null Object Pattern) in , An empty object replaces NULL Object instance checking .Null Object is not a check null , It's about reacting to a relationship that doesn't do anything . In this way Null Objects can also provide default behavior when data is not available .
In empty object mode , We create an abstract class that specifies various operations to be performed and an entity class that extends that class , Also create an empty object class that does not implement anything for this class , This empty object class will be used seamlessly where null values need to be checked .
Realization
We will create a definition operation ( ad locum , It's the name of the customer ) Of AbstractCustomer abstract class , And expanded AbstractCustomer Entity class of class . Factory CustomerFactory Returns... Based on the name passed by the customer RealCustomer or NullCustomer object .
NullPatternDemo, Our demo class uses CustomerFactory To demonstrate the use of the empty object pattern .
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 entity classes that extend the above classes .
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;
}
}
step 3
establish 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
Use CustomerFactory, Based on the name passed by the customer , To get RealCustomer or NullCustomer object .
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 output .
Customers
Rob
Not Available in Customer Database
Julie
Not Available in Customer Database
版权声明
本文为[Look, water is water]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210626121704.html
边栏推荐
- Programme de démarrage Spark: WordCount
- Supporting early and scalable discovery of information websites
- Temperature control via mqc582tt + PNET
- Nexus私服——(二) 3.2.0版 控制台安装,初始密码位置
- 常见的测试方式
- Cached and condensed: Web cache degradation in the wild
- The problem that the video in video becomes muted when the mouse scrolls
- [matlab] draw Zernike polynomials
- Reset kubernetes cluster
- (2022.1.31-2022.2.14) template mode analysis
猜你喜欢

Nexus private server - (II) console installation of version 3.2.0, initial password location

2022-1-17 to 2022-1-30 iterator mode

Database (II) addition, deletion, modification and query of MySQL table (basic)

Temperature control via mqc582tt + PNET

How to initiate mqtt 100 million connection and 10 million message throughput performance test

Generic types in classes and generic types

Chapter V function

Visitor mode from 2022-1-3 to 2022-1-16

MySQL encoding problem

Spark starter: wordcount
随机推荐
Leetcode 1557. Minimum number of vertices to reach all nodes
Batch resolves the IP address of the domain name and opens the web page
MySQL数据库第十一次
Chapter 6 Association query
Write down the daily temperature
【Redis笔记】数据结构和对象:字典
Introduction to esmodule specification
Chapter IV constraints and indexes
Generic types in classes and generic types
Western dichotomy and Eastern trisection
The chain of implicit trust: an analysis of the web third party resources loading
Chapter IX user and authority
MySQL encoding problem
Go array slice
MySQL view character set and proofing rules
Final Cut Pro mosaics in multiple places of the video at the same time
萌新看过来 | WeDataSphere 开源社区志愿者招募
Introduction to swagger UI
【Pytorch】Tensor. Use and understanding of continguous()
Basic concepts of outh2