本质: 实例化对象不直接使用new,而是用工厂代替
工厂模式分为:
- 简单工厂模式:用来生产同一等级结构中的任意产品(增加新产品需要修改已有代码)
- 工厂方法模式:用来生产同一等级结构中的固定产品(支持增加任意产品)
- 抽象工厂模式:围绕一个超级工厂创建其他工厂,超级工厂可看做工厂的工厂。
简单工厂模式
为了生成所需的具体产品,工厂类提供方法,根据传入的参数决定具体实例化哪个产品类。
//产品接口:车
public interface Car {public void run();
}//具体产品1:比亚迪
public class BYD implements Car{@Overridepublic void run() {System.out.println("比亚迪");}
}
//具体产品2:特斯拉
public class TSL implements Car{@Overridepublic void run() {System.out.println("特斯拉");}
}//车工厂
public class CarFactory {public static Car getCar(String carType){Car car = null;if(carType.equals("BYD")){car = new BYD();}else if(carType.equals("TSL")){car = new TSL();}return car;}
}//测试类
public class Consumer {public static void main(String[] args) {Car byd = new CarFactory().getCar("BYD");Car tsl = new CarFactory().getCar("TSL");byd.run();tsl.run();}
}
工厂方法模式
定义一个用于创建对象的接口,让子类决定去实例化哪个类,使得类的实例化延迟到子类。
示例:
//产品:车
public interface Car {public void run();
}
//具体产品1:比亚迪
public class BYD implements Car{@Overridepublic void run() {System.out.println("比亚迪");}
}
//具体产品2:特斯拉
public class TSL implements Car {@Overridepublic void run() {System.out.println("特斯拉");}
}
//车工厂
public interface CarFactory {
public Car getCar();
}//具体产品对应的工厂1:比亚迪工厂
public class BYDFactory implements CarFactory{@Overridepublic Car getCar() {return new BYD();}
}//具体产品对应的工厂2:特斯拉工厂
public class TSLFactory implements CarFactory{@Overridepublic Car getCar() {return new TSL();}
}//测试类
public class Consumer {public static void main(String[] args) {BYDFactory bydFactory = new BYDFactory();TSLFactory tslFactory = new TSLFactory();bydFactory.getCar().run();tslFactory.getCar().run();}
}
抽象工厂模式
public interface PhoneProduct {public void call();public void sendSMS();public void takePhoto();
}public interface RouteProduct {public void start();public void shutDown();public void openWifi();public void setting();}
public class HuaweiPhone implements PhoneProduct{@Overridepublic void call() {System.out.println("华为手机打电话");}@Overridepublic void sendSMS() {System.out.println("华为手机发短信");}@Overridepublic void takePhoto() {System.out.println("华为手机拍照");}
}public class XiaomiPhone implements PhoneProduct{@Overridepublic void call() {System.out.println("小米手机打电话");}@Overridepublic void sendSMS() {System.out.println("小米手机发短信");}@Overridepublic void takePhoto() {System.out.println("小米手机拍照");}
}public class HuaweiRoute implements RouteProduct{@Overridepublic void start() {System.out.println("开启华为路由器");}@Overridepublic void shutDown() {System.out.println("关闭华为路由器");}@Overridepublic void openWifi() {System.out.println("开启华为路由器的wifi");}@Overridepublic void setting() {System.out.println("设置华为路由器");}
}public class XiaomiRoute implements RouteProduct {@Overridepublic void start() {System.out.println("开启小米路由器");}@Overridepublic void shutDown() {System.out.println("关闭小米路由器");}@Overridepublic void openWifi() {System.out.println("打开小米路由器的wifi");}@Overridepublic void setting() {System.out.println("设置小米路由器");}
}//超级工厂
public interface ProductFactory {PhoneProduct phoneProduct();RouteProduct routeProduct();
}public class HuaweiFactory implements ProductFactory{@Overridepublic PhoneProduct phoneProduct() {return new HuaweiPhone();}@Overridepublic RouteProduct routeProduct() {return new HuaweiRoute();}
}
public class XiaomiFactory implements ProductFactory{@Overridepublic PhoneProduct phoneProduct() {return new XiaomiPhone();}@Overridepublic RouteProduct routeProduct() {return new XiaomiRoute();}
}//测试类
public class Consumer {public static void main(String[] args) {System.out.println("====小米系列产品====");XiaomiFactory xiaomiFactory = new XiaomiFactory();PhoneProduct xiaomiPhone = xiaomiFactory.phoneProduct();RouteProduct xiaomiRoute = xiaomiFactory.routeProduct();xiaomiPhone.call();xiaomiRoute.setting();System.out.println("====华为系列产品====");HuaweiFactory huaweiFactory = new HuaweiFactory();PhoneProduct huaweiPhone = huaweiFactory.phoneProduct();RouteProduct huaweiRoute = huaweiFactory.routeProduct();huaweiPhone.call();huaweiRoute.setting();}
}
参考:
《实用软件设计模式教程》