- 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.
- 每天5分鐘玩轉Kubernetes
- Getting Started with oVirt 3.3
- 高性能Linux服務器構建實戰:運維監控、性能調優與集群應用
- Linux集群和自動化運維
- Extending Bootstrap
- 嵌入式操作系統(Linux篇)(微課版)
- OpenStack系統架構設計實戰
- Delphi Programming Projects
- 計算機應用基礎(Windows 7+Office 2016)
- Troubleshooting Docker
- 鴻蒙操作系統設計原理與架構
- 嵌入式微系統
- OpenStack Essentials(Second Edition)
- Linux操作系統案例教程(第2版)
- OpenSolaris紅寶書