创建型模式之工厂模式

2023-12-17 22:55:25

?
本质: 实例化对象不直接使用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();

    }
}

参考:
《实用软件设计模式教程》

文章来源:https://blog.csdn.net/weixin_59864324/article/details/135049907
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。