• 设计模式课程 设计模式精讲 7-3 建造者模式源码解析(jdk+guava+spring+mybaties)


    1    源码解析

    1.1  jdk解析

    1.2  guava解析

    1.3  spring解析

    1.4  mybaties解析

    1    源码解析
    1.1  jdk解析

    String

     public StringBuilder append(boolean b) {
            super.append(b);
            return this;
        }
    
        public StringBuilder append(char c) {
            super.append(c);
            return this;
        }
    
        public StringBuilder append(int i) {
            super.append(i);
            return this;
        }
    
        public StringBuilder append(long lng) {
            super.append(lng);
            return this;
        }
    
        public StringBuilder append(float f) {
            super.append(f);
            return this;
        }
    
        public StringBuilder append(double d) {
            super.append(d);
            return this;
        }

    StringBuilder

        public synchronized StringBuffer append(Object obj) {
            super.append(String.valueOf(obj));
            return this;
        }
    
        public synchronized StringBuffer append(String str) {
            super.append(str);
            return this;
        }
    1.2  guava解析(ImmutableSet)

     copyOf方法

     public static <E> ImmutableSet<E> copyOf(Collection<? extends E> elements) {
            if (elements instanceof ImmutableSet && !(elements instanceof ImmutableSortedSet)) {
                ImmutableSet<E> set = (ImmutableSet)elements;
                if (!set.isPartialView()) {
                    return set;
                }
            } else if (elements instanceof EnumSet) {
                return copyOfEnumSet((EnumSet)elements);
            }
    
            Object[] array = elements.toArray();
            return construct(array.length, array);
        }

    Of方法

     public static <E> ImmutableSet<E> of() {
            return RegularImmutableSet.EMPTY;
        }
    
        public static <E> ImmutableSet<E> of(E element) {
            return new SingletonImmutableSet(element);
        }
    
        public static <E> ImmutableSet<E> of(E e1, E e2) {
            return construct(2, e1, e2);
        }
    
        public static <E> ImmutableSet<E> of(E e1, E e2, E e3) {
            return construct(3, e1, e2, e3);
        }
    
        public static <E> ImmutableSet<E> of(E e1, E e2, E e3, E e4) {
            return construct(4, e1, e2, e3, e4);
        }
    
        public static <E> ImmutableSet<E> of(E e1, E e2, E e3, E e4, E e5) {
            return construct(5, e1, e2, e3, e4, e5);
        }

    add方法(注意静态内部类)

       @CanIgnoreReturnValue
            public ImmutableSet.Builder<E> add(E element) {
                super.add(element);
                return this;
            }
    
    public static class Builder<E> extends ArrayBasedBuilder<E> {
            public Builder() {
                this(4);
            }
    
            Builder(int capacity) {
                super(capacity);
            }
    
            @CanIgnoreReturnValue
            public ImmutableSet.Builder<E> add(E element) {
                super.add(element);
                return this;
            }
    
            @CanIgnoreReturnValue
            public ImmutableSet.Builder<E> add(E... elements) {
                super.add(elements);
                return this;
            }
    
            @CanIgnoreReturnValue
            public ImmutableSet.Builder<E> addAll(Iterable<? extends E> elements) {
                super.addAll(elements);
                return this;
            }
    
            @CanIgnoreReturnValue
            public ImmutableSet.Builder<E> addAll(Iterator<? extends E> elements) {
                super.addAll(elements);
                return this;
            }
    
            public ImmutableSet<E> build() {
                ImmutableSet<E> result = ImmutableSet.construct(this.size, this.contents);
                this.size = result.size();
                return result;
            }
        
    1.3  spring解析

    BeanDefinitionBuilder类

       public static BeanDefinitionBuilder genericBeanDefinition() {
            BeanDefinitionBuilder builder = new BeanDefinitionBuilder();
            builder.beanDefinition = new GenericBeanDefinition();
            return builder;
        }
    1.4  mybaties解析

    SqlSessionFactoryBuilder类(建造者模式中再使用建造者)

    解析xml过程

    public SqlSessionFactory build(Reader reader, String environment, Properties properties) {
            SqlSessionFactory var5;
            try {
                XMLConfigBuilder parser = new XMLConfigBuilder(reader, environment, properties);
                var5 = this.build(parser.parse());
            } catch (Exception var14) {
                throw ExceptionFactory.wrapException("Error building SqlSession.", var14);
            } finally {
                ErrorContext.instance().reset();
    
                try {
                    reader.close();
                } catch (IOException var13) {
                    ;
                }
    
            }
    
            return var5;
        }

    XMLConfigBuilder类

      public Configuration parse() {
        if (parsed) {
          throw new BuilderException("Each XMLConfigBuilder can only be used once.");
        }
        parsed = true;
        parseConfiguration(parser.evalNode("/configuration"));
        return configuration;
      }
    
      private void parseConfiguration(XNode root) {
        try {
          Properties settings = settingsAsPropertiess(root.evalNode("settings"));
          //issue #117 read properties first
          propertiesElement(root.evalNode("properties"));
          loadCustomVfs(settings);
          typeAliasesElement(root.evalNode("typeAliases"));
          pluginElement(root.evalNode("plugins"));
          objectFactoryElement(root.evalNode("objectFactory"));
          objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));
          reflectorFactoryElement(root.evalNode("reflectorFactory"));
          settingsElement(settings);
          // read it after objectFactory and objectWrapperFactory issue #631
          environmentsElement(root.evalNode("environments"));
          databaseIdProviderElement(root.evalNode("databaseIdProvider"));
          typeHandlerElement(root.evalNode("typeHandlers"));
          mapperElement(root.evalNode("mappers"));
        } catch (Exception e) {
          throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e);
        }
      }
  • 相关阅读:
    Controller之daemonset
    Ubuntu下Zmap的安装
    VSCode无法加载PlatformIO按钮可能的原因(踩坑笔记)
    由于更换域名或者IP变更导致WordPressg无法进入后台的问题解决办法
    使用VSCode进行Arduino与ESP32开发配置指南
    Win7下阿米洛机械键盘蓝牙配置
    IIC通讯协议与SPI通讯协议小结
    如何在树莓派上搭建个人博客系统(踩坑笔记)
    STorM32 BGC三轴云台控制板电机驱动电路设计(驱动芯片DRV8313)
    #数据结构#什么是栈及栈的作用
  • 原文地址:https://www.cnblogs.com/1446358788-qq/p/11347881.html
Copyright © 2020-2023  润新知