手写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();
    }
}

产生结果:

相关推荐
Peter(阿斯拉)几秒前
[Java性能优化]_[时间优化]_[字符串拼接的多种方法性能分析]
java·性能优化·stringbuilder·string·字符串拼接·stringbuffer·时间优化
水痕011 小时前
gin结合minio来做文件存储
java·eureka·gin
寒士obj1 小时前
Spring事物
java·spring
柯南二号2 小时前
【Java后端】Spring Boot 集成 MyBatis-Plus 全攻略
java·spring boot·mybatis
桦说编程10 小时前
Java 中如何创建不可变类型
java·后端·函数式编程
lifallen10 小时前
Java Stream sort算子实现:SortedOps
java·开发语言
IT毕设实战小研10 小时前
基于Spring Boot 4s店车辆管理系统 租车管理系统 停车位管理系统 智慧车辆管理系统
java·开发语言·spring boot·后端·spring·毕业设计·课程设计
快乐的划水a10 小时前
组合模式及优化
c++·设计模式·组合模式
没有bug.的程序员11 小时前
JVM 总览与运行原理:深入Java虚拟机的核心引擎
java·jvm·python·虚拟机
甄超锋11 小时前
Java ArrayList的介绍及用法
java·windows·spring boot·python·spring·spring cloud·tomcat