FactoryPattern
设计模式——工厂模式
工厂方法模式一种创建对象的模式,它被广泛应用在jdk中以及Spring和Struts框架中;
工厂方法模式基于"输入",应用在超类和多个子类之间的情况,这种模式将创建对象的责任转移到工厂类;
public static void main(String[] args) {
peopleFactory factory = new peopleFactory();
//接口是不能实例化的,即不能new一个接口出来,但是可以创建一个接口实现类的对象
people student = factory.getPeople("student");
people teacher = factory.getPeople("teacher");
people designer = factory.getPeople("designer");
student.eat();
teacher.eat();
designer.eat();
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190311141047829.png)
public people getPeople(String peopleName){
switch (peopleName){
case "student":return new student();
case "teacher":return new teacher();
case "designer":return new designer();
default:break;
}
return null;
}