享元模式(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();
}
}