zoukankan      html  css  js  c++  java
  • 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 解决的是对象的定制化构建。

  • 相关阅读:
    maven settings
    java.util.Base64
    Centos 7 下 LAMP 部署
    Cisco N3K VPC+HSRP+ospf 配置
    centos 7 下多网卡绑定+ vlan 网卡配置
    centos 7 下 cobbler 安装
    hive0.12 rcfile gzip 测试
    Hive内部表外部表转化分析(装)
    hadoop2.2.0 + hbase 0.94 + hive 0.12 配置记录
    hbase 问题记录
  • 原文地址:https://www.cnblogs.com/okadanana/p/5854723.html
Copyright © 2011-2022 走看看