简介
围绕一个超级工厂创建其他工厂.该超级工厂又称为其他工厂的工厂.
UML
code
public class Clinet {
public static void main(String[] args) {
System.out.println("==================");
XiaomiFactory xiaomiFactory = new XiaomiFactory();
IphoneProduct iphoneProduct = xiaomiFactory.iphoneProduct();
iphoneProduct.callup();
iphoneProduct.sendSMS();
IRouterProduct iRouterProduct = xiaomiFactory.routerProduct();
iRouterProduct.openWife();
}
}
public class HuaweiFactory implements IProductFactory {
@Override
public IphoneProduct iphoneProduct() {
return new HuaweiPhone();
}
@Override
public IRouterProduct routerProduct() {
return new HuaweiRouter();
}
}
public class HuaweiPhone implements IphoneProduct {
@Override
public void start() {
System.out.println("开启华为手机");
}
@Override
public void shutdown() {
System.out.println("关闭华为手机");
}
@Override
public void callup() {
System.out.println("华为打电话");
}
@Override
public void sendSMS() {
System.out.println("华为发短信");
}
}
public class HuaweiRouter implements IRouterProduct {
@Override
public void start() {
System.out.println("启动华为路由器");
}
@Override
public void shutdown() {
System.out.println("关闭华为路由器");
}
@Override
public void openWife() {
System.out.println("打开华为wifi");
}
@Override
public void seeting() {
System.out.println("华为设置");
}
}
public interface IphoneProduct {
void start();
void shutdown();
void callup();
void sendSMS();
}
public interface IProductFactory {
IphoneProduct iphoneProduct();
IRouterProduct routerProduct();
}
public interface IRouterProduct {
void start();
void shutdown();
void openWife();
void seeting();
}
public class XiaomiFactory implements IProductFactory {
@Override
public IphoneProduct iphoneProduct() {
return new XiaomiPhone();
}
@Override
public IRouterProduct routerProduct() {
return new XiaomiRouter();
}
}
public class XiaomiPhone implements IphoneProduct {
@Override
public void start() {
System.out.println("开启小米手机");
}
@Override
public void shutdown() {
System.out.println("关闭小米手机");
}
@Override
public void callup() {
System.out.println("用小米手机打电话");
}
@Override
public void sendSMS() {
System.out.println("小米手机发短信");
}
}
public class XiaomiRouter implements IRouterProduct {
@Override
public void start() {
System.out.println("启动小米路由器");
}
@Override
public void shutdown() {
System.out.println("关闭小米路由器");
}
@Override
public void openWife() {
System.out.println("打开小米wifi");
}
@Override
public void seeting() {
System.out.println("小米设置");
}
}