工厂方法模式只能处理同一个产品等级下的对象
public interface Human {
public void getSound();
public void getColor();
}
public class BlackHuman implements Human {
@Override
public void getSound() {
System.out.println("黑人的肤色是黑的");
}
@Override
public void getColor() {
System.out.println("黑人喜欢黑色");
}
}
public class WhiteHuman implements Human {
@Override
public void getSound() {
System.out.println("白人说英语");
}
@Override
public void getColor() {
System.out.println("白人的肤色一般是白色的");
}
}
public class YellowHuman implements Human {
@Override
public void getSound() {
System.out.println("黄种人的肤色一般是黄色的");
}
@Override
public void getColor() {
System.out.println("黄种人一般说汉语");
}
}
public abstract class MethodFac {
public static void getAction(Human human){
human.getColor();
human.getSound();
}
public abstract Human humanFac();
public static void main(String[] args) {
getAction(new BlackFac().humanFac());
}
}
public class BlackFac extends MethodFac {
public Human humanFac() {
return new BlackHuman();
}
}