- Java EE 8 Design Patterns and Best Practices
- Rhuan Rocha Jo?o Purifica??o
- 264字
- 2021-07-23 16:55:01
Implementing the EmployeeStoreManager class
The EmployeeStoreManager class is responsible for connecting with the data source as well as reading and writing employee data:
import com.packt.javaee8.entity.Employee;
import java.util.HashMap;
import java.util.Map;
public class EmployeeStoreManager {
private Map<Object, Employee> dataSource = new HashMap<>();
public void storeNew ( Employee employee ) throws Exception {
if( dataSource.containsKey( employee.getId() ) ) throw new Exception( "Data already exist" );
dataSource.put( employee.getId(), employee );
}
public void update ( Employee employee ) throws Exception {
if( !dataSource.containsKey( employee.getId() ) ) throw new Exception( "Data not exist" );
dataSource.put( employee.getId(), employee );
}
public void delete ( Employee employee ) throws Exception {
if( !dataSource.containsKey( employee.getId() ) ) throw new Exception( "Data not exist" );
dataSource.remove( employee.getId() );
}
public Employee load(Object key){
return dataSource.get( key );
}
}
In the preceding code block, we have the EmployeeStoreManager class, which is responsible for connecting with the data source and reading and writing employee data. This class works as a DAO and encapsulates all the data source complexity. It also has the dataSource attribute, which is a map that represents a data source. Furthermore, this class has the storeNew method, which is used to write new employee data represented by the object model. It also has the update method, used to write existing employee data represented by an object model. This method is used to update stored data. The delete method is also used to delete existing employee data, and the load method is used to read employee data in an object model.
- Linux運維之道(第3版)
- Kubernetes修煉手冊
- pcDuino開發實戰
- 零起點學Linux系統管理
- PLC控制系統應用與維護
- Extending Bootstrap
- 奔跑吧 Linux內核(入門篇)
- 嵌入式實時操作系統μC/OS原理與實踐
- Linux內核設計的藝術:圖解Linux操作系統架構設計與實現原理
- Learning BeagleBone
- μC/OS-III內核實現與應用開發實戰指南:基于STM32
- Zabbix監控系統之深度解析和實踐
- Java EE 8 High Performance
- 鴻蒙HarmonyOS應用開發從入門到精通
- Microsoft Azure Administrator:Exam Guide AZ-103