动物叫声
public interface AnimalPlugin {
void makeSound();
}
public class DogPlugin implements AnimalPlugin {
@Override
public void makeSound() {
System.out.println("汪汪汪!");
}
}
public class CatPlugin implements AnimalPlugin {
@Override
public void makeSound() {
System.out.println("喵喵喵!");
}
}
public class MainApplication {
public static void main(String[] args) {
AnimalPlugin plugin = loadAnimalPlugin("dog");
if (plugin != null) {
plugin.makeSound();
}
}
private static AnimalPlugin loadAnimalPlugin(String animalType) {
if ("dog".equals(animalType)) {
return new DogPlugin();
} else if ("cat".equals(animalType)) {
return new CatPlugin();
}
return null;
}
}