建造者模式,是将建造(制造)很多产品对象中很多复杂过程,封装了起来,直接给用户一个建造好的产品对象,用户不需要关系这个非常复杂的产品是如果建造出来的;
生活中举例:一个客户去买汽车,他去了专卖店花钱买了一台汽车,给客户的是完整的汽车,不可能给客户是汽车的零件(发动机,方向盘,电池,轮胎……..),还让客户自己去组装吧!
建造者模式的建造者定义了建造规范,被建造者建造出来的产品,遵循的是建造者的规则,就像(肯德基,麦当劳)味道都一样,因为他们遵循了规范化,不依赖细节,而是让细节依赖于抽象(这也就是依赖倒置原则);
案例一:(建造电脑)
package com.build.demo3;
import java.util.HashSet;
import java.util.Set;
/**
* 定义生产电脑出来的具体产品类,此产品类对象真实中非常的复制,而为了演示就非常的简单举例
* @author Liudeli
*
*/
public class Product {
private Set<String> product = new HashSet<String>();
/**
* 真实产品过程是非常复杂,这里就简单的描述下
* @param computer
*/
public void addComputer(String computer) {
product.add(computer);
}
/**
* 得到产品类辛辛苦苦制作的产品
* @return
*/
public Set<String> getComputer() {
return product;
}
}
package com.build.demo3;
/**
* 建造电脑,以抽象规范行为,让细节依赖抽象,从而也是依赖倒置原则
* @author Liudeli
*
*/
public abstract class BuildComputer {
/**
* 建造CPU
*/
public abstract void buildCPU();
/**
* 建造硬盘
*/
public abstract void buildHardDisk();
/**
* 建造主板
*/
public abstract void buildMainBoard();
/**
* 建造显示屏
*/
public abstract void buildDisplay();
// 还有很多配件就不写了
// 省略 ....
/**
* 获取到产品信息
*/
public abstract void careateProduct();
}
package com.build.demo3;
import java.util.Set;
/**
* 建造具体华硕电脑
* @author Liudeli
*
*/
public class ASUSComputer extends BuildComputer {
private Product product = new Product();
@Override
public void buildCPU() {
product.addComputer("华硕电脑CUP...");
}
@Override
public void buildHardDisk() {
product.addComputer("华硕电脑硬盘...");
}
@Override
public void buildMainBoard() {
product.addComputer("华硕电脑主板...");
}
@Override
public void buildDisplay() {
product.addComputer("华硕电脑显示屏幕...");
}
@Override
public void careateProduct() {
Set<String> computer = product.getComputer();
for (String string : computer) {
System.out.println(string.toString());
}
}
}
package com.build.demo3;
import java.util.Set;
/**
* 建造具体联想电脑
* @author Liudeli
*
*/
public class LenovoComputer extends BuildComputer {
private Product product = new Product();
@Override
public void buildCPU() {
product.addComputer("联想电脑CPU...");
}
@Override
public void buildHardDisk() {
product.addComputer("联想电脑的硬盘...");
}
@Override
public void buildMainBoard() {
product.addComputer("联想电脑的主板...");
}
@Override
public void buildDisplay() {
product.addComputer("联想电脑的显示屏...");
}
@Override
public void careateProduct() {
Set<String> computer = product.getComputer();
for (String string : computer) {
System.out.println(string.toString());
}
}
}
package com.build.demo3;
import java.util.Set;
/**
* 建造具体三星电脑
* @author Liudeli
*
*/
public class SamsungComputer extends BuildComputer{
private Product product = new Product();
@Override
public void buildCPU() {
product.addComputer("三星电脑CPU...");
}
@Override
public void buildHardDisk() {
product.addComputer("三星电脑硬盘...");
}
@Override
public void buildMainBoard() {
product.addComputer("三星电脑主板...");
}
@Override
public void buildDisplay() {
product.addComputer("三星电脑的显示屏...");
}
@Override
public void careateProduct() {
Set<String> computer = product.getComputer();
for (String string : computer) {
System.out.println(string.toString());
}
}
}
package com.build.demo3;
/**
* 指挥者,指挥去建造那一台电脑,与建造抽象交互
* @author Liudeli
*
*/
public class Director {
private static Director director = null;
public static Director getInstance() {
if (null == director) {
synchronized (Director.class) {
director = new Director();
}
}
return director;
}
public void createCar(BuildComputer buildComputer) {
buildComputer.buildCPU();
buildComputer.buildDisplay();
buildComputer.buildHardDisk();
buildComputer.buildMainBoard();
buildComputer.careateProduct();
}
}
package com.build.demo3;
/**
* 客户端测试程序
* @author Liudeli
*
*/
public class Client {
public static void main(String[] args) {
// 调用指挥者
Director director = new Director();
// 得到一台联想电脑对象示例(具体细节,具体建造,具体产品,等等 复杂的一切过程我不用去管)
director.createCar(new LenovoComputer());
director.createCar(new ASUSComputer());
director.createCar(new SamsungComputer());
}
}
运行结果:
案例二:(建造汽车)
package com.build.demo2;
import java.util.ArrayList;
import java.util.List;
/**
* 产品类,注意:具体产品类是非常非常复杂的,组件很多,而在这里只是很简单的描述一下
* @author Liudeli
*
*/
public class Product {
/**
* 产品集合管理
*/
private List<String> listProduct = new ArrayList<String>();
/**
* 增加产品零部件
* @param product
*/
public void addProduct(String product) {
listProduct.add(product);
}
/**
* 得到产品集合
* @return
*/
public List<String> getProduct() {
return listProduct;
}
}
package com.build.demo2;
/**
* 建造汽车抽象类
* @author Liudeli
*
*/
public abstract class BuildCar {
/**
* 此行为建造汽车轮胎
*/
public abstract void buildCarTyre();
/**
* 此行为建造汽车发动机
*/
public abstract void buildCarEngine();
/**
* 得到汽车建造结果
*/
public abstract void getCarBuildResult();
}
package com.build.demo2;
import java.util.List;
/**
* 建造的具体实现过程,第一批零件
* @author Liudeli
*
*/
public class BuildCarSpecific1 extends BuildCar {
private Product product = new Product();
public void buildCarTyre() {
product.addProduct("四个轮胎的内胎...");
}
public void buildCarEngine() {
product.addProduct("整个发动机引擎的气门活塞部分...");
}
public void getCarBuildResult() {
List<String> productResult = product.getProduct();
for (Object object : productResult) {
System.out.println(object.toString());
}
}
}
package com.build.demo2;
import java.util.List;
/**
* 建造的具体实现过程,第二批零件
* @author Liudeli
*
*/
public class BuildCarSpecific2 extends BuildCar {
private Product product = new Product();
public void buildCarTyre() {
product.addProduct("四个轮胎的轮毂部分....");
}
public void buildCarEngine() {
product.addProduct("整个发动机引擎变速箱部分...");
}
public void getCarBuildResult() {
List<String> productResult = product.getProduct();
for (String string : productResult) {
System.out.println(string.toString());
}
}
}
package com.build.demo2;
/**
* 定义指挥者,目的是为了调用建造者,让客户端程序和具体建造产品的过程分离开
* @author Liudeli
*
*/
public class Director {
/**
* 与建造具体交互在指挥者行为完成
*/
public void createCar(BuildCar buildCar) {
if (null != buildCar) {
buildCar.buildCarTyre();
buildCar.buildCarEngine();
buildCar.getCarBuildResult();
}
}
}
package com.build.demo2;
/**
* 客户端测试程序
* @author Liudeli
*
*/
public class Main {
public static void main(String[] args) {
// 得到指挥者(导演类)
Director director = new Director();
director.createCar(new BuildCarSpecific1());
director.createCar(new BuildCarSpecific2());
}
}
运行结果:
案例三:(建造小人)
此案例是看了一篇大话设计模式文章才发现这是建造模式最好的理解方式
package com.build.demo1;
/**
* 建造person
* @author Liudeli
*
*/
public abstract class BuildPerson {
/**
* 建造头部
*/
public abstract void buildHead();
/**
* 建造身体
*/
public abstract void buildBody();
/**
* 建造左手
*/
public abstract void buildLeftHand();
/**
* 建造右手
*/
public abstract void buildRightHand();
/**
* 建造左脚
*/
public abstract void buildLeftFoot();
/**
* 建造右脚
*/
public abstract void buildRightFoot();
}
package com.build.demo1;
/**
* 胖人对象
* @author Liudeli
*
*/
public class FatPerson extends BuildPerson {
public void buildHead() {
System.out.println("胖人头部...");
}
public void buildBody() {
System.out.println("胖人身体部分...");
}
public void buildLeftHand() {
System.out.println("胖人左手...");
}
public void buildRightHand() {
System.out.println("胖人右手...");
}
public void buildLeftFoot() {
System.out.println("胖人左脚...");
}
public void buildRightFoot() {
System.out.println("胖人右脚...");
}
}
package com.build.demo1;
/**
* 瘦人对象
* @author Liudeli
*
*/
public class HighPerson extends BuildPerson {
public void buildHead() {
System.out.println("瘦人头部...");
}
public void buildBody() {
System.out.println("瘦人身体部分...");
}
public void buildLeftHand() {
System.out.println("瘦人左手...");
}
public void buildRightHand() {
System.out.println("瘦人右手...");
}
public void buildLeftFoot() {
System.out.println("瘦人左脚...");
}
public void buildRightFoot() {
System.out.println("瘦人右脚...");
}
}
package com.build.demo1;
/**
* 高人对象
* @author Liudeli
*
*/
public class ThinPerson extends BuildPerson {
public void buildHead() {
System.out.println("高人头部...");
}
public void buildBody() {
System.out.println("高人身体部分...");
}
public void buildLeftHand() {
System.out.println("高人左手...");
}
public void buildRightHand() {
System.out.println("高人右手...");
}
public void buildLeftFoot() {
System.out.println("高人左脚...");
}
public void buildRightFoot() {
System.out.println("瘦人右脚...");
}
}
package com.build.demo1;
/**
* 指挥者对象
* @author Liudeli
*
*/
public class Director {
private BuildPerson buildPerson = null;
public Director(BuildPerson buildPerson) {
this.buildPerson = buildPerson;
}
public void careatePerson() {
if (null != buildPerson) {
buildPerson.buildHead();
buildPerson.buildBody();
buildPerson.buildLeftHand();
buildPerson.buildRightHand();
buildPerson.buildLeftFoot();
buildPerson.buildRightFoot();
} else {
System.out.println("构造函数中没有传入具体继承子类...");
}
}
}
package com.build.demo1;
/**
* 定义客户端测试程序
* @author Liudeli
*
*/
public class Client {
public static void main(String[] args) {
// 操控指挥者即可
Director director = new Director(new FatPerson());
director.careatePerson();
System.out.println("------------------------");
director = new Director(new ThinPerson());
director.careatePerson();
System.out.println("------------------------");
director = new Director(new HighPerson());
director.careatePerson();
}
}
运行结果:
我们发现这三个案例中,都是细节依赖于抽象(也就是我之前发表的博客,依赖倒置原则),这三个案例中,客户端程序,不需要关心复杂的产品是如何建造出来的,因为建造者模式把复杂的制作过程进行了分离表示;
Project结构:
谢谢大家的观看,更多精彩技术博客,会不断的更新,请大家访问,
刘德利CSDN博客, http://blog.csdn.net/u011967006