一.简单工厂模式
使用简单工厂模式模拟女娲(Nvwa)造人(Person),如果传入参数M,则返回一个Man对象,如果传入参数W,则返回一个Woman对象,请实现该场景。现需要增加一个新的Robot类,如果传入参数R,则返回一个Robot对象,对代码进行修改并注意女娲的变化。
package 女娲造人;
public interface People {
public void makepeople();
}
public class Man implements
People {
public void makepeople() {
System.out.println("出现了男人");
}
}
public class Woman implements
People{
public void makepeople() {
System.out.println("出现了女人");
}
}
public class Robot implements
People{
public void makepeople() {
System.out.println("出现了机器人");
}
}
public class Nvzr {
public static People
getPeople(String arg) {
People people=null;
if(arg.equalsIgnoreCase("M")) {
people=new Man();
System.out.println("女娲造了男人");
}
else if(arg.equalsIgnoreCase("W"))
{
people=new Woman();
System.out.println("女娲造了女人");
}
else if(arg.equalsIgnoreCase("R"))
{
people=new Robot();
System.out.println("女娲造了机器人");
}
return people;
}
}