package com.wp.java.builder;

import org.junit.Test;

public class DoDoContactDemo {

    @Test
public void test(){
DoDoContact contact = new DoDoContact.Builder("jack").address("lundun").safeID(5).age(10).build(); }
} class DoDoContact {
private int age;
private int safeID;
private String name;
private String address; public int getAge() {
return age;
}
public int getSafeID() {
return safeID;
}
public String getName() {
return name;
}
public String getAddress() {
return address;
} static class Builder{
private int age=0;
private int safeID =0;
private String name=null;
private String address = null; public Builder(String name){
this.name = name;
} public Builder age(int age){
this.age = age;
return this;
} public Builder safeID(int safeID){
this.safeID = safeID;
return this;
} public Builder address(String address){
this.address = address;
return this;
} public DoDoContact build(){
return new DoDoContact(this);
}
} private DoDoContact(Builder b){
age = b.age;
name = b.name;
safeID = b.safeID;
address = b.address;
} }
package com.wp.java.builder;

import org.junit.Test;
/**
* Builder 模式
* */
public class CardDemo {
@Test
public void test(){
Card con = new ConcreteCard();
Director dir = new Director(con);
dir.construct();
}
} interface Card {
void createPardA();
void createPardB();
void createPardC();
} class ConcreteCard implements Card{ @Override
public void createPardA() { } @Override
public void createPardB() { } @Override
public void createPardC() { } } class Director {
private Card card; public Director(Card card) {
super();
this.card = card;
} public void construct(){
card.createPardA();
card.createPardB();
card.createPardC();
}
}

上面都是简单的测试案例,下面将接触到的框架中原样,Universal-Image-Loader,相信有部分搞android开发的哥们是有点眼熟的,看代码吧

