設計模式學習心得<享元模式 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();
    }
}
相關文章
相關標籤/搜索