Ensures that each object gets loaded only once by keeping every loaded object in a map. Looks up objects using the map when referring to them.
Real world example
We are writing a program which the user may use to find the records of a given person in a database.
In plain words
Construct an Identity map which stores the records of recently searched for items in the database. When we look for the same record next time load it from the map do not go to the database.
Wikipedia says
In the design of DBMS, the identity map pattern is a database access design pattern used to improve performance by providing a context-specific, in-memory cache to prevent duplicate retrieval of the same object data from the database
Programmatic Example
1@EqualsAndHashCode(onlyExplicitlyIncluded = true)
2@Getter
3@Setter
4@AllArgsConstructor
5public final class Person implements Serializable {
6
7 private static final long serialVersionUID = 1L;
8
9 @EqualsAndHashCode.Include
10 private int personNationalId;
11 private String name;
12 private long phoneNum;
13
14 @Override
15 public String toString() {
16
17 return "Person ID is : " + personNationalId + " ; Person Name is : " + name + " ; Phone Number is :" + phoneNum;
18
19 }
20
21}
1@Slf4j
2@Getter
3@Setter
4public class PersonFinder {
5 private static final long serialVersionUID = 1L;
6 // Access to the Identity Map
7 private IdentityMap identityMap = new IdentityMap();
8 private PersonDbSimulatorImplementation db = new PersonDbSimulatorImplementation();
9 /**
10 * get person corresponding to input ID.
11 *
12 * @param key : personNationalId to look for.
13 */
14 public Person getPerson(int key) {
15 // Try to find person in the identity map
16 Person person = this.identityMap.getPerson(key);
17 if (person != null) {
18 LOGGER.info("Person found in the Map");
19 return person;
20 } else {
21 // Try to find person in the database
22 person = this.db.find(key);
23 if (person != null) {
24 this.identityMap.addPerson(person);
25 LOGGER.info("Person found in DB.");
26 return person;
27 }
28 LOGGER.info("Person with this ID does not exist.");
29 return null;
30 }
31 }
32}
1@Slf4j
2@Getter
3public class IdentityMap {
4 private Map<Integer, Person> personMap = new HashMap<>();
5 /**
6 * Add person to the map.
7 */
8 public void addPerson(Person person) {
9 if (!personMap.containsKey(person.getPersonNationalId())) {
10 personMap.put(person.getPersonNationalId(), person);
11 } else { // Ensure that addPerson does not update a record. This situation will never arise in our implementation. Added only for testing purposes.
12 LOGGER.info("Key already in Map");
13 }
14 }
15
16 /**
17 * Get Person with given id.
18 *
19 * @param id : personNationalId as requested by user.
20 */
21 public Person getPerson(int id) {
22 Person person = personMap.get(id);
23 if (person == null) {
24 LOGGER.info("ID not in Map.");
25 }
26 return person;
27 }
28
29 /**
30 * Get the size of the map.
31 */
32 public int size() {
33 if (personMap == null) {
34 return 0;
35 }
36 return personMap.size();
37 }
38
39}
1 Person person1 = new Person(1, "John", 27304159);
2 db.insert(person1);
1 PersonFinder finder = new PersonFinder();
2 finder.getPerson(1);
1 ID not in Map.
2 Person ID is:1;Person Name is:John;Phone Number is:27304159
3 Person found in DB.
1 Person ID is:1;Person Name is:John;Phone Number is:27304159
2 Person found in Map.
1public class IdNotFoundException extends RuntimeException {
2 public IdNotFoundException(final String message) {
3 super(message);
4 }
5}