Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
工厂方法
public class Factory {
public static void main(String[] args) {
Dog dog = new FactoryDog().create();
dog.walk();
Cat cat = new FactoryCat().create();
cat.walk();
}
public interface Animal {
void walk();
}
public static class Dog implements Animal {
@Override
public void walk() {
System.out.println(">>> dog");
}
}
public static class Cat implements Animal {
@Override
public void walk() {
System.out.println(">>> cat");
}
}
public interface FactoryAnimal {
Animal create();
}
public static class FactoryDog implements FactoryAnimal {
@Override
public Dog create() {
return new Dog();
}
}
public static class FactoryCat implements FactoryAnimal {
@Override
public Cat create() {
return new Cat();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment