A: 创造性模式
1. 工厂方法模式(FactoryMethod)
1.1 类图
1.2 代码1
public interface Pet { public String petSound(); } public class Cat implements Pet { @Override public String petSound() { return "Meaw Meaw..."; } } public class Dog implements Pet { @Override public String petSound() { return "Bow Bow..."; } } public class PetFactory { public Pet getPet(String petType){ Pet pet = null; if("Bow".equals(petType)){ pet = new Dog(); }else if("Meaw".equals(petType)){ pet = new Cat(); } return pet; } } public class SampleFactoryMethod { public static void main(String[] args){ PetFactory factory = new PetFactory(); Pet pet = factory.getPet("Bow"); System.out.println(pet.petSound()); } }