设计模式二(工厂模式)

本质:实例化对象不用new,用工厂代替,实现了创建者和调用者分离

满足:

开闭原则:对拓展开放,对修改关闭

依赖倒置原则:要针对接口编程

迪米特原则:最少了解原则,只与自己直接相关的类有关系

简单工厂模式

也被称为静态工厂

java 复制代码
public interface Car {
    void name();
}
public class BWM implements Car{
​
    @Override
    public void name() {
        System.out.println("宝马");
    }
}
public class DaZhong implements Car{
​
    @Override
    public void name() {
        System.out.println("大众");
    }
}
java 复制代码
public class CarFactory{
    public static Car getCar(String name){
        if(name.equals("大众")){
            return new DaZhong();
        }else if (name.equals("宝马")){
            return new BWM();
        }else{
            return null;
        }
    }
}
public class consumer {
    public static void main(String[] args) {
​
        Car car = CarFactory.getCar("大众");
        car.name();
        Car car2 = CarFactory.getCar("宝马");
        car2.name();
    }
}

总结

将创建对象的任务交给工厂去完成

缺点

不满足开闭原则,如果我们新创建一个车,就需要修改CarFactory的源代码

工厂方法模式

多个工厂对应多个实现类

java 复制代码
public interface CarFactory {
​
    Car getCar();
}
​


public class BMWFactory implements CarFactory{
    @Override
    public Car getCar() {
        return new BWM();
    }
}
​
public class DaZhongFactory implements CarFactory{
    @Override
    public Car getCar() {
        return new DaZhong();
    }
}

如果我们想要创建新的车对象,只要创建对应的车工厂即可,无需修改CarFactory的代码

java 复制代码
public class Aodi implements Car {
    @Override
    public void name() {
        System.out.println("奥迪");
    }
}

public class AodiFactory implements CarFactory{
    @Override
    public Car getCar() {
        return new Aodi();
    }
}

public class consumer {
    public static void main(String[] args) {
        Car car = new DaZhongFactory().getCar();
        car.name();
        Car car1 = new BMWFactory().getCar();
        car1.name();

        Car car2 = new AodiFactory().getCar();
        car2.name();
    }
}

抽象工厂模式

围绕一个超级工厂生产工厂,该工厂又称为其他工厂的工厂 (抽象的抽象)

java 复制代码
public interface IProductFactory {

    IPhoneProduct iphoneproduct();

    IRouterProduct irouterproduct();
}

具体的产品工厂

java 复制代码
public class XiaomiFactory implements IProductFactory{
    @Override
    public IPhoneProduct iphoneproduct() {
        return new XiaomiPhone();
    }

    @Override
    public IRouterProduct irouterproduct() {
        return new XiaomiRouter();
    }
}
java 复制代码
public class HuaweiFactory implements IProductFactory{
    @Override
    public IPhoneProduct iphoneproduct() {
        return new HuaweiPhone();
    }

    @Override
    public IRouterProduct irouterproduct() {
        return new HuaweiRouter();
    }
}

产品功能

java 复制代码
public interface IPhoneProduct {

    void open();

    void close();
}
java 复制代码
public interface IRouterProduct {

    void open();
    void close();
    
}

具体实现

java 复制代码
public class XiaomiPhone implements IPhoneProduct{
    @Override
    public void open() {
        System.out.println("小米手机开机");
    }

    @Override
    public void close() {
        System.out.println("小米手机关机");
    }
}
java 复制代码
public class HuaweiPhone implements IPhoneProduct{
    @Override
    public void open() {
        System.out.println("华为手机开机");
    }

    @Override
    public void close() {
        System.out.println("华为手机关机");
    }
}
java 复制代码
public class XiaomiRouter implements IRouterProduct{
    @Override
    public void open() {
        System.out.println("小米路由器开机");
    }

    @Override
    public void close() {
        System.out.println("小米路由器关机");
    }
}
java 复制代码
public class HuaweiRouter implements IRouterProduct{
    @Override
    public void open() {
        System.out.println("华为路由器开机");
    }

    @Override
    public void close() {
        System.out.println("华为路由器关机");
    }
}

测试

java 复制代码
public class consumer {

    public static void main(String[] args) {
        //先创建工厂
        System.out.println("==========小米==========");
        IPhoneProduct product = new XiaomiFactory().iphoneproduct();
        product.open();
        product.close();
        IRouterProduct irouterproduct = new XiaomiFactory().irouterproduct();
        irouterproduct.open();
        irouterproduct.close();
        System.out.println("===========华为==========");
        IPhoneProduct iphoneproduct = new HuaweiFactory().iphoneproduct();
        iphoneproduct.open();
        iphoneproduct.close();
        IRouterProduct irouterproduct1 = new HuaweiFactory().irouterproduct();
        irouterproduct1.open();
        irouterproduct1.close();
    }
}
相关推荐
躺平大鹅1 小时前
Java面向对象入门(类与对象,新手秒懂)
java
静水流深_沧海一粟2 小时前
04 | 别再写几十个参数的构造函数了——建造者模式
设计模式
StarkCoder2 小时前
从UIKit到SwiftUI的迁移感悟:数据驱动的革命
设计模式
初次攀爬者2 小时前
RocketMQ在Spring Boot上的基础使用
java·spring boot·rocketmq
花花无缺2 小时前
搞懂@Autowired 与@Resuorce
java·spring boot·后端
Derek_Smart4 小时前
从一次 OOM 事故说起:打造生产级的 JVM 健康检查组件
java·jvm·spring boot
NE_STOP5 小时前
MyBatis-mybatis入门与增删改查
java
孟陬8 小时前
国外技术周刊 #1:Paul Graham 重新分享最受欢迎的文章《创作者的品味》、本周被划线最多 YouTube《如何在 19 分钟内学会 AI》、为何我不
java·前端·后端
想用offer打牌8 小时前
一站式了解四种限流算法
java·后端·go
华仔啊9 小时前
Java 开发千万别给布尔变量加 is 前缀!很容易背锅
java