⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ex8_21.txt

📁 j2ee core design patterns
💻 TXT
字号:
Example 8.21 	public class EmployeeStateManager implements StateManager {
    boolean isNew = true;
    private Employee employee;
    private PersistenceManager pm;

    public EmployeeStateManager(PersistenceManager pm, Employee employee) {
        this.employee = employee;
    }

    public void flush() {
        if ( pm.isDirty( employee) ){
            EmployeeTO to =
                    new EmployeeTO(employee.getId(),
                            employee.getLastName(),
                            employee.getFirstName(),
                            employee.getSS(),
                            employee.getSalary(),
                            employee.getDivisionId());

            EmployeeStoreManager storeManager = new EmployeeStoreManager();
            if (isNew) {
                storeManager.storeNew(to);
                isNew = false;
            } else {
                storeManager.update(to);
            }
            pm.resetDirty( employee );
        }
    }

    public void load() {
        EmployeeStoreManager storeManager = new EmployeeStoreManager();
        EmployeeTO to = storeManager.load(employee.getId());
        updateEmployee(to);
    }

    private void updateEmployee(EmployeeTO to) {
        employee.setId(to.id);
        employee.setLastName(to.lastName);
        employee.setFirstName(to.firstName);
        employee.setSS(to.ss);
        employee.setSalary(to.salary);
        employee.setDivisionId(to.divisionId);
        isNew = false;
    }

    public boolean needLoading() {
        if( pm.needLoading(employee) )
            return true;
        else
            return false;
    }
} 
EmployeeTO Class and EmployeeStoreManager Class
public class EmployeeTO {
	public String id;
	public String lastName;
	public String firstName;
	public String ss;
	public float   salary;
	public String divisionId;

	public EmployeeTO(String id, String lastName,
			String firstName, String ss, float salary,
			String divisionId ) {
		this.id = id;
		this.lastName = lastName;
		this.firstName = firstName;
		this.ss = ss;
		this.salary = salary;
 		this.divisionId = divisionId;
	}
}

public class EmployeeStoreManager {
	public void storeNew(EmployeeTO to) {
		String sql = "Insert into Employee( id, last_name," +
				" first_name, ss, salary, division_id ) " +
				" values( '?', '?', '?', '?', '?', '?' )";
		. . .
	}

	public void update(EmployeeTO to) {
		String sql = "Update Employee set last_name = '?'," +
				" first_name = '?', salary = '?'," +
				" division_id = '?' where id = '?'";
		. . .
	}

	public void delete(String empId) {
		String sql = "Delete from Employee where id = '?'";
		. . .
	}

	public EmployeeTO load(String empId) {
		. . .
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -