简单的工厂模式代码
public interface Work{
void doWork();
}
public class EmployeeWork implements Work {
public void doWork() {
System.out.println("员工*工作");
}
}
public class BossWork implements Work {
public void doWork() {
System.out.println("老板审批");
}
}
public interface IWorkFactory {
Work getWork();
}
public class EmployeeWorkFactory implements IWorkFactory {
public Work getWork() {
return new EmployeeWork();
}
}
public class BossWorkFactory implements IWorkFactory {
public Work getWork() {
return new BossWork();
}
}
public class Test {
public static void main(String[] args){
IWorkFactory EwFactory = new EmployeeWorkFactory();
EwFactory.getWork().doWork();
IWorkFactory BwFactory = new BossWorkFactory();
BwFactory.getWork().doWork();
}
}