前言
主要是参考了这篇文章链接,增加了一个builder构造器形式的构建参数以及对应的策略模式
1.先来讲讲策略模式
这是策略模式的结构图,主要是定义一个任务,往往可以有多种不同的方式,每一种方式称为一个策略,我们可以根据环境或者条件的不同选择不同的策略来完成该项任务。
就拿这个图片加载来讲,加载图片是一个任务,你可以使用Glide,Fresco,Imageloader等策略来加载,一般而言任务都是抽象的,我使用接口来定义
public interface BaseImageLoaderStrategy<T extends ImageConfig> {
void loadImage(Context ctx, T config);
}
而且还对数据进行了封装,数据必须继承ImageConfig
public class ImageConfig {
protected String url;
protected int placeholder;
protected int errorPic;
public String getUrl() {
return url;
}
public int getPlaceholder() {
return placeholder;
}
public int getErrorPic() {
return errorPic;
}
}
里面要有url,用于请求资源的,然后用一个统一的类ImageLoader来管理这个
public class ImageLoader {
private static final ImageLoader INSTANCE = new ImageLoader();
private ImageLoader(){
//默认使用Glide
mStrategy=new GlideImageLoaderStrategy();
}
public static ImageLoader getInstance(){
return INSTANCE;
}
private BaseImageLoaderStrategy mStrategy;
public ImageLoader(BaseImageLoaderStrategy strategy) {
setLoadImgStrategy(strategy);
}
public <T extends ImageConfig> void loadImage(Context context, T config) {
this.mStrategy.loadImage(context, config);
}
public void setLoadImgStrategy(BaseImageLoaderStrategy strategy) {
this.mStrategy = strategy;
}
}
策略模式必须要指定使用哪种类型的策略,为了性能考虑,我把ImageLoader设置为单例
2.Glide的封装
public class GlideImageConfig extends ImageConfig {
private ImageView iamgeView;
private GlideImageConfig(Builder builder) {
url = builder.url;
placeholder = builder.placeholder;
errorPic = builder.errorPic;
iamgeView = builder.iamgeView;
}
public static Builder builder(){
return new Builder();
}
public ImageView getIamgeView() {
return iamgeView;
}
public static final class Builder {
private String url;
private ImageView imageView;
private int placeholder;
private int errorPic;
private ImageView iamgeView;
public Builder() {
}
public Builder url(String val) {
url = val;
return this;
}
public Builder placeholder(int val) {
placeholder = val;
return this;
}
public Builder errorPic(int val) {
errorPic = val;
return this;
}
public Builder iamgeView(ImageView val) {
iamgeView = val;
return this;
}
public GlideImageConfig build() {
return new GlideImageConfig(this);
}
}
}
数据使用Builder来封装,以后可以很容易扩展
public class GlideImageLoaderStrategy implements BaseImageLoaderStrategy<GlideImageConfig> {
@Override
public void loadImage(Context ctx, GlideImageConfig config) {
if (ctx == null) throw new IllegalStateException("Context is required");
if (config == null) throw new IllegalStateException("GlideImageConfig is required");
if (TextUtils.isEmpty(config.getUrl())) throw new IllegalStateException("url is required");
if (config.getIamgeView() == null) throw new IllegalStateException("imageview is required");
Glide.with(ctx).load(config.getUrl()).into(config.getIamgeView());
}
}
只需要继承BaseImageLoaderStrategy即可,以后也可以自己增加方法,参数都可以
3.对于Fresco的封装点不同,由于Fresco使用自建的SimpleDraweeView,它的数据封装有点不同
public class FrescoImageConfig extends ImageConfig {
private SimpleDraweeView imageView;
private FrescoImageConfig(Builder builder){
this.imageView = builder.imageView;
this.url = builder.url;
this.placeholder = builder.placeholder;
this.errorPic = builder.errorPic;
}
public static Builder builder(){
return new Builder();
}
public SimpleDraweeView getImageView() {
return imageView;
}
public static final class Builder{
private String url;
private SimpleDraweeView imageView;
private int placeholder;
private int errorPic;
private Builder(){
}
public Builder url(String url) {
this.url = url;
return this;
}
public Builder placeholder(int placeholder) {
this.placeholder = placeholder;
return this;
}
public Builder errorPic(int errorPic) {
this.errorPic = errorPic;
return this;
}
public Builder SimpleDraweeView(SimpleDraweeView imageView) {
this.imageView = imageView;
return this;
}
public FrescoImageConfig build(){
return new FrescoImageConfig(this);
}
}
}
而相应的策略类都是大同小异了
public class FrescoImageLoaderStrategy implements BaseImageLoaderStrategy<FrescoImageConfig> {
@Override
public void loadImage(Context ctx, FrescoImageConfig config) {
config.getImageView().setImageURI(Uri.parse(config.getUrl()));
}
}
详细代码请看demo 希望给个star