1.生产工厂的工厂
package com.wuming.factory.abstract1; public class Client { 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(); System.out.println("===============华为系列产品============"); /* ===============小米系列产品============ 小米打电话 小米发短信 打开小米wifi*/ //小米工厂 HuaweiFactory huaweiFactory = new HuaweiFactory(); iphoneProduct= huaweiFactory.iphoneProduct(); iphoneProduct.callup(); iphoneProduct.sendSMS(); irouterProduct= huaweiFactory.routerProduct(); irouterProduct.openWife(); /* ===============华为系列产品============ 华为手机通话 华为手机发短信 打开华为wifi*/ } }
package com.wuming.factory.abstract1; public class HuaweiFactory implements IProductFactory { @Override public IphoneProduct iphoneProduct() { return new HuaWeiPhone(); } @Override public IrouterProduct routerProduct() { return new HuaweiRouter(); } }
package com.wuming.factory.abstract1; 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("华为手机发短信"); } }
package com.wuming.factory.abstract1; 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 setting() { System.out.println("华为设置"); } }
package com.wuming.factory.abstract1; //手机产品接口 public interface IphoneProduct { void start(); void shutdown(); void callup(); void sendSMS(); }
package com.wuming.factory.abstract1; //抽象工厂模式(不可以增加产品,可以增加产品族) public interface IProductFactory { //生产手机 IphoneProduct iphoneProduct(); //生产漏油器 IrouterProduct routerProduct(); }
package com.wuming.factory.abstract1; //漏油器产品接口 public interface IrouterProduct { void start(); void shutdown(); void openWife(); void setting(); }
package com.wuming.factory.abstract1; public class XiaomiFactory implements IProductFactory{ @Override public IphoneProduct iphoneProduct() { return new XiaomiPhone(); } @Override public IrouterProduct routerProduct() { return new XiaomiRouter(); } }
package com.wuming.factory.abstract1; 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("小米发短信"); } }
package com.wuming.factory.abstract1; 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 setting() { System.out.println("小米设置"); } }