美文网首页
10.简单工厂模式

10.简单工厂模式

作者: 测试员 | 来源:发表于2019-11-25 23:26 被阅读0次

    基本介绍

    1)简单工厂模式是属于创建型模式,是工厂模式的一种。简单工厂模式是由一个工厂对象决定创建出哪一种产品类的实例。简单工厂模式是工厂模式家族中最简单实用的模式

    2)简单工厂模式:定义了一个创建对象的类,由这个类来封装实例化对象的行为(代码)

    3)在软件开发中,当我们会用到大量的创建某种、某类或者某批对象时,就会使用到工厂模式

    个人理解

    有个类专门创建对象,还能通过工厂类的方法定义类的最终功能。【可能理解有点问题】

    代码实现

    简单工厂类

    package com.yuan.dp.simpfactory;
    
    /**
     * 简单工厂模式【专门用来生产对象的类】
     *
     * @author Yuan-9826
     */
    public class SimpFactory<T> {
    
        private SimpFactory() {
        }
    
    
        /**
         * 获取工厂类实例对象
         *
         * @return
         */
        static SimpFactory createSimpFactory() {
    
            return new SimpFactory();
        }
    
        /**
         * 根据简单的类名获取类实例对象
         * @param name 同级别目录下的类名 简单乱写的一个方法只做参考用
         * @return
         * @throws ClassNotFoundException
         * @throws IllegalAccessException
         * @throws InstantiationException
         */
        T getProxy(String name) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
            String className = "com.yuan.dp.simpfactory." + name;
            Class<T> aClass = (Class<T>) Class.forName(className);
            T proxy = aClass.newInstance();
            return proxy;
        }
    
        @Override
        public String toString() {
            return "SimpFactory{" +
                    '}';
        }
    
    
    }
    
    

    通过工厂类创建的第一个类

    package com.yuan.dp.simpfactory;
    
    /**
     * 假装这是一个代理类
     * 只有前置增强
     * @author Yuan-9826
     */
    public class SimpProxy_1 {
    
        String before;
    
    
    
        public SimpProxy_1() {
            System.out.println(this.getClass().getSimpleName()+"类被创建啦");
        }
    
        /**
         * 假装是设置前置增强方法
         *
         * @param before
         * @return
         */
        public SimpProxy_1 setBefore(String before) {
            if (null != before && this.before == null) {
                this.before = before;
            }
            //谁调用,this就是谁
            return this;
        }
    
    
    
        @Override
        public String toString() {
            return "SimpProxy{" +
                    "before='" + before + '\'' +"成功啦"+
                    '}';
        }
    }
    
    

    通过工厂类创建的第二个类

    package com.yuan.dp.simpfactory;
    
    /**
     * 假装这是一个代理类
     * 只有后置增强
     *
     * @author Yuan-9826
     */
    public class SimpProxy_2 {
    
        String after;
    
    
        public SimpProxy_2() {
            System.out.println(this.getClass().getSimpleName()+"类被创建啦");
        }
    
    
        /**
         * 假装是设置后置增强方法
         *
         * @param after
         * @return
         */
        public SimpProxy_2 setAfter(String after) {
            if (null != after && this.after == null) {
                this.after = after;
            }
            //谁调用,this就是谁
            return this;
        }
    
        @Override
        public String toString() {
            return "SimpProxy{" +
                    ", after='" + after + '\'' +"成功啦"+
                    '}';
        }
    }
    
    

    测试类

    package com.yuan.dp.simpfactory;
    
    /**
     * 用来测试简单工厂的测试类
     *
     * @author Yuan-9826
     */
    public class FactoryTest {
        public static void main(String[] args) throws IllegalAccessException, InstantiationException, ClassNotFoundException {
            SimpFactory simpFactory = SimpFactory.createSimpFactory();
            SimpProxy_1 simpProxy_1 = (SimpProxy_1) simpFactory.getProxy("SimpProxy_1");
            simpProxy_1.setBefore("设置前置方法");
            System.out.println("simpProxy_1 = " + simpProxy_1);
            SimpProxy_2 simpProxy_2 = (SimpProxy_2) simpFactory.getProxy("SimpProxy_2");
            simpProxy_2.setAfter("设后前置方法");
            System.out.println("simpProxy_2 = " + simpProxy_2);
    
        }
    }
    
    
    结果

    相关文章

      网友评论

          本文标题:10.简单工厂模式

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