設計模式學習心得<享元模式 Flyweight>
享元模式(Flyweight Pattern)主要用于減少創建對象的數量,以減少內存占用和提高性能。這種類型的設計模式屬于結構型模式,它提供了減少對象數量從而改善應用所需的對象結構的方式。
享元模式嘗試重用現有的同類對象,如果未找到匹配的對象,則創建新對象。
概述
-
意圖
運用共享技術有效地支持大量細粒度的對象。 -
主要解決
在有大量對象時,有可能會造成內存溢出,我們把其中共同的部分抽象出來,如果有相同的業務請求,直接返回在內存中已有的對象,避免重新創建。 -
何時使用
- 系統中有大量對象。
- 這些對象消耗大量內存。
- 這些對象的狀態大部分可以外部化。
- 這些對象可以按照內蘊狀態分為很多組,當把外蘊對象從對象中剔除出來時,每一組對象都可以用一個對象來代替。
- 系統不依賴于這些對象身份,這些對象是不可分辨的。
-
如何解決
用唯一標識碼判斷,如果在內存中有,則返回這個唯一標識碼所標識的對象。 -
關鍵代碼
用 HashMap 存儲這些對象。 -
應用實例
- JAVA 中的 String,如果有則返回,如果沒有則創建一個字符串保存在字符串緩存池里面。
- 數據庫的數據池。
- hibernate-validate自定義了基于弱引用的緩存容器ConcurrentReferenceHashMap
-
優點
大大減少對象的創建,降低系統的內存,使效率提高。 -
缺點
提高了系統的復雜度,需要分離出外部狀態和內部狀態,而且外部狀態具有固有化的性質,不應該隨著內部狀態的變化而變化,否則會造成系統的混亂。 -
使用場景
- 系統有大量相似對象。
- 需要緩沖池的場景。
- 注意事項
- 注意劃分外部狀態和內部狀態,否則可能會引起線程安全問題。
- 這些類必須有一個工廠對象加以控制。
實現
摘抄hibernate-validate源碼
調用摘抄
/**
* 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 <cbeckey@paypal.com>
* @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();
}
}
浮生潦草閑愁廣,一聽啤酒一口盡
浙公網安備 33010602011771號