java链式调用

 之前接入支付宝支付接口的时候,看到支付宝提供的demo中有这么一段代码:

// 创建扫码支付请求builder,设置请求参数
	        AlipayTradePrecreateRequestBuilder builder = new AlipayTradePrecreateRequestBuilder()
	            .setSubject(subject)//订单主题
	            .setTotalAmount(totalAmount)//订单总金额,单位为元
	            .setOutTradeNo(outTradeNo)//订单号
	            .setUndiscountableAmount(undiscountableAmount)//默认0.0
	            .setSellerId(sellerId)//卖家支付宝账号ID
	            .setBody(body)//订单描述
	            .setOperatorId(operatorId)//商户操作员编号 13688889999
	            .setStoreId(storeId)//商户门店编号
	            .setExtendParams(extendParams)//业务扩展参数
	            .setTimeoutExpress(timeoutExpress)//支付超时
	            .setNotifyUrl("http://www.test-notify-url.com")//支付宝服务器主动通知商户服务器里指定的页面http路径,根据需要设置
	            .setGoodsDetailList(goodsDetailList);//商品明细列表
当时在开发没顾得上去了解,现在闲了一点,在看设计模式,今天看到建造者模式,看到代码形态是差不多的,才知道这java链式调用。

废话不多说,直接用代码来介绍java链式调用。

一般,我们在创建javaBean的时候,采用的是new关键字来创建的,

Computer computer = new Computer();
computer.setCPU("inter-i5");
computer.setGPU("GTX-960");
computer.setMemoryType("ddr3 1666MHz");
computer.setMemorySize(8);
...
而采用链式调用的写法的话,则是不一样的风格,这种风格更适合人类的思维,

Computer computer = new Computer.Builder()
        .setCPU("inter-skylake-i7")
        .setGPU("GTX-Titan")
        .setMemoryType("ddr4-2133MHz")
        .setMemorySize(16)
        .setStorageType("ssd")
        .setStorageSize(512)
        .setScreenType("IPS")
        .setScreenSize(28)
        .setOSType("Ubuntu/Window10")
        .create();
下面就是Computer的写法:

public class Computer {
	private String CPU;
	private String GPU;
	private String memoryType;
	private int memorySize;
	private String storageType;
	private int storageSize;
	private String screenType;
	private float screenSize;
	private String OSType;
	
	/**
	 * 构造方法私有化
	 * @param builder
	 */
	private Computer(Builder builder) {
		CPU = builder.CPU;
		GPU = builder.GPU;
		memoryType = builder.memoryType;
		memorySize = builder.memorySize;
		storageType = builder.storageType;
		storageSize = builder.storageSize;
		screenType = builder.screenType;
		screenSize = builder.screenSize;
		OSType = builder.OSType;
	}

	public static class Builder {
		// Optional parameters - initialize with default values
		private String CPU;
		private String GPU;
		private String memoryType;
		private int memorySize;
		private String storageType;
		private int storageSize;
		private String screenType;
		private float screenSize;
		private String OSType;
		
		/**
		 * 提供调用入口
		 * @author niwei
		 * @time   2018下午7:34:26
		 * @return
		 */
		public Computer create() {
			return new Computer(this);
		}
		
		public Builder() {
		}

		public Builder setCPU(String CPU) {
			this.CPU = CPU;
			return this;
		}

		public Builder setGPU(String GPU) {
			this.GPU = GPU;
			return this;
		}

		public Builder setMemoryType(String memoryType) {
			this.memoryType = memoryType;
			return this;
		}

		public Builder setMemorySize(int memorySize) {
			this.memorySize = memorySize;
			return this;
		}

		public Builder setStorageType(String storageType) {
			this.storageType = storageType;
			return this;
		}

		public Builder setStorageSize(int storageSize) {
			this.storageSize = storageSize;
			return this;
		}

		public Builder setScreenType(String screenType) {
			this.screenType = screenType;
			return this;
		}

		public Builder setScreenSize(float screenSize) {
			this.screenSize = screenSize;
			return this;
		}

		public Builder setOSType(String OSType) {
			this.OSType = OSType;
			return this;
		}
	}
}
写下这篇博客,希望对自己加深理解,同事也对需要的人有些许帮助。有不明白的地方,可以参考我的下一篇博客,代码示例比较简洁明了,http://blog.csdn.net/cainiaobulan/article/details/79128574





你可能感兴趣的:(Java,设计模式,java链式调用)