工厂方法模式---实例
程序员文章站
2022-03-09 16:15:37
...
某人力经理需要维护一份公司雇员的明细单,公司雇员分总经理,项目经理,项目组领导,资深软件工程师,新手软件工程师等五个等级,薪水分别对应12000,9000,7000,5000,2000,现输入是姓名和职位,要求将这两个信息转化为对应的实例.
根据工厂模式实现的类可以根据提供的数据生成一组类中某一个类的实例,通常这一组类有一个公共的抽象父类并且实现了相同的方法,但是这些方法针对不同的数据进行了不同的操作。首先需要定义一个基类,该类的子类通过不同的方法实现了基类中的方法。然后需要定义一个工厂类,工厂类可以根据条件生成不同的子类实例。当得到子类的实例后,开发人员可以调用基类中的方法而不必考虑到底返回的是哪一个子类的实例。
基类
public abstract class Employee{
protected String name;
protected String title;
protected int salary;
public static final String Title_JuniorSoftwareEngineer="JSE";
public static final String Title_SeniorSoftwareEngineer="SSE";
public static final String Title_TeamLeader="TL";
public static final String Title_ProjectLeader="PL";
public static final String Title_GeneralManager="GM";
public Employee(String name,String title){
this.name=name;
this.title=title;
}
public String toString(){
return "雇员名="+name+" 雇员职位="+title+" 薪水="+salary;
}
}
各类
public class GeneralManager extends Employee{
public GeneralManager(String name){
super(name,Employee.Title_GeneralManager);
this.salary=12000;
}
}
class ProjectLeader extends Employee{
public ProjectLeader(String name){
super(name,Employee.Title_ProjectLeader);
this.salary=9000;
}
}
class TeamLeader extends Employee{
public TeamLeader(String name){
super(name,Employee.Title_TeamLeader);
this.salary=7000;
}
}
class SeniorSoftwareEngineer extends Employee{
public SeniorSoftwareEngineer(String name){
super(name,Employee.Title_SeniorSoftwareEngineer);
this.salary=5000;
}
}
class JuniorSoftwareEngineer extends Employee{
public JuniorSoftwareEngineer(String name){
super(name,Employee.Title_JuniorSoftwareEngineer);
this.salary=2000;
}
}
package com.xzy;
import java.util.ArrayList;
import java.util.List;
public class HrManager{
private List<Employee> employeeList;
private void addEmployee(Employee newEmployee){
if(employeeList==null){
employeeList=new ArrayList<Employee>();
}
employeeList.add(newEmployee);
}
public void addEmployee(String name,String title){
addEmployee(EmployeeFactory.generate(name, title));
}
public void printEmployeeList(){
for(Employee employee:employeeList){
System.out.println(employee);
}
}
}
工厂类,生成各个实例
package com.xzy;
public class EmployeeFactory {
public static Employee generate(String name, String title) {
if (title.equals(Employee.Title_GeneralManager)) {
return new GeneralManager(name);
} else if (title.equals(Employee.Title_ProjectLeader)) {
return new ProjectLeader(name);
} else if (title.equals(Employee.Title_TeamLeader)) {
return new TeamLeader(name);
} else if (title.equals(Employee.Title_SeniorSoftwareEngineer)) {
return new SeniorSoftwareEngineer(name);
} else {
return new JuniorSoftwareEngineer(name);
}
}
}
public class Main{
public static void main(String[] args){
HrManager hrManager=new HrManager();
hrManager.addEmployee("Andy","GM");
hrManager.addEmployee("Bill","PL");
hrManager.addEmployee("Cindy","TL");
hrManager.addEmployee("Dell","SSE");
hrManager.addEmployee("Edin","JSE");
hrManager.addEmployee("Felex","JSE");
hrManager.addEmployee("Green","Unknown");
hrManager.printEmployeeList();
}
}
结果:
雇员名=Andy 雇员职位=GM 薪水=12000
雇员名=Bill 雇员职位=PL 薪水=9000
雇员名=Cindy 雇员职位=TL 薪水=7000
雇员名=Dell 雇员职位=SSE 薪水=5000
雇员名=Edin 雇员职位=JSE 薪水=2000
雇员名=Felex 雇员职位=JSE 薪水=2000
雇员名=Green 雇员职位=JSE 薪水=2000
上一篇: C语言学习第015课——文件操作(二)
下一篇: 1.1 Qt qml 开端