定义
《大话设计模式》|策略模式
策略模式(Strategy):定义了算法家族,分别封装起来,让它们之间可以互相替换,此模式让算法的变化,不会影响到使用算法的客户
菜鸟教程|设计模式
在策略模式(Strategy Pattern)中,一个类的行为或其算法可以在运行时更改。这种类型的设计模式属于行为型模式。
在策略模式中,我们创建表示各种策略的对象和一个行为随着策略对象改变而改变的 context 对象。策略对象改变 context 对象的执行算法。
Glide|DiskCacheStrategy包路径:com.bumptech.glide.load.engine.DiskCacheStrategy
/** Set of available caching strategies for media. */
public abstract class DiskCacheStrategy {
/**
* Returns true if this request should cache the original unmodified data.
*
* @param dataSource Indicates where the data was originally retrieved.
*/
如果此请求应该缓存原始未修改的数据,则返回true。
参数:
dataSource -指示最初检索数据的位置。
public abstract boolean isDataCacheable(DataSource dataSource);
/**
* Returns true if this request should cache the final transformed resource.
*
* @param isFromAlternateCacheKey {@code true} if the resource we've decoded was loaded using an
* alternative, rather than the primary, cache key.
* @param dataSource Indicates where the data used to decode the resource was originally
* retrieved.
* @param encodeStrategy The {@link EncodeStrategy} the {@link
* com.bumptech.glide.load.ResourceEncoder} will use to encode the resource.
*/
如果此请求缓存最终转换的资源,则返回true。
参数:
isFromAlternateCacheKey -如果我们已经解码的资源是使用替代的缓存键加载的,而不是主缓存键,则为true。
dataSource -指示最初检索用于解码资源的数据的位置。
encodeStrategy - ResourceEncoder将使用encodeStrategy对资源进行编码。
public abstract boolean isResourceCacheable(
boolean isFromAlternateCacheKey, DataSource dataSource, EncodeStrategy encodeStrategy);
/** Returns true if this request should attempt to decode cached resource data. */
如果此请求应该尝试解码缓存的资源数据,则返回true。
public abstract boolean decodeCachedResource();
/** Returns true if this request should attempt to decode cached source data. */
如果此请求应该尝试解码缓存的源数据,则返回true。
public abstract boolean decodeCachedData();
}
一组可用的媒体缓存策略。
共有五种策略实现- ALL
- NONE
- DATA
- RESOURCE
- AUTOMATIC
使用data和RESOURCE缓存远程数据,仅使用RESOURCE缓存本地数据。
/**
* Caches remote data with both {@link #DATA} and {@link #RESOURCE}, and local data with {@link
* #RESOURCE} only.
*/
public static final DiskCacheStrategy ALL =
new DiskCacheStrategy() {
@Override
public boolean isDataCacheable(DataSource dataSource) {
return dataSource == DataSource.REMOTE;
}
@Override
public boolean isResourceCacheable(
boolean isFromAlternateCacheKey, DataSource dataSource, EncodeStrategy encodeStrategy) {
return dataSource != DataSource.RESOURCE_DISK_CACHE
&& dataSource != DataSource.MEMORY_CACHE;
}
@Override
public boolean decodeCachedResource() {
return true;
}
@Override
public boolean decodeCachedData() {
return true;
}
};
DiskCacheStrategy.NONE
不保存数据到缓存。
/** Saves no data to cache. */
public static final DiskCacheStrategy NONE =
new DiskCacheStrategy() {
@Override
public boolean isDataCacheable(DataSource dataSource) {
return false;
}
@Override
public boolean isResourceCacheable(
boolean isFromAlternateCacheKey, DataSource dataSource, EncodeStrategy encodeStrategy) {
return false;
}
@Override
public boolean decodeCachedResource() {
return false;
}
@Override
public boolean decodeCachedData() {
return false;
}
};
DiskCacheStrategy.DATA
在解码之前,将检索到的数据直接写入磁盘缓存。
/** Writes retrieved data directly to the disk cache before it's decoded. */
public static final DiskCacheStrategy DATA =
new DiskCacheStrategy() {
@Override
public boolean isDataCacheable(DataSource dataSource) {
return dataSource != DataSource.DATA_DISK_CACHE && dataSource != DataSource.MEMORY_CACHE;
}
@Override
public boolean isResourceCacheable(
boolean isFromAlternateCacheKey, DataSource dataSource, EncodeStrategy encodeStrategy) {
return false;
}
@Override
public boolean decodeCachedResource() {
return false;
}
@Override
public boolean decodeCachedData() {
return true;
}
};
DiskCacheStrategy.RESOURCE
将解码后的资源写入磁盘。
/** Writes resources to disk after they've been decoded. */
public static final DiskCacheStrategy RESOURCE =
new DiskCacheStrategy() {
@Override
public boolean isDataCacheable(DataSource dataSource) {
return false;
}
@Override
public boolean isResourceCacheable(
boolean isFromAlternateCacheKey, DataSource dataSource, EncodeStrategy encodeStrategy) {
return dataSource != DataSource.RESOURCE_DISK_CACHE
&& dataSource != DataSource.MEMORY_CACHE;
}
@Override
public boolean decodeCachedResource() {
return true;
}
@Override
public boolean decodeCachedData() {
return false;
}
};
DiskCacheStrategy.AUTOMATIC
根据DataFetcher的数据源和ResourceEncoder的EncodeStrategy(如果ResourceEncoder可用),尝试智能地选择策略。
/**
* Tries to intelligently choose a strategy based on the data source of the {@link
* com.bumptech.glide.load.data.DataFetcher} and the {@link
* com.bumptech.glide.load.EncodeStrategy} of the {@link com.bumptech.glide.load.ResourceEncoder}
* (if an {@link com.bumptech.glide.load.ResourceEncoder} is available).
*/
public static final DiskCacheStrategy AUTOMATIC =
new DiskCacheStrategy() {
@Override
public boolean isDataCacheable(DataSource dataSource) {
return dataSource == DataSource.REMOTE;
}
@Override
public boolean isResourceCacheable(
boolean isFromAlternateCacheKey, DataSource dataSource, EncodeStrategy encodeStrategy) {
return ((isFromAlternateCacheKey && dataSource == DataSource.DATA_DISK_CACHE)
|| dataSource == DataSource.LOCAL)
&& encodeStrategy == EncodeStrategy.TRANSFORMED;
}
@Override
public boolean decodeCachedResource() {
return true;
}
@Override
public boolean decodeCachedData() {
return true;
}
};
应用场景列表如下:
- 商场银行收银时促销,打折
- 诸葛亮的锦囊妙计,每一个锦囊就是一个策略
- 旅行的出游方式,选择骑自行车、坐汽车,每一种旅行方式都是一个策略
- JAVA AWT 中的 LayoutManager
布局策略如下:
- LinearLayout
- RelativeLayout
- FrameLayout
- DrawerLayout
- ConstraintLayout
- GridLayout
- 。。。 这些继承ViewGroup的xxxLayout布局方式就是一种布局实现策略。策略模式在Android系统源码里应用的也很频繁。
策略模式用于迭代算法实现。作者也经常这么使用。使用场景很典型,实现上也比较容易 建议开发者把具体的复杂算法实现抽取出来进行策略模式实现。这样在进行算法改进的时候就很方便替换了