Android中的Builder模式

没有Builder模式的时候,是怎么样写代码的?

举个组装电脑的例子来说明。有一台电脑类,如下

public class Computer {
	private String cpu;
	private String motherboard;
	private String displayCard;
	private String ram;
	private String disk;
	private String power;
}

如果要组装这样一台电脑,可以使用构造方法传入参数列表,构建一个对象

public class Computer {
	private String cpu;
	private String motherboard;
	private String displayCard;
	private String ram;
	private String disk;
	private String power;
	
	public Computer(String cpu, String motherboard, String displayCard, String ram, String disk, String power) {
		this.cpu = cpu;
		this.motherboard = motherboard;
		this.displayCard = displayCard;
		this.ram = ram;
		this.disk = disk;
		this.power = power;
	}
}

但是参数列表的长度也太长了,看得都烦躁。而且,在使用构造 Computer 对象的时候,有可能暂时不需要传入某些参数,比如目前只需要一个 CPU,那么就要重新写一个构造方法,这样也挺麻烦的。

public Computer(String cpu) {
	this.cpu = cpu;
}

Builder模式解决的问题

要实现电脑配件的定制化,这个时候,就可以使用 Builder 模式。

在 Computer 类中写一个 Builder 类,配件的装备工作交由 Builder 来完成。

public class Computer {
	private String cpu;
	private String motherboard;
	private String displayCard;
	private String ram;
	private String disk;
	private String power;

	public void setCpu(String cpu) {
		this.cpu = cpu;
	}

	public void setMotherboard(String motherboard) {
		this.motherboard = motherboard;
	}

	public void setDisplayCard(String displayCard) {
		this.displayCard = displayCard;
	}

	public void setRam(String ram) {
		this.ram = ram;
	}

	public void setDisk(String disk) {
		this.disk = disk;
	}

	public void setPower(String power) {
		this.power = power;
	}

	public static class Builder {
		private String cpu;
		private String motherboard;
		private String displayCard;
		private String ram;
		private String disk;
		private String power;
		
		public Builder setCpu(String cpu) {
			this.cpu = cpu;
			return this;
		}

		public Builder setMotherboard(String motherboard) {
			this.motherboard = motherboard;
			return this;
		}

		public Builder setDisplayCard(String displayCard) {
			this.displayCard = displayCard;
			return this;
		}

		public Builder setRam(String ram) {
			this.ram = ram;
			return this;
		}

		public Builder setDisk(String disk) {
			this.disk = disk;
			return this;
		}

		public Builder setPower(String power) {
			this.power = power;
			return this;
		}
		
		public Computer create() {
			Computer computer = new Computer();
			
			if (cpu != null) {
				computer.setCpu(cpu);
			}
			if (motherboard != null) {
				computer.setMotherboard(motherboard);
			}
			if (displayCard != null) {
				computer.setDisplayCard(displayCard);
			}
			if (ram != null) {
				computer.setRam(ram);
			}
			if (disk != null) {
				computer.setDisk(disk);
			}
			if (power != null) {
				computer.setPower(power);
			}
			
			return computer;
		}
	}
}

组装电脑的代码如下

Computer computer = new Computer.Builder()
	.setCpu("Intel Core i7")
	.setMotherboard("GIGABYTE Z97")
	.setDisplayCard("GTX Titan")
	.setRam("32G")
	.setDisk("2TB")
	.setPower("1000W")
	.create();

通过使用 Builder 模式,实现了对象的定制化构建,而且采用链式调用,方便了代码编写。

总结

Builder 解决的是对象的定制化构建。

posted @ 2016-09-08 22:21  勇敢的少年啊  阅读(458)  评论(0编辑  收藏  举报