Skip to content

Instantly share code, notes, and snippets.

@JeasonWong
Created September 7, 2017 07:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JeasonWong/e9de394f492d2fb1eacb3c581a064e6b to your computer and use it in GitHub Desktop.
Save JeasonWong/e9de394f492d2fb1eacb3c581a064e6b to your computer and use it in GitHub Desktop.
工厂方法
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