Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
抽象工厂
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