设计模式二(工厂模式)

本质:实例化对象不用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();
    }
}
相关推荐
勤奋菲菲13 分钟前
Vue3+Three.js:requestAnimationFrame的详细介绍
开发语言·javascript·three.js·前端可视化
jiunian_cn21 分钟前
【Linux】高级IO
java·linux·服务器
要天天开心啊28 分钟前
Java序列化和反序列化
java·开发语言
zz-zjx1 小时前
Tomcat核心架构与生产部署指南
java·运维·tomcat
灰灰老师1 小时前
在Ubuntu22.04和24.04中安装Docker并安装和配置Java、Mysql、Tomcat
java·mysql·docker·tomcat
二宝1521 小时前
黑马商城day1-MyBatis-Plus
java·开发语言·mybatis
235161 小时前
【MQ】RabbitMQ:架构、工作模式、高可用与流程解析
java·分布式·架构·kafka·rabbitmq·rocketmq·java-rabbitmq
Porunarufu1 小时前
JAVA·类和对象③封装及包
java·开发语言
霍小毛1 小时前
Kubernetes云平台管理实战:滚动升级与秒级回滚
java·容器·kubernetes
代码充电宝1 小时前
LeetCode 算法题【简单】20. 有效的括号
java·算法·leetcode·面试·职场和发展