设计模式二(工厂模式)

本质:实例化对象不用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();
    }
}
相关推荐
yingjie1109 分钟前
快榜GEO|官网 AI 可读性自查 12 项(附 Python 检测脚本)
开发语言·人工智能·python
Felven27 分钟前
C. Make Them Equal
c语言·开发语言·前端
tryCbest1 小时前
Python 爬虫扩展篇(二):验证码的检测与人工介入——把“拦路虎”当作信号,而不是要征服的对手
开发语言·爬虫·python
Rain的Java大神之路1 小时前
10万QPS订单系统:缓存、ES、MQ三道连环题,我把踩过的坑一次讲透
java·后端·面试
Rain的Java大神之路2 小时前
LeetCode 45 跳跃游戏 II:大厂面试官最爱追问的贪心题,我把骨头给你拆干净了
java·后端·leetcode
风落青芜3 小时前
排序算法(四):归并排序、计数排序与算法总结
c语言·开发语言·数据结构·算法·排序算法
Resean02233 小时前
讲透Java(14)泛型的本质:类型擦除与桥接方法
java·开发语言
智能收银研究所4 小时前
收银系统选型对比:美团、客如云、拉卡拉、收钱吧各适合什么门店
java·前端·数据库
七夜zippoe5 小时前
金仓 Kingbase 安装与 Java 连接(保姆级):从装库到 JDBC 跑通全记录
java·数据库·jdbc·kingbase·金仓