1. 描述
组合模式又叫部分-整体模式,组合模式将对象组合成树的结构,并且让客户端对单个对象和组合对象的使用能够保持一致,组合模式使用的场景不是非常多,但如果一旦数据满足树形结构,那么使用组合模式将会发挥巨大的作用。
2. 标准结构
“根节点”
public abstract class Company {
public abstract void method();
}
“子节点”
public class Dept extends Company {
private List<Company> companyList = new ArrayList();
public void method() {
}
public void addCompany(Company company) {
}
public void removeCompany(Company company) {
}
public Company getChild(int index) {
return companyList.get(index);
}
}
“叶子节点”
public class Employee extends Company {
public void method() {
}
}
3. 使用场景
组合模式使用的场景不是非常多,但如果一旦数据满足树形结构,那么使用组合模式将会发挥巨大的作用。
4. 使用案例
Spring
spring为了方便管理缓存,提供了CacheManager接口,主要就提供了两个方法,一个根据缓存名称查找缓存对象,一个获取所管理的所有缓存名称。
/**
* Spring's central cache manager SPI.
* Allows for retrieving named {@link Cache} regions.
*
* @author Costin Leau
* @since 3.1
*/
public interface CacheManager {
/**
* Return the cache associated with the given name.
* @param name the cache identifier (must not be {@code null})
* @return the associated cache, or {@code null} if none found
*/
@Nullable
Cache getCache(String name);
/**
* Return a collection of the cache names known by this manager.
* @return the names of all caches known by the cache manager
*/
Collection<String> getCacheNames();
}
那么,针对这两个接口要如何实现呢?spring定义了CompositeCacheManager来统一处理,把CacheManager当做“子树”,把Cache当做“叶子”,内部维护了一个List集合,向集合中添加元素时,即可以是CompositeCacheManager本身(spring考虑了可能根据不同的业务,需要独立、隔离CacheManager的需求),也可以是其他的CacheManager实现类(一个CacheManager可以有不同的实现),比如:NoOpCacheManager、CaffeineCacheManager等。
/**
* Composite {@link CacheManager} implementation that iterates over
* a given collection of delegate {@link CacheManager} instances.
*
* <p>Allows {@link NoOpCacheManager} to be automatically added to the end of
* the list for handling cache declarations without a backing store. Otherwise,
* any custom {@link CacheManager} may play that role of the last delegate as
* well, lazily creating cache regions for any requested name.
*
* <p>Note: Regular CacheManagers that this composite manager delegates to need
* to return {@code null} from {@link #getCache(String)} if they are unaware of
* the specified cache name, allowing for iteration to the next delegate in line.
* However, most {@link CacheManager} implementations fall back to lazy creation
* of named caches once requested; check out the specific configuration details
* for a 'static' mode with fixed cache names, if available.
*
* @author Costin Leau
* @author Juergen Hoeller
* @since 3.1
* @see #setFallbackToNoOpCache
* @see org.springframework.cache.concurrent.ConcurrentMapCacheManager#setCacheNames
*/
public class CompositeCacheManager implements CacheManager, InitializingBean {
private final List<CacheManager> cacheManagers = new ArrayList<>();
private boolean fallbackToNoOpCache = false;
/**
* Construct an empty CompositeCacheManager, with delegate CacheManagers to
* be added via the {@link #setCacheManagers "cacheManagers"} property.
*/
public CompositeCacheManager() {
}
/**
* Construct a CompositeCacheManager from the given delegate CacheManagers.
* @param cacheManagers the CacheManagers to delegate to
*/
public CompositeCacheManager(CacheManager... cacheManagers) {
setCacheManagers(Arrays.asList(cacheManagers));
}
/**
* Specify the CacheManagers to delegate to.
*/
public void setCacheManagers(Collection<CacheManager> cacheManagers) {
this.cacheManagers.addAll(cacheManagers);
}
/**
* Indicate whether a {@link NoOpCacheManager} should be added at the end of the delegate list.
* In this case, any {@code getCache} requests not handled by the configured CacheManagers will
* be automatically handled by the {@link NoOpCacheManager} (and hence never return {@code null}).
*/
public void setFallbackToNoOpCache(boolean fallbackToNoOpCache) {
this.fallbackToNoOpCache = fallbackToNoOpCache;
}
@Override
public void afterPropertiesSet() {
if (this.fallbackToNoOpCache) {
this.cacheManagers.add(new NoOpCacheManager());
}
}
@Override
@Nullable
public Cache getCache(String name) {
for (CacheManager cacheManager : this.cacheManagers) {
Cache cache = cacheManager.getCache(name);
if (cache != null) {
return cache;
}
}
return null;
}
@Override
public Collection<String> getCacheNames() {
Set<String> names = new LinkedHashSet<>();
for (CacheManager manager : this.cacheManagers) {
names.addAll(manager.getCacheNames());
}
return Collections.unmodifiableSet(names);
}
}
可以看到getCache,getCacheNames都是通过递归完成