设计模式学习心得<享元模式 Flyweight>

享元模式(Flyweight Pattern)主要用于减小建立对象的数量,以减小内存占用和提升性能。这种类型的设计模式属于结构型模式,它提供了减小对象数量从而改善应用所需的对象结构的方式。java

享元模式尝试重用现有的同类对象,若是未找到匹配的对象,则建立新对象。git

概述


  • 意图
    运用共享技术有效地支持大量细粒度的对象。github

  • 主要解决
    在有大量对象时,有可能会形成内存溢出,咱们把其中共同的部分抽象出来,若是有相同的业务请求,直接返回在内存中已有的对象,避免从新建立。数据库

  • 什么时候使用
  1. 系统中有大量对象。
  2. 这些对象消耗大量内存。
  3. 这些对象的状态大部分能够外部化。
  4. 这些对象能够按照内蕴状态分为不少组,当把外蕴对象从对象中剔除出来时,每一组对象均可以用一个对象来代替。
  5. 系统不依赖于这些对象身份,这些对象是不可分辨的。
  • 如何解决
    用惟一标识码判断,若是在内存中有,则返回这个惟一标识码所标识的对象。设计模式

  • 关键代码
    用 HashMap 存储这些对象。缓存

  • 应用实例
  1. JAVA 中的 String,若是有则返回,若是没有则建立一个字符串保存在字符串缓存池里面。
  2. 数据库的数据池。
  3. hibernate-validate自定义了基于弱引用的缓存容器ConcurrentReferenceHashMap
  • 优势
    大大减小对象的建立,下降系统的内存,使效率提升。安全

  • 缺点
    提升了系统的复杂度,须要分离出外部状态和内部状态,并且外部状态具备固有化的性质,不该该随着内部状态的变化而变化,不然会形成系统的混乱。ide

  • 使用场景
  1. 系统有大量类似对象。
  2. 须要缓冲池的场景。
  • 注意事项
  1. 注意划分外部状态和内部状态,不然可能会引发线程安全问题。
  2. 这些类必须有一个工厂对象加以控制。

实现


摘抄hibernate-validate源码性能

容器:ConcurrentReferenceHashMapui

调用摘抄

/**
 * This manager is in charge of providing all constraint related meta data
 * required by the validation engine.
 * <p>
 * Actual retrieval of meta data is delegated to {@link MetaDataProvider}
 * implementations which load meta-data based e.g. based on annotations or XML.
 * </p>
 * <p>
 * For performance reasons a cache is used which stores all meta data once
 * loaded for repeated retrieval. Upon initialization this cache is populated
 * with meta data provided by the given <i>eager</i> providers. If the cache
 * doesn't contain the meta data for a requested type it will be retrieved on
 * demand using the annotation based provider.
 * </p>
 *
 * @author Gunnar Morling
 * @author Chris Beckey &lt;cbeckey@paypal.com&gt;
 * @author Guillaume Smet
*/
public class BeanMetaDataManager {
    /**
     * The default initial capacity for this cache.
     */
    private static final int DEFAULT_INITIAL_CAPACITY = 16;

    /**
     * The default load factor for this cache.
     */
    private static final float DEFAULT_LOAD_FACTOR = 0.75f;

    /**
     * The default concurrency level for this cache.
     */
    private static final int DEFAULT_CONCURRENCY_LEVEL = 16;

    /**
     * Additional metadata providers used for meta data retrieval if
     * the XML and/or programmatic configuration is used.
     */
    @Immutable
    private final List<MetaDataProvider> metaDataProviders;

    /**
     * Helper for builtin constraints and their validator implementations
     */
    private final ConstraintHelper constraintHelper;

    /**
     * Used for resolving generic type information.
     */
    private final TypeResolutionHelper typeResolutionHelper;

    /**
     * The {@link ValueExtractor} manager.
     */
    private final ValueExtractorManager valueExtractorManager;

    private final ExecutableParameterNameProvider parameterNameProvider;

    /**
     * Used to cache the constraint meta data for validated entities
     */
    private final ConcurrentReferenceHashMap<Class<?>, BeanMetaData<?>> beanMetaDataCache;

    /**
     * Used for resolving type parameters. Thread-safe.
     */
    private final ExecutableHelper executableHelper;

    private final ValidationOrderGenerator validationOrderGenerator;

    /**
     * the three properties in this field affect the invocation of rules associated to section 4.5.5
     * of the specification.  By default they are all false, if true they allow
     * for relaxation of the Liskov Substitution Principal.
     */
    private final MethodValidationConfiguration methodValidationConfiguration;

    public BeanMetaDataManager(ConstraintHelper constraintHelper,
            ExecutableHelper executableHelper,
            TypeResolutionHelper typeResolutionHelper,
            ExecutableParameterNameProvider parameterNameProvider,
            ValueExtractorManager valueExtractorManager,
            ValidationOrderGenerator validationOrderGenerator,
            List<MetaDataProvider> optionalMetaDataProviders,
            MethodValidationConfiguration methodValidationConfiguration) {
        this.constraintHelper = constraintHelper;
        this.executableHelper = executableHelper;
        this.typeResolutionHelper = typeResolutionHelper;
        this.valueExtractorManager = valueExtractorManager;
        this.parameterNameProvider = parameterNameProvider;
        this.validationOrderGenerator = validationOrderGenerator;

        this.methodValidationConfiguration = methodValidationConfiguration;

        this.beanMetaDataCache = new ConcurrentReferenceHashMap<>(
                DEFAULT_INITIAL_CAPACITY,
                DEFAULT_LOAD_FACTOR,
                DEFAULT_CONCURRENCY_LEVEL,
                SOFT,
                SOFT,
                EnumSet.of( IDENTITY_COMPARISONS )
        );

        AnnotationProcessingOptions annotationProcessingOptions = getAnnotationProcessingOptionsFromNonDefaultProviders( optionalMetaDataProviders );
        AnnotationMetaDataProvider defaultProvider = new AnnotationMetaDataProvider(
                constraintHelper,
                typeResolutionHelper,
                valueExtractorManager,
                annotationProcessingOptions
        );
        List<MetaDataProvider> tmpMetaDataProviders = new ArrayList<>( optionalMetaDataProviders.size() + 1 );
        // We add the annotation based metadata provider at the first position so that the entire metadata model is assembled
        // first.
        // The other optional metadata providers will then contribute their additional metadata to the preexisting model.
        // This helps to mitigate issues like HV-1450.
        tmpMetaDataProviders.add( defaultProvider );
        tmpMetaDataProviders.addAll( optionalMetaDataProviders );

        this.metaDataProviders = CollectionHelper.toImmutableList( tmpMetaDataProviders );
    }

    @SuppressWarnings("unchecked")
    public <T> BeanMetaData<T> getBeanMetaData(Class<T> beanClass) {
        Contracts.assertNotNull( beanClass, MESSAGES.beanTypeCannotBeNull() );

        BeanMetaData<T> beanMetaData = (BeanMetaData<T>) beanMetaDataCache.computeIfAbsent( beanClass,
                bc -> createBeanMetaData( bc ) );

        return beanMetaData;
    }

    public void clear() {
        beanMetaDataCache.clear();
    }

    public int numberOfCachedBeanMetaDataInstances() {
        return beanMetaDataCache.size();
    }
}
相关文章
相关标签/搜索