构建者模式
来源:互联网 发布:淘宝密码修改 编辑:程序博客网 时间:2024/06/08 19:49
在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的参数填好就行了,比前者更简洁
0 0
- 构建者模式
- 构建者模式(Builder)
- 构建者模式
- 构建者模式
- 构建者模式
- building--构建者模式
- 《设计模式》-- 构建者模式
- [设计模式] 构建者模式
- 设计模式-构建者模式
- 设计模式--构建者模式
- c++实现构建者模式
- Java构建者模式(Builder)
- 构建者模式(Builder)
- 简单的构建者模式
- Android 构建者模式(Builder)
- JAVA之构建者模式
- 构建者(Builder)模式
- java设计模式---构建者模式
- Runtime运行时机制
- 二叉树java版
- 最小二乘法拟合圆
- Swift - 页面传值
- BZOJ 1833
- 构建者模式
- linux 下链接mysql练习
- 解析校内网邮箱
- 显式锁Lock、ReentrantLock
- 带标题可拖动的SeekBar
- Filezilla--FZ怎么强制出现隐藏文件
- BZOJ3223文艺平衡树
- 118. Pascal's Triangle
- 文章标题