package com.nostra13.universalimageloader.core;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory.Options;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.BitmapDisplayer;
import com.nostra13.universalimageloader.core.display.SimpleBitmapDisplayer;
import com.nostra13.universalimageloader.core.download.ImageDownloader;
import com.nostra13.universalimageloader.core.process.BitmapProcessor; public final class DisplayImageOptions { private final int imageResOnLoading;
private final int imageResForEmptyUri;
private final int imageResOnFail;
private final Drawable imageOnLoading;
private final Drawable imageForEmptyUri;
private final Drawable imageOnFail;
private final boolean resetViewBeforeLoading;
private final boolean cacheInMemory;
private final boolean cacheOnDisk;
private final ImageScaleType imageScaleType;
private final Options decodingOptions;
private final int delayBeforeLoading;
private final boolean considerExifParams;
private final Object extraForDownloader;
private final BitmapProcessor preProcessor;
private final BitmapProcessor postProcessor;
private final BitmapDisplayer displayer;
private final Handler handler;
private final boolean isSyncLoading; private DisplayImageOptions(Builder builder) {
imageResOnLoading = builder.imageResOnLoading;
imageResForEmptyUri = builder.imageResForEmptyUri;
imageResOnFail = builder.imageResOnFail;
imageOnLoading = builder.imageOnLoading;
imageForEmptyUri = builder.imageForEmptyUri;
imageOnFail = builder.imageOnFail;
resetViewBeforeLoading = builder.resetViewBeforeLoading;
cacheInMemory = builder.cacheInMemory;
cacheOnDisk = builder.cacheOnDisk;
imageScaleType = builder.imageScaleType;
decodingOptions = builder.decodingOptions;
delayBeforeLoading = builder.delayBeforeLoading;
considerExifParams = builder.considerExifParams;
extraForDownloader = builder.extraForDownloader;
preProcessor = builder.preProcessor;
postProcessor = builder.postProcessor;
displayer = builder.displayer;
handler = builder.handler;
isSyncLoading = builder.isSyncLoading;
} public boolean shouldShowImageOnLoading() {
return imageOnLoading != null || imageResOnLoading != 0;
} public boolean shouldShowImageForEmptyUri() {
return imageForEmptyUri != null || imageResForEmptyUri != 0;
} public boolean shouldShowImageOnFail() {
return imageOnFail != null || imageResOnFail != 0;
} public boolean shouldPreProcess() {
return preProcessor != null;
} public boolean shouldPostProcess() {
return postProcessor != null;
} public boolean shouldDelayBeforeLoading() {
return delayBeforeLoading > 0;
} public Drawable getImageOnLoading(Resources res) {
return imageResOnLoading != 0 ? res.getDrawable(imageResOnLoading) : imageOnLoading;
} public Drawable getImageForEmptyUri(Resources res) {
return imageResForEmptyUri != 0 ? res.getDrawable(imageResForEmptyUri) : imageForEmptyUri;
} public Drawable getImageOnFail(Resources res) {
return imageResOnFail != 0 ? res.getDrawable(imageResOnFail) : imageOnFail;
} public boolean isResetViewBeforeLoading() {
return resetViewBeforeLoading;
} public boolean isCacheInMemory() {
return cacheInMemory;
} public boolean isCacheOnDisk() {
return cacheOnDisk;
} public ImageScaleType getImageScaleType() {
return imageScaleType;
} public Options getDecodingOptions() {
return decodingOptions;
} public int getDelayBeforeLoading() {
return delayBeforeLoading;
} public boolean isConsiderExifParams() {
return considerExifParams;
} public Object getExtraForDownloader() {
return extraForDownloader;
} public BitmapProcessor getPreProcessor() {
return preProcessor;
} public BitmapProcessor getPostProcessor() {
return postProcessor;
} public BitmapDisplayer getDisplayer() {
return displayer;
} public Handler getHandler() {
return handler;
} boolean isSyncLoading() {
return isSyncLoading;
} /**
* Builder for {@link DisplayImageOptions}
*/
public static class Builder {
private int imageResOnLoading = 0;
private int imageResForEmptyUri = 0;
private int imageResOnFail = 0;
private Drawable imageOnLoading = null;
private Drawable imageForEmptyUri = null;
private Drawable imageOnFail = null;
private boolean resetViewBeforeLoading = false;
private boolean cacheInMemory = false;
private boolean cacheOnDisk = false;
private ImageScaleType imageScaleType = ImageScaleType.IN_SAMPLE_POWER_OF_2;
private Options decodingOptions = new Options();
private int delayBeforeLoading = 0;
private boolean considerExifParams = false;
private Object extraForDownloader = null;
private BitmapProcessor preProcessor = null;
private BitmapProcessor postProcessor = null;
private BitmapDisplayer displayer = DefaultConfigurationFactory.createBitmapDisplayer();
private Handler handler = null;
private boolean isSyncLoading = false; @Deprecated
public Builder showStubImage(int imageRes) {
imageResOnLoading = imageRes;
return this;
} public Builder showImageOnLoading(int imageRes) {
imageResOnLoading = imageRes;
return this;
} public Builder showImageOnLoading(Drawable drawable) {
imageOnLoading = drawable;
return this;
} public Builder showImageForEmptyUri(int imageRes) {
imageResForEmptyUri = imageRes;
return this;
} public Builder showImageForEmptyUri(Drawable drawable) {
imageForEmptyUri = drawable;
return this;
} public Builder showImageOnFail(int imageRes) {
imageResOnFail = imageRes;
return this;
} public Builder showImageOnFail(Drawable drawable) {
imageOnFail = drawable;
return this;
} public Builder resetViewBeforeLoading() {
resetViewBeforeLoading = true;
return this;
} public Builder resetViewBeforeLoading(boolean resetViewBeforeLoading) {
this.resetViewBeforeLoading = resetViewBeforeLoading;
return this;
} @Deprecated
public Builder cacheInMemory() {
cacheInMemory = true;
return this;
} public Builder cacheInMemory(boolean cacheInMemory) {
this.cacheInMemory = cacheInMemory;
return this;
} @Deprecated
public Builder cacheOnDisc() {
return cacheOnDisk(true);
} @Deprecated
public Builder cacheOnDisc(boolean cacheOnDisk) {
return cacheOnDisk(cacheOnDisk);
} /** Sets whether loaded image will be cached on disk */
public Builder cacheOnDisk(boolean cacheOnDisk) {
this.cacheOnDisk = cacheOnDisk;
return this;
} public Builder imageScaleType(ImageScaleType imageScaleType) {
this.imageScaleType = imageScaleType;
return this;
} public Builder bitmapConfig(Bitmap.Config bitmapConfig) {
if (bitmapConfig == null) throw new IllegalArgumentException("bitmapConfig can't be null");
decodingOptions.inPreferredConfig = bitmapConfig;
return this;
} public Builder decodingOptions(Options decodingOptions) {
if (decodingOptions == null) throw new IllegalArgumentException("decodingOptions can't be null");
this.decodingOptions = decodingOptions;
return this;
} public Builder delayBeforeLoading(int delayInMillis) {
this.delayBeforeLoading = delayInMillis;
return this;
} public Builder extraForDownloader(Object extra) {
this.extraForDownloader = extra;
return this;
} public Builder considerExifParams(boolean considerExifParams) {
this.considerExifParams = considerExifParams;
return this;
} public Builder preProcessor(BitmapProcessor preProcessor) {
this.preProcessor = preProcessor;
return this;
} public Builder postProcessor(BitmapProcessor postProcessor) {
this.postProcessor = postProcessor;
return this;
} public Builder displayer(BitmapDisplayer displayer) {
if (displayer == null) throw new IllegalArgumentException("displayer can't be null");
this.displayer = displayer;
return this;
} Builder syncLoading(boolean isSyncLoading) {
this.isSyncLoading = isSyncLoading;
return this;
} public Builder handler(Handler handler) {
this.handler = handler;
return this;
} /** Sets all options equal to incoming options */
public Builder cloneFrom(DisplayImageOptions options) {
imageResOnLoading = options.imageResOnLoading;
imageResForEmptyUri = options.imageResForEmptyUri;
imageResOnFail = options.imageResOnFail;
imageOnLoading = options.imageOnLoading;
imageForEmptyUri = options.imageForEmptyUri;
imageOnFail = options.imageOnFail;
resetViewBeforeLoading = options.resetViewBeforeLoading;
cacheInMemory = options.cacheInMemory;
cacheOnDisk = options.cacheOnDisk;
imageScaleType = options.imageScaleType;
decodingOptions = options.decodingOptions;
delayBeforeLoading = options.delayBeforeLoading;
considerExifParams = options.considerExifParams;
extraForDownloader = options.extraForDownloader;
preProcessor = options.preProcessor;
postProcessor = options.postProcessor;
displayer = options.displayer;
handler = options.handler;
isSyncLoading = options.isSyncLoading;
return this;
} /** Builds configured {@link DisplayImageOptions} object */
public DisplayImageOptions build() {
return new DisplayImageOptions(this);
}
} public static DisplayImageOptions createSimple() {
return new Builder().build();
}
}

builder-设计模式的更多相关文章

  1. C#创建IIS站点及相应的应用程序池,支持IIS6.0+Windows Server 2003. 使用Builder设计模式

    测试项目结构: PS:IIS6UtilsBuilder, IIS7UtilsBuilder,IISUtilsBuilder以及IISDirector为Builder设计模式实现的核心代码.Progra ...

  2. Java调用FFmpeg进行视频处理及Builder设计模式的应用

    1.FFmpeg是什么 FFmpeg(https://www.ffmpeg.org)是一套可以用来记录.转换数字音频.视频,并能将其转化为流的开源计算机程序.它用来干吗呢?视频采集.视频格式转化.视频 ...

  3. Builder设计模式--改善构造器多个参数时可显著改善可读性

    作为一名程序开发者,设计模式其实一直有在接触,只是没有专门的去学过,所以可能对设计模式没有一个系统的理解.在一次项目中,需要使用到第三方服务商提供的功能,为了尽快的熟悉其功能代码,在官网下了demo来 ...

  4. builder设计模式(摘录ITeye文章lintomny)

    对于Builder模式很简单,但是一直想不明白为什么要这么设计,为什么要向builder要Product而不是向知道建造过程的Director要.刚才google到一篇文章,总算清楚了.在这里转贴一下 ...

  5. Builder 设计模式的学习

    Buileder(生成器)—对象创建型模式 一 意图 将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示. 二 适用性 在以下情况使用Build模式: 1 当创建复杂对象的算法应 ...

  6. [DesignPattern]Builder设计模式

    模式的定义 将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示. 模式的使用场景 相同的方法,不同的执行顺序,产生不同的事件结果时: 多个部件或零件,都可以装配到一个对象中,但是 ...

  7. Builder设计模式

    Builder模式,又称生成器或构建者模式,属于对象创建型模式,侧重于一步一步的构建复杂对象,只有在构建完成后才会返回生成的对象.Builder模式将一个复杂对象的构建与它的表示分离,使得同样的构建过 ...

  8. 23种设计模式之Builder设计模式

    概述 建造者模式(Builder Pattern),是创造性模式之一,Builder 模式的目的则是为了将对象的构建与展示分离.Builder 模式是一步一步创建一个复杂对象的创建型模式,它允许用户在 ...

  9. [学习笔记]设计模式之Builder

    写在前面 为方便读者,本文已添加至索引: 设计模式 学习笔记索引 作为一个新入职的魔导士呢,哦不,是程序员,我以为并没有太多机会去设计项目的软件架构.但是,工作一段时间之后,自己渐渐意识到,哪怕是自己 ...

  10. Android开发中常见的设计模式(二)——Builder模式

    了解了单例模式,接下来介绍另一个常见的模式--Builder模式. 那么什么是Builder模式呢.通过搜索,会发现大部分网上的定义都是 将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建 ...

随机推荐

  1. lucene和egg项目的异同点

    1 和lucene一样 支持全域索引 2 对字符串域提供全文检索,对数字类型域提供范围查询 3 采取和lucene类似的倒排表压缩方式 4 和lucene的多级跳转表不同,egg采取的是B+树做索引, ...

  2. C#操作SQL Server通用类

    using System; using System.Data; using System.Xml; using System.Data.SqlClient; using System.Collect ...

  3. CSS浮动属性Float到底什么怎么回事,下面详细解释一下

    float 是 css 的定位属性.在传统的印刷布局中,文本可以按照需要围绕图片.一般把这种方式称为“文本环绕”.在网页设计中,应用了CSS的float属性的页面元素就像在印刷布局里面的被文字包围的图 ...

  4. jquery相关代码

    1.jquery获取当前选中select的text值 var checkText=$("#slc1").find("option:selected").text ...

  5. solr4.x配置IK2012FF智能分词+同义词配置

    本文配置环境:solr4.6+ IK2012ff +tomcat7 在Solr4.0发布以后,官方取消了BaseTokenizerFactory接口,而直接使用Lucene Analyzer标准接口T ...

  6. SQL 必知必会-- 第17课:创建和操作表

    我这里用的是oracle 10g,PL/SQL来做的. 第17课  创建和操纵表  14517.1  创建表  14517.2  更新表  15017.3  删除表  15317.4  重命名表  1 ...

  7. 结合源码看nginx-1.4.0之nginx异步机制详解

    目录 0. 摘要 1. nginx异步设计思想 2. nginx异步设计数据结构 3. nginx异步机制源码解析 4. 一个简单的应用异步例子 5. 小结 6. 参考源码

  8. ASP.NET的票据工具类FormsAuthenticationTicket

    票据是asp.net登录验证的一种方式,以前研究过,现在并不使用,今天发现了,记录一下. /*###################票据工具################### * 1.设置< ...

  9. linux_机器信息查询

    查看系统版本:[root@css-management ~]# lsb_release -aLSB Version: :core-4.0-amd64:core-4.0-noarch:graphics- ...

  10. 关于automatic_Panoramic_Image_Stitching_using_Invariant_features 的阅读笔记(2)

    接上一篇: http://www.cnblogs.com/letben/p/5446074.html#3538201 捆绑调整 (好开心有同学一起来看看这些问题,要不然就是我自己的话,我应该也不会看的 ...