您的位置:首页 > 其它

构建者模式

2016-03-14 21:16 253 查看
在Android 开发中对于Build一点也不模式,Android 中的Dialog ,Notification以及最常见的第三方库Universal-Image-Loader就是采用Build 模式,对于Build模式有什么好处

就以常见的ImageLoad为例,一下是简单的模拟ImageLoad

public class ImageLoad {
//加载图片错误参数
int mLoadImageFailed;
//加载图片过程中的参数
int mLoadImage;
//线程池
int mPoorNum = Runtime.getRuntime().availableProcessors();
ExecutorService mExcutorService = Executors.newFixedThreadPool(mPoorNum);

public ImageLoad(int mLoadImage, int mPoorNum, int mLoadImageFailed) {
this.mLoadImage = mLoadImage;
this.mPoorNum = mPoorNum;
this.mLoadImageFailed = mLoadImageFailed;
}

public void showImageView(final ImageView iv, final String url) {
iv.setImageResource(mLoadImage);
iv.setTag(url);
mExcutorService.submit(new Runnable() {
@Override
public void run() {
Bitmap download = download(url);
if(download==null){
//加载失败的图片
iv.setImageResource(mLoadImageFailed);
}else{
iv.setImageBitmap(download);
}
}
});
}
private Bitmap download(String url){
Bitmap bitmap=null;
//下载过程省略
return bitmap;
}

public void setmLoadImageFailed(int mLoadImageFailed){
this.mLoadImageFailed=mLoadImageFailed;
}
public void setmPoorNum(int num){
this.mPoorNum=num;
}
public void setmLoadImage(int mLoadImage){
this.mLoadImage=mLoadImage;
}
}


public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView iv = (ImageView) findViewById(R.id.iv);
ImageLoad imageLoad = new ImageLoad(R.mipmap.ic_launcher, 3, R.mipmap.ic_launcher);
imageLoad.showImageView(iv,"xxx");
}
}


以上是较为简单的ImageLoad 内部只有三个参数一个是较为简单的Demo以上没有实现单例。

从这可以看出对于ImageLoad 这个对象 我初始化传了三个参数 或者你可以是

imageLoad.setmLoadImage(R.mipmap.ic_launcher);
imageLoad.setmLoadImageFailed(R.mipmap.ic_launcher);
imageLoad.setmPoorNum(3);
imageLoad.showImageView(iv,"xxx");


如果ImageLoad 内部参数过多或者是不的setxxx ,而且初始化了一个指定的线程数量的线程池,如果你在setPoorNum 该怎么办,唯一的好处就是 你可以根据不同的场景选择自己的想要的加载图片,加下了看下试用构建着模式的ImageLoad

class ImageLoadConfig {
//加载图片错误参数
private int mLoadImageFailed=R.mipmap.ic_launcher;

//加载图片过程中的参数
private int mLoadImage=R.mipmap.ic_launcher;
//线程池
private int mPoorNum = Runtime.getRuntime().availableProcessors();

private ImageLoadConfig(){

}
class Builder {
//加载图片错误参数
private int mLoadImageFailed;

//加载图片过程中的参数
private int mLoadImage;
//线程池
private int mPoorNum = Runtime.getRuntime().availableProcessors();

public Builder setmLoadImageFailed(int mLoadImageFailed) {
this.mLoadImageFailed = mLoadImageFailed;
return this;
}
public Builder setmLoadImage(int mLoadImage) {
this.mLoadImage = mLoadImage;
return this;
}
public Builder setmPoorNum(int mPoorNum) {
this.mPoorNum = mPoorNum;
return this;
}
public ImageLoadConfig Build(){
ImageLoadConfig config=new ImageLoadConfig();
applyConfig(config);
return config;
}
public void applyConfig(ImageLoadConfig config){
config.mLoadImage=this.mLoadImage;
config.mLoadImageFailed=this.mLoadImageFailed;
config.mPoorNum=this.mPoorNum;
}
}
public int getmLoadImageFailed() {
return mLoadImageFailed;
}

public int getmLoadImage() {
return mLoadImage;
}

public int getmPoorNum() {
return mPoorNum;
}

}


public class MyImageLoad {
ImageLoadConfig config;
ExecutorService mExcutorService =null;
public void init(ImageLoadConfig config) {
if (config==null){
throw  new IllegalStateException("xxxx");
}
this.config = config;
mExcutorService=Executors.newFixedThreadPool(config.getmPoorNum());
}
public void showBitmap(){

}

public void showImageView(final ImageView iv, final String url) {
iv.setImageResource(config.getmLoadImage());
iv.setTag(url);
mExcutorService.submit(new Runnable() {
@Override
public void run() {
Bitmap download = download(url);
if(download==null){
//加载失败的图片
iv.setImageResource(config.getmLoadImageFailed());
}else{
iv.setImageBitmap(download);
}
}
});
}
private Bitmap download(String url){
Bitmap bitmap=null;
//下载过程省略
return bitmap;
}
}


ImageLoadConfig config = new ImageLoadConfig.Builder().
setmLoadImageFailed(R.mipmap.ic_launcher).setmLoadImage(R.mipmap.ic_launcher).setmPoorNum(3).Build();
MyImageLoad load = new MyImageLoad();
load.init(config);
load.showImageView(iv, "xxxx");


由于本作者懒 如果用单例设计可能会显得比较好和前者对比,使用Build模式的好吃在于初始化配置是,他的属性不能再修改(没有set的方法)导致使用者不必知道内部的细节只要把Build的参数填好就行了,比前者更简洁
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: