手写Java设计模式之抽象工厂模式,附源码解读

接上篇,抽象工厂模式将汽车的一些属性可以抽象出来,可以理解为给不同汽车品牌生成时加上不同的特性,如颜色等,具体代码如下:

引入颜色接口:

java 复制代码
public interface Colour {
    void fill();
}

将颜色与汽车生成品牌抽象出来,增加抽象类:

java 复制代码
public abstract class CarAndColourFactory {
    public abstract Car getCar(String CarType);
    public abstract Colour getColour(String colour);
}

继承抽象类,分别对不同属性的特征进行操作,如涂上颜色等,首先实现颜色接口:

java 复制代码
public class Green implements Colour {
    @Override
    public void fill() {
        System.out.println("涂上绿色");
    }
}
java 复制代码
public class Red implements Colour {
    @Override
    public void fill() {
        System.out.println("涂上红色");
    }
}

汽车品牌和汽车颜色分别继承抽象类:

java 复制代码
public class CarTypeFactory extends CarAndColourFactory{
    @Override
    public Car getCar(String CarType) {
        if(CarType == null){
            return null;
        }
        if(CarType.equalsIgnoreCase("XiaoMi")){
            return new XiaoMi();
        } else if(CarType.equalsIgnoreCase("HuaWei")){
            return new HuaWei();
        } else if(CarType.equalsIgnoreCase("Tesla")){
            return new Tesla();
        }
        return null;
    }

    @Override
    public Colour getColour(String colour) {
        return null;
    }
}
java 复制代码
public class ColourFactory extends CarAndColourFactory{
    @Override
    public Car getCar(String CarType) {
        return null;
    }

    @Override
    public Colour getColour(String colour) {
        if(colour == null){
            return null;
        }
        if(colour.equalsIgnoreCase("Green")){
            return new Green();
        } else if(colour.equalsIgnoreCase("Red")){
            return new Red();
        }
            return null;
    }
}

增加一个根据参数区分增加汽车属性的加工工厂类,如下:

java 复制代码
public class FactoryProducer {
    public static CarAndColourFactory get(String choice){
         if(choice.equalsIgnoreCase("Car")){
             return new CarTypeFactory();
         }else if(choice.equalsIgnoreCase("Colour")){
             return new ColourFactory();
         }
         return null;
    }
}

进行汽车加工:

java 复制代码
public class CarTest {
    public static void main(String[] args){
       /* CarFactory carFactory = new CarFactory();
        carFactory.getCar("xiaomi").draw();
        carFactory.getCar("huawei").draw();
        carFactory.getCar("tesla").draw();*/
        CarAndColourFactory carAndColourFactory1 = FactoryProducer.get("Car");
        carAndColourFactory1.getCar("xiaomi").draw();
        CarAndColourFactory carAndColourFactory= FactoryProducer.get("Colour");
        carAndColourFactory.getColour("Green").fill();
    }
}

产生结果:

相关推荐
数据龙傲天8 分钟前
1688商品API接口:电商数据自动化的新引擎
java·大数据·sql·mysql
刷帅耍帅26 分钟前
设计模式-桥接模式
设计模式·桥接模式
带带老表学爬虫37 分钟前
java数据类型转换和注释
java·开发语言
千里码aicood44 分钟前
【2025】springboot教学评价管理系统(源码+文档+调试+答疑)
java·spring boot·后端·教学管理系统
彭于晏6891 小时前
Android广播
android·java·开发语言
程序员-珍1 小时前
使用openapi生成前端请求文件报错 ‘Token “Integer“ does not exist.‘
java·前端·spring boot·后端·restful·个人开发
MinBadGuy1 小时前
【GeekBand】C++设计模式笔记5_Observer_观察者模式
c++·设计模式
2401_857297912 小时前
招联金融2025校招内推
java·前端·算法·金融·求职招聘
刷帅耍帅2 小时前
设计模式-生成器模式/建造者模式Builder
设计模式·建造者模式
福大大架构师每日一题2 小时前
23.1 k8s监控中标签relabel的应用和原理
java·容器·kubernetes