美文网首页
Factory Pattern in Java

Factory Pattern in Java

作者: Lance_Xu | 来源:发表于2019-04-25 15:28 被阅读0次

    引言

    在Java编程中创建一个实例对象你最常用什么方法,我想莫过于通过new关键字吧,new关键字实例化对象是Java中最常见也是最古老的方式了;如果你的对象在整个应用程序中传播,这时候却需要修改对象的实例化过程,那么此时不得不在每个实例化过程中修改,想必这是个十分痛苦的过程,此时你应该考虑使用工厂模式来重构你的代码。

    现实中,工厂通常是流水线上加工生产出来不同功能的产品,但是产品又有功能上的相似,这种模式被应用于计算器的编程实现中,帮助工程师构建扩展性更好的程序。

    工厂模式应用场景

    工厂模式在类之间引入了松耦合设计原则,这是一条设计应用程序体系结构时必须考虑和应用的设计原则。针对抽象实体而不针对具体实现进行编程,在设计阶段引入松耦合原则可以使我们的系统架构更加灵活和健壮。

    class relationship

    上面的类图描绘了一个常见的场景,使用汽车工厂的一个例子,它能够建造3种类型的汽车,即小型汽车,轿车和豪华汽车。制造汽车需要从零件制造到最终出厂有许多步骤,计算机编程中我们将这些步骤定义为方法函数,并且应该在创建特定汽车类型的实例时调用。

    工厂模式案例

    根据上面类图展示Car工厂模式的实现案例:

    首先定义CarType:

    package com.iblog.pattern.factory;
    
    public enum CarType {
        SMALL, SEDAN, LUXURY
    }
    

    定义Car作为所有类型汽车的抽象父类:

    package com.iblog.pattern.factory;
    
    public abstract class Car {
        private CarType model;
    
        public Car(CarType model) {
            this.model = model;
        }
    
        private void arrangeParts() {
            // Do one time processing here
        }
    
        // Do subclass level processing in this method
        protected abstract void construct();
    
        public CarType getModel() {
            return model;
        }
    
        public void setModel(CarType model) {
            this.model = model;
        }
    }
    
    

    LuxuryCar类型汽车定义:

    package com.iblog.pattern.factory;
    
    public class LuxuryCar extends Car {
    
        LuxuryCar() {
            super(CarType.LUXURY);
            construct();
        }
    
        @Override
        protected void construct() {
            System.out.println("Building luxury car");
            // add accessories
        }
    }
    
    

    SedanCar类型汽车定义:

    package com.iblog.pattern.factory;
    
    public class SedanCar extends Car {
    
        SedanCar() {
            super(CarType.SEDAN);
            construct();
        }
    
        @Override
        protected void construct() {
            System.out.println("Building sedan car");
            // add accessories
        }
    }
    

    SmallCar类型汽车定义:

    
    public class SmallCar extends Car {
    
        SmallCar() {
            super(CarType.SMALL);
            construct();
        }
    
        @Override
        protected void construct() {
            System.out.println("Building small car");
            // add accessories
        }
    }
    

    工厂模式实现类CarFactory:

    package com.iblog.pattern.factory;
    
    public class CarFactory {
        public static Car buildCar(CarType model) {
            Car car = null;
            switch (model) {
                case SMALL:
                    car = new SmallCar();
                    break;
    
                case SEDAN:
                    car = new SedanCar();
                    break;
    
                case LUXURY:
                    car = new LuxuryCar();
                    break;
    
                default:
                    // throw some exception
                    break;
            }
            return car;
        }
    }
    
    

    工厂方法测试用例:

    package com.iblog.pattern.factory;
    
    import org.junit.Test;
    
    import static org.junit.Assert.*;
    
    public class CarFactoryTest {
    
        @Test
        public void buildCar() {
            System.out.println(CarFactory.buildCar(CarType.SMALL));
            System.out.println(CarFactory.buildCar(CarType.SEDAN));
            System.out.println(CarFactory.buildCar(CarType.LUXURY));
        }
    }
    

    结果:

    Building small car
    com.iblog.pattern.factory.SmallCar@4ee285c6
    Building sedan car
    com.iblog.pattern.factory.SedanCar@621be5d1
    Building luxury car
    com.iblog.pattern.factory.LuxuryCar@573fd745
    

    通过工厂类来实例化对象,我们无需关心工厂生产过程,未来对于改变生产过程时,我们只需要修改对应的生产函数即可,而无需在程序的各个实例化的地方修改代码。

    工厂模式的优点

    工厂模式使得创建对象不必关心过程,缩减代码的同时提高了程序的可扩展性;

    工厂类无需感应工厂方法在何处被调用,只需要专注内部生产过程;

    必须集中生成对象的生命周期管理,以确保应用程序内的一致行为。

    Jdk中的工厂模式应用

    工厂模式最适合涉及复杂对象创建步骤的场景,为了确保这些步骤是集中的而不是暴露给组合类,应该使用工厂模式。我们可以在JDK中看到许多工厂模式的实时例子,例如:

    相关文章

      网友评论

          本文标题:Factory Pattern in Java

          本文链接:https://www.haomeiwen.com/subject/xfdzgqtx.html