b2科目四模拟试题多少题驾考考爆了怎么补救
b2科目四模拟试题多少题 驾考考爆了怎么补救

解释器设计模式_lua解释器交互模式_解释器模式c语言(4)

电脑杂谈  发布时间:2017-02-22 18:11:38  来源:网络整理

2 根据不同的配置我们生产不同的电脑,通过不同产品的工厂来生产不同的类型产品

public intece ComputerFactory {
    Cpu createCpu(int pins);
    Mainboard createMainBoard(int cpuHoles);
}
public class InterFactory implements ComputerFactory {

    @Override
    public Cpu createCpu(int pins) {
        return new IntelCpu(pins);
    }

    @Override
    public Mainboard createMainBoard(int cpuHoles) {
        return new IntelMainboard(cpuHoles);
    }

}
public class AmdFactory implements ComputerFactory {
    @Override
    public Cpu createCpu(int pins) {
        return new AmdCpu(pins);
    }

    @Override
    public Mainboard createMainBoard(int cpuHoles) {
        return new AmdMainboard(cpuHoles);
    }
}

这是一个大家经常会遇到的设计模式。Effective Java第二条讲的就是Builder模式的使用。

经典的Builder模式需要有以下部分:

Product 产品抽象类。

Builder 抽象的Builder类。

ConcretBuilder 具体的Builder类。

Director 同一组装过程。

下面我们看看谷歌大神在Guava里面是怎样使用Builder模式的(选择部分代码)

这里的内部类public static class Builder<K, V>即具体的Builder类,ImmutableMap<K, V>即产品类,通过ImmutableMap的builder方法产生Builder,又通过Builder的build(类似Director的组装方法)方法构造ImmutableMap

public abstract class ImmutableMap<K, V> implements Map<K, V>, Serializable {
    public static <K, V> ImmutableMap<K, V> of() {
        return ImmutableBiMap.of();
    }
    public static <K, V> ImmutableMap<K, V> of(K k1, V v1) {
        return ImmutableBiMap.of(k1, v1);
    }
    public static <K, V> Builder<K, V> builder() {
        return new Builder<K, V>();
    }
    public static class Builder<K, V> {

        Comparator<? super V> valueComparator;
        ImmutableMapEntry<K, V>[] entries;
        int size;
        boolean entriesUsed;
        public Builder() {
            this(ImmutableCollection.Builder.DEFAULT_INITIAL_CAPACITY);
        }

        @SuppressWarnings("unchecked")
        Builder(int initialCapacity) {
            this.entries = new ImmutableMapEntry[initialCapacity];
            this.size = 0;
            this.entriesUsed = false;
        }

        public Builder<K, V> put(K key, V value) {
            ensureCapacity(size + 1);
            ImmutableMapEntry<K, V> entry = entryOf(key, value);
            // don't inline this: we want to fail atomically if key or value is null
            entries[size++] = entry;
            return this;
        }

        public Builder<K, V> put(Entry<? extends K, ? extends V> entry) {
            return put(entry.getKey(), entry.getValue());
        }

        /**
         * Associates all of the given map's keys and values in the built map.
         * Duplicate keys are not allowed, and will cause {@link #build} to fail.
         *
         * @throws NullPointerException if any key or value in {@code map} is null
         */
        public Builder<K, V> putAll(Map<? extends K, ? extends V> map) {
            return putAll(map.entrySet());
        }

        public ImmutableMap<K, V> build() {
            switch (size) {
                case 0:
                    return of();
                case 1:
                    return of(entries[0].getKey(), entries[0].getValue());
                default:
          /*
           * If entries is full, then this implementation may end up using the entries array
           * directly and writing over the entry objects with non-terminal entries, but this is
           * safe; if this Builder is used further, it will grow the entries array (so it can't
           * affect the original array), and future build() calls will always copy any entry
           * objects that cannot be safely reused.
           */
                    if (valueComparator != null) {
                        if (entriesUsed) {
                            entries = ObjectArrays.arraysCopyOf(entries, size);
                        }
                        Arrays.sort(
                                entries,
                                0,
                                size,
                                Ordering.from(valueComparator).onResultOf(Maps.<V>valueFunction()));
                    }
                    entriesUsed = size == entries.length;
                    return RegularImmutableMap.fromEntryArray(size, entries);
            }
        }
    }
}


本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-33899-4.html

相关阅读
    发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

    热点图片
    拼命载入中...