Created
September 7, 2017 07:57
-
-
Save JeasonWong/7b871d366d9a5018f40cfaa9c0a18abe 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 AbstractFactory { | |
public static void main(String[] args) { | |
FactoryA factoryA = new FactoryA(); | |
factoryA.createAndroid().tel(); | |
factoryA.createIOS().play(); | |
FactoryB factoryB = new FactoryB(); | |
factoryB.createAndroid().tel(); | |
factoryB.createIOS().play(); | |
} | |
public interface FactoryPhone { | |
Android createAndroid(); | |
IOS createIOS(); | |
} | |
public interface Android { | |
void tel(); | |
} | |
public interface IOS { | |
void play(); | |
} | |
public static class XiaoMi implements Android { | |
@Override | |
public void tel() { | |
System.out.println("xiaomi"); | |
} | |
} | |
public static class Huawei implements Android { | |
@Override | |
public void tel() { | |
System.out.println("huawei"); | |
} | |
} | |
public static class IOS8 implements IOS { | |
@Override | |
public void play() { | |
System.out.println("iOS8"); | |
} | |
} | |
public static class IOS9 implements IOS { | |
@Override | |
public void play() { | |
System.out.println("iOS9"); | |
} | |
} | |
public static class FactoryA implements FactoryPhone { | |
@Override | |
public XiaoMi createAndroid() { | |
return new XiaoMi(); | |
} | |
@Override | |
public IOS8 createIOS() { | |
return new IOS8(); | |
} | |
} | |
public static class FactoryB implements FactoryPhone { | |
@Override | |
public Huawei createAndroid() { | |
return new Huawei(); | |
} | |
@Override | |
public IOS9 createIOS() { | |
return new IOS9(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment