文章目录
【Java设计模式】身份映射模式
一、概述
Java中的身份映射设计模式旨在确保每个对象仅被加载一次,通过将每个加载的对象保存在一个映射中,增强数据库性能和内存管理。
二、详细解释及实际示例
- 实际示例:
- 想象你正在组织一场会议,有一个登记台,每个参会者都必须在此签到。这个场景说明了Java中的身份映射模式可以防止重复对象。为了避免不必要的延误和混乱,每个参会者的详细信息在他们第一次签到时就会被输入到计算机系统中。如果同一个参会者再次来到登记台,系统会快速检索他们的详细信息,而无需他们重新提交相同的信息。这确保了每个参会者的信息都能得到高效和一致的处理,类似于身份映射模式确保一个对象仅被加载一次,并在整个应用程序中重复使用。
- 通俗解释:
- 身份映射设计模式确保每个唯一对象仅被加载一次,并从中央注册表中重复使用,防止应用程序内存中出现重复对象。
- 维基百科解释:
- 在DBMS的设计中,身份映射模式是一种数据库访问设计模式,通过提供特定于上下文的内存缓存来防止从数据库中重复检索相同对象的数据,从而提高性能。
三、Java中身份映射模式的编程示例
为了在Java编程中进行演示,假设我们已经创建了一个数据库实例,展示了身份映射模式以避免内存中的重复对象。
首先,让我们看一下Person
实体的实现及其字段:
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
@Getter
@Setter
@AllArgsConstructor
public final class Person implements Serializable {
private static final long serialVersionUID = 1L;
@EqualsAndHashCode.Include
private int personNationalId;
private String name;
private long phoneNum;
@Override
public String toString() {
return "Person ID is : " + personNationalId + " ; Person Name is : " + name + " ; Phone Number is :" + phoneNum;
}
}
下面是PersonFinder
的实现,用户将使用它在我们的数据库中搜索记录。它具有相关的数据库连接。它还维护一个IdentityMap
来存储最近读取的记录。
@Slf4j
@Getter
@Setter
public class PersonFinder {
private static final long serialVersionUID = 1L;
// 访问身份映射
private IdentityMap identityMap = new IdentityMap();
private PersonDbSimulatorImplementation db = new PersonDbSimulatorImplementation();
public Person getPerson(int key) {
// 尝试在身份映射中查找人员
Person person = this.identityMap.getPerson(key);
if (person!= null) {
LOGGER.info("Person found in the Map");
return person;
} else {
// 尝试在数据库中查找人员
person = this.db.find(key);
if (person!= null) {
this.identityMap.addPerson(person);
LOGGER.info("Person found in DB.");
return person;
}
LOGGER.info("Person with this ID does not exist.");
return null;
}
}
}
上述类中的身份映射字段只是一个以personNationalId为键,相应的人员对象为值的HashMap的抽象。以下是它的实现:
@Slf4j
@Getter
public class IdentityMap {
private Map<Integer, Person> personMap = new HashMap<>();
public void addPerson(Person person) {
if (!personMap.containsKey(person.getPersonNationalId())) {
personMap.put(person.getPersonNationalId(), person);
} else { // 确保addPerson不会更新记录。在我们的实现中,这种情况永远不会发生。仅为测试目的添加。
LOGGER.info("Key already in Map");
}
}
public Person getPerson(int id) {
Person person = personMap.get(id);
if (person == null) {
LOGGER.info("ID not in Map.");
}
return person;
}
public int size() {
if (personMap == null) {
return 0;
}
return personMap.size();
}
}
现在,让我们看看身份映射在App
的main
函数中是如何工作的。
public static void main(String[] args) {
// 虚拟人员
Person person1 = new Person(1, "John", 27304159);
Person person2 = new Person(2, "Thomas", 42273631);
Person person3 = new Person(3, "Arthur", 27489171);
Person person4 = new Person(4, "Finn", 20499078);
Person person5 = new Person(5, "Michael", 40599078);
// 初始化数据库
PersonDbSimulatorImplementation db = new PersonDbSimulatorImplementation();
db.insert(person1);
db.insert(person2);
db.insert(person3);
db.insert(person4);
db.insert(person5);
// 初始化人员查找器
PersonFinder finder = new PersonFinder();
finder.setDb(db);
// 在数据库中查找人员,而不是在映射中。
LOGGER.info(finder.getPerson(2).toString());
LOGGER.info(finder.getPerson(4).toString());
LOGGER.info(finder.getPerson(5).toString());
// 在映射中查找人员。
LOGGER.info(finder.getPerson(2).toString());
}
运行示例将产生以下控制台输出:
11:19:43.775 [main] INFO com.iluwatar.identitymap.IdentityMap -- ID not in Map.
11:19:43.780 [main] INFO com.iluwatar.identitymap.PersonDbSimulatorImplementation -- Person ID is : 2 ; Person Name is : Thomas ; Phone Number is :42273631
11:19:43.780 [main] INFO com.iluwatar.identitymap.PersonFinder -- Person found in DB.
11:19:43.780 [main] INFO com.iluwatar.identitymap.App -- Person ID is : 2 ; Person Name is : Thomas ; Phone Number is :42273631
11:19:43.780 [main] INFO com.iluwatar.identitymap.IdentityMap -- ID not in Map.
11:19:43.780 [main] INFO com.iluwatar.identitymap.PersonDbSimulatorImplementation -- Person ID is : 4 ; Person Name is : Finn ; Phone Number is :20499078
11:19:43.780 [main] INFO com.iluwatar.identitymap.PersonFinder -- Person found in DB.
11:19:43.780 [main] INFO com.iluwatar.identitymap.App -- Person ID is : 4 ; Person Name is : Finn ; Phone Number is :20499078
11:19:43.780 [main] INFO com.iluwatar.identitymap.IdentityMap -- ID not in Map.
11:19:43.780 [main] INFO com.iluwatar.identitymap.PersonDbSimulatorImplementation -- Person ID is : 5 ; Person Name is : Michael ; Phone Number is :40599078
11:19:43.780 [main] INFO com.iluwatar.identitymap.PersonFinder -- Person found in DB.
11:19:43.780 [main] INFO com.iluwatar.identitymap.App -- Person ID is : 5 ; Person Name is : Michael ; Phone Number is :40599078
11:19:43.780 [main] INFO com.iluwatar.identitymap.IdentityMap -- Person ID is : 2 ; Person Name is : Thomas ; Phone Number is :42273631
11:19:43.780 [main] INFO com.iluwatar.identitymap.PersonFinder -- Person found in the Map
11:19:43.780 [main] INFO com.iluwatar.identitymap.App -- Person ID is : 2 ; Person Name is : Thomas ; Phone Number is :42273631
四、何时在Java中使用身份映射模式
在Java应用程序中,当在单个会话或事务中对相同数据进行多次访问时,使用身份映射设计模式,以确保高效的对象映射和一致性。
五、身份映射模式在Java中的实际应用
- Java中的ORM(对象 - 关系映射)框架经常实现身份映射,以更有效地处理数据库交互,展示了该模式在Java设计模式中的重要性。
- 企业应用程序在不同的业务流程中维护一致的数据状态。
六、身份映射模式的好处和权衡
好处:
- 通过确保每个对象在内存中只有一个副本,减少内存使用。
- 防止更新期间的不一致性,因为应用程序的所有部分都引用相同的实例。
- 通过避免对相同数据的重复数据库读取,提高性能。
权衡:
- 增加了对象管理和持久化逻辑的复杂性。
- 如果管理不当,可能会导致数据过时,特别是在并发环境中。