Skip to content

Instantly share code, notes, and snippets.

@JeasonWong
Created September 7, 2017 07:57
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/7b871d366d9a5018f40cfaa9c0a18abe to your computer and use it in GitHub Desktop.
Save JeasonWong/7b871d366d9a5018f40cfaa9c0a18abe to your computer and use it in GitHub Desktop.
抽象工厂
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