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


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

    引入颜色接口:

    public interface Colour {
        void fill();
    }
    
    • 1
    • 2
    • 3

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

    public abstract class CarAndColourFactory {
        public abstract Car getCar(String CarType);
        public abstract Colour getColour(String colour);
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

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

    public class Green implements Colour {
        @Override
        public void fill() {
            System.out.println("涂上绿色");
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    public class Red implements Colour {
        @Override
        public void fill() {
            System.out.println("涂上红色");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

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

    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;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    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;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

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

    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;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    进行汽车加工:

    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();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    产生结果:
    在这里插入图片描述

  • 相关阅读:
    数字孪生有哪些应用价值?
    【OpenCV--视频文件相关操作】
    [附源码]计算机毕业设计JAVA宿舍管理系统
    如何在Windows7 上查看.NET Framework版本
    java开发工具MyEclipse 中实体关系设计器介绍
    Springboot足球运动员训练计划管理系统的设计与实现 毕业设计-附源码281444
    【Go并发】自旋锁及Go实现
    手机cpu架构查看及armeabi、armeabi-v7a、arm64-v8a及x86等说明
    华为机试真题 C++ 实现【字符串中找出连续最长的数字串】
    sqli-labs-master安装及报错处理
  • 原文地址:https://blog.csdn.net/weixin_43365615/article/details/137968747