?
本质: 实例化对象不直接使用new,而是用工厂代替
工厂模式分为:
为了生成所需的具体产品,工厂类提供方法,根据传入的参数决定具体实例化哪个产品类。
//产品接口:车
public interface Car {
public void run();
}
//具体产品1:比亚迪
public class BYD implements Car{
@Override
public void run() {
System.out.println("比亚迪");
}
}
//具体产品2:特斯拉
public class TSL implements Car{
@Override
public 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{
@Override
public void run() {
System.out.println("比亚迪");
}
}
//具体产品2:特斯拉
public class TSL implements Car {
@Override
public void run() {
System.out.println("特斯拉");
}
}
//车工厂
public interface CarFactory {
public Car getCar();
}
//具体产品对应的工厂1:比亚迪工厂
public class BYDFactory implements CarFactory{
@Override
public Car getCar() {
return new BYD();
}
}
//具体产品对应的工厂2:特斯拉工厂
public class TSLFactory implements CarFactory{
@Override
public 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{
@Override
public void call() {
System.out.println("华为手机打电话");
}
@Override
public void sendSMS() {
System.out.println("华为手机发短信");
}
@Override
public void takePhoto() {
System.out.println("华为手机拍照");
}
}
public class XiaomiPhone implements PhoneProduct{
@Override
public void call() {
System.out.println("小米手机打电话");
}
@Override
public void sendSMS() {
System.out.println("小米手机发短信");
}
@Override
public void takePhoto() {
System.out.println("小米手机拍照");
}
}
public class HuaweiRoute implements RouteProduct{
@Override
public void start() {
System.out.println("开启华为路由器");
}
@Override
public void shutDown() {
System.out.println("关闭华为路由器");
}
@Override
public void openWifi() {
System.out.println("开启华为路由器的wifi");
}
@Override
public void setting() {
System.out.println("设置华为路由器");
}
}
public class XiaomiRoute implements RouteProduct {
@Override
public void start() {
System.out.println("开启小米路由器");
}
@Override
public void shutDown() {
System.out.println("关闭小米路由器");
}
@Override
public void openWifi() {
System.out.println("打开小米路由器的wifi");
}
@Override
public void setting() {
System.out.println("设置小米路由器");
}
}
//超级工厂
public interface ProductFactory {
PhoneProduct phoneProduct();
RouteProduct routeProduct();
}
public class HuaweiFactory implements ProductFactory{
@Override
public PhoneProduct phoneProduct() {
return new HuaweiPhone();
}
@Override
public RouteProduct routeProduct() {
return new HuaweiRoute();
}
}
public class XiaomiFactory implements ProductFactory{
@Override
public PhoneProduct phoneProduct() {
return new XiaomiPhone();
}
@Override
public 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();
}
}
参考:
《实用软件设计模式教程》