mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-16 20:59:07 +00:00
* #1299 IMPLEMENT IDENTITY MAP PATTERN. * #1299 Add docstrings, README, testCases and class diagram. * #1299 Update a comment string in DB implementation. * #1299 Fix code smells. * #1299 Fix code smells and add comments to App.java * #1299 Update constant name. * #1299 Remove java version dependency and update README.md. * #1299 Add lombok to PersonFinder.java. * #1299 Add dependency to maven-assembly-plugin. * #1299 Use java streams in PersonDbSimulatorImplementation.java. * #1299 Add print statements while returning the person object. * #1299 Update README.md. * #1299 Add puml file. * Update README.md
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
|
||||
*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2022 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.identitymap;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* The basic idea behind the Identity Map is to have a series of maps containing objects that have been pulled from the database.
|
||||
* The below example demonstrates the identity map pattern by creating a sample DB.
|
||||
* Since only 1 DB has been created we only have 1 map corresponding to it for the purpose of this demo.
|
||||
* When you load an object from the database, you first check the map.
|
||||
* If there’s an object in it that corresponds to the one you’re loading, you return it. If not, you go to the database,
|
||||
* putting the objects on the map for future reference as you load them.
|
||||
*/
|
||||
@Slf4j
|
||||
public class App {
|
||||
/**
|
||||
* Program entry point.
|
||||
*
|
||||
* @param args command line args.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
||||
// Dummy Persons
|
||||
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);
|
||||
|
||||
// Init database
|
||||
PersonDbSimulatorImplementation db = new PersonDbSimulatorImplementation();
|
||||
db.insert(person1);
|
||||
db.insert(person2);
|
||||
db.insert(person3);
|
||||
db.insert(person4);
|
||||
db.insert(person5);
|
||||
|
||||
// Init a personFinder
|
||||
PersonFinder finder = new PersonFinder();
|
||||
finder.setDb(db);
|
||||
|
||||
// Find persons in DataBase not the map.
|
||||
LOGGER.info(finder.getPerson(2).toString());
|
||||
LOGGER.info(finder.getPerson(4).toString());
|
||||
LOGGER.info(finder.getPerson(5).toString());
|
||||
// Find the person in the map.
|
||||
LOGGER.info(finder.getPerson(2).toString());
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
|
||||
*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2022 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.identitymap;
|
||||
|
||||
public class IdNotFoundException extends RuntimeException {
|
||||
public IdNotFoundException(final String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
|
||||
*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2022 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.identitymap;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* This class stores the map into which we will be caching records after loading them from a DataBase.
|
||||
* Stores the records as a Hash Map with the personNationalIDs as keys.
|
||||
*/
|
||||
@Slf4j
|
||||
@Getter
|
||||
public class IdentityMap {
|
||||
private Map<Integer, Person> personMap = new HashMap<>();
|
||||
/**
|
||||
* Add person to the map.
|
||||
*/
|
||||
public void addPerson(Person person) {
|
||||
if (!personMap.containsKey(person.getPersonNationalId())) {
|
||||
personMap.put(person.getPersonNationalId(), person);
|
||||
} else { // Ensure that addPerson does not update a record. This situation will never arise in our implementation. Added only for testing purposes.
|
||||
LOGGER.info("Key already in Map");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Person with given id.
|
||||
*
|
||||
* @param id : personNationalId as requested by user.
|
||||
*/
|
||||
public Person getPerson(int id) {
|
||||
Person person = personMap.get(id);
|
||||
if (person == null) {
|
||||
LOGGER.info("ID not in Map.");
|
||||
return null;
|
||||
}
|
||||
LOGGER.info(person.toString());
|
||||
return person;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the size of the map.
|
||||
*/
|
||||
public int size() {
|
||||
if (personMap == null) {
|
||||
return 0;
|
||||
}
|
||||
return personMap.size();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
|
||||
*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2022 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.identitymap;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
/**
|
||||
* Person definition.
|
||||
*/
|
||||
@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;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
|
||||
*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2022 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.identitymap;
|
||||
|
||||
public interface PersonDbSimulator {
|
||||
Person find(int personNationalID);
|
||||
|
||||
void insert(Person person);
|
||||
|
||||
void update(Person person);
|
||||
|
||||
void delete(int personNationalID);
|
||||
|
||||
}
|
||||
+108
@@ -0,0 +1,108 @@
|
||||
/*
|
||||
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
|
||||
*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2022 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.identitymap;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* This is a sample database implementation. The database is in the form of an arraylist which stores records of
|
||||
* different persons. The personNationalId acts as the primary key for a record.
|
||||
* Operations :
|
||||
* -> find (look for object with a particular ID)
|
||||
* -> insert (insert record for a new person into the database)
|
||||
* -> update (update the record of a person). To do this, create a new person instance with the same ID as the record you
|
||||
* want to update. Then call this method with that person as an argument.
|
||||
* -> delete (delete the record for a particular ID)
|
||||
*/
|
||||
@Slf4j
|
||||
public class PersonDbSimulatorImplementation implements PersonDbSimulator {
|
||||
|
||||
//This simulates a table in the database. To extend logic to multiple tables just add more lists to the implementation.
|
||||
private List<Person> personList = new ArrayList<>();
|
||||
static final String NOT_IN_DATA_BASE = " not in DataBase";
|
||||
static final String ID_STR = "ID : ";
|
||||
|
||||
@Override
|
||||
public Person find(int personNationalID) throws IdNotFoundException {
|
||||
Optional<Person> elem = personList.stream().filter(p -> p.getPersonNationalId() == personNationalID).findFirst();
|
||||
if (elem.isEmpty()) {
|
||||
throw new IdNotFoundException(ID_STR + personNationalID + NOT_IN_DATA_BASE);
|
||||
}
|
||||
LOGGER.info(elem.get().toString());
|
||||
return elem.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void insert(Person person) {
|
||||
Optional<Person> elem = personList.stream().filter(p -> p.getPersonNationalId() == person.getPersonNationalId()).findFirst();
|
||||
if (elem.isPresent()) {
|
||||
LOGGER.info("Record already exists.");
|
||||
return;
|
||||
}
|
||||
personList.add(person);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(Person person) throws IdNotFoundException {
|
||||
Optional<Person> elem = personList.stream().filter(p -> p.getPersonNationalId() == person.getPersonNationalId()).findFirst();
|
||||
if (elem.isPresent()) {
|
||||
elem.get().setName(person.getName());
|
||||
elem.get().setPhoneNum(person.getPhoneNum());
|
||||
LOGGER.info("Record updated successfully");
|
||||
return;
|
||||
}
|
||||
throw new IdNotFoundException(ID_STR + person.getPersonNationalId() + NOT_IN_DATA_BASE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the record corresponding to given ID from the DB.
|
||||
*
|
||||
* @param id : personNationalId for person whose record is to be deleted.
|
||||
*/
|
||||
public void delete(int id) throws IdNotFoundException {
|
||||
Optional<Person> elem = personList.stream().filter(p -> p.getPersonNationalId() == id).findFirst();
|
||||
if (elem.isPresent()) {
|
||||
personList.remove(elem.get());
|
||||
LOGGER.info("Record deleted successfully.");
|
||||
return;
|
||||
}
|
||||
throw new IdNotFoundException(ID_STR + id + NOT_IN_DATA_BASE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the size of the database.
|
||||
*/
|
||||
public int size() {
|
||||
if (personList == null) {
|
||||
return 0;
|
||||
}
|
||||
return personList.size();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
|
||||
*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2022 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.identitymap;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* Any object of this class stores a DataBase and an Identity Map. When we try to look for a key we first check if
|
||||
* it has been cached in the Identity Map and return it if it is indeed in the map.
|
||||
* If that is not the case then go to the DataBase, get the record, store it in the
|
||||
* Identity Map and then return the record. Now if we look for the record again we will find it in the table itself which
|
||||
* will make lookup faster.
|
||||
*/
|
||||
@Slf4j
|
||||
@Getter
|
||||
@Setter
|
||||
public class PersonFinder {
|
||||
private static final long serialVersionUID = 1L;
|
||||
// Access to the Identity Map
|
||||
private IdentityMap identityMap = new IdentityMap();
|
||||
private PersonDbSimulatorImplementation db = new PersonDbSimulatorImplementation();
|
||||
/**
|
||||
* get person corresponding to input ID.
|
||||
*
|
||||
* @param key : personNationalId to look for.
|
||||
*/
|
||||
public Person getPerson(int key) {
|
||||
// Try to find person in the identity map
|
||||
Person person = this.identityMap.getPerson(key);
|
||||
if (person != null) {
|
||||
LOGGER.info("Person found in the Map");
|
||||
return person;
|
||||
} else {
|
||||
// Try to find person in the database
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user