Created
September 7, 2017 07:26
-
-
Save JeasonWong/e9de394f492d2fb1eacb3c581a064e6b to your computer and use it in GitHub Desktop.
工厂方法
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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