建造者模式
建造者模式的核心思想是将一个"复杂对象的构建算法"与它的"部件及组装方式"分离,使得构件算法和组装方式可以独立应对变化;复用同样的构建算法可以创建不同的表示,不同的构建过程可以复用相同的部件组装方式。
好吧,我承认上面那句话是Baidu来的,要是能很容易的明白讲的是什么,请切换到漫游模式继续漫游吧。
下面我们不聊这些模式了,继续聊我们的实验室,因为模式不发给我们工资,实验室才是发工资的。
最近我们的实验室又遇到麻烦了,老总们即使能够同时使用女秘书了也忍不住发火了,因为赶太阳国一大笔兽人订单,到交付的时候才发现,大都数兽人没有头,没有头的兽人能叫兽人吗?那只能叫无头兽!
class ShouRen { private String head; private String body; private String foot; public void setHead(String head) { this.head = head; } public void setBody(String body) { this.body = body; } public void setFoot(String foot) { this.foot = foot; } } public class Builder { public static void main(String[] args) { ShouRen sr = new ShouRen(); sr.setBody("兽身"); sr.setFoot("兽脚"); } }
为了解决这种浪费材料的问题再次发生,我们实验室又开了大会,某位管理层说这是因为兽人分厂建设在太阳国的原因,当地的土著完全不理解我们兽人工厂生产兽人时的步骤,所以才会有这个问题,所以要加大兽人员工的培训,让他们的技能和我们人类工厂的员工达到一致水平。老总们一听,立即暴怒,不说培训兽人员工的费用问题(其实就是我们coding时了解其他类的接口花费的时间),就兽人的智商,即使培训了也不一定有效果(coding时面对一个上千行的类,实在是提不起兴致过一遍 -_-! )。
这时有是我们的资深顾问说,既然这样,我们就不要让员工自己创建兽人了,我们给他们发一个兽人创建器,他们只要把原料放到创建器里面自动就出来一个兽人好了。
class ShouRen { private String head; private String body; private String foot; public void setHead(String head) { this.head = head; } public void setBody(String body) { this.body = body; } public void setFoot(String foot) { this.foot = foot; } } class ShouRenBuilder { ShouRen sr; public ShouRenBuilder (){ sr = new ShouRen(); } public void createHead() { sr.setHead("兽头"); } public void createBody() { sr.setBody("兽身"); } public void createFoot() { sr.setFoot("兽脚"); } public ShouRen createShouRen() { createHead(); createBody(); createFoot(); return sr; } } public class Builder { public static void main(String[] args) { ShouRen sr = new ShouRenBuilder().createShouRen(); } }
老总一看这个方法不错,大肆赞赏,并且希望扩大使用范围,希望我们的正常人工厂和人妖工厂也使用这种构造器。
class Ren { } class NanRen extends Ren { private String head; private String body; private String foot; public void setHead(String head) { this.head = head; } public void setBody(String body) { this.body = body; } public void setFoot(String foot) { this.foot = foot; } } class NvRen extends Ren { private String head; private String body; private String foot; public void setHead(String head) { this.head = head; } public void setBody(String body) { this.body = body; } public void setFoot(String foot) { this.foot = foot; } } class RenYao extends Ren { private String head; private String body; private String foot; public void setHead(String head) { this.head = head; } public void setBody(String body) { this.body = body; } public void setFoot(String foot) { this.foot = foot; } } class ShouRen extends Ren { private String head; private String body; private String foot; public void setHead(String head) { this.head = head; } public void setBody(String body) { this.body = body; } public void setFoot(String foot) { this.foot = foot; } } class NanRenBuilder { NanRen nr; public NanRenBuilder (){ nr = new NanRen(); } public void createHead() { nr.setHead("男人头"); } public void createBody() { nr.setBody("男人身"); } public void createFoot() { nr.setFoot("男人脚"); } public NanRen createNanRen() { createHead(); createBody(); createFoot(); return nr; } } class NvRenBuilder { NvRen nr; public NvRenBuilder (){ nr = new NvRen(); } public void createHead() { nr.setHead("女人头"); } public void createBody() { nr.setBody("女人身"); } public void createFoot() { nr.setFoot("女人脚"); } public NvRen createNvRen() { createHead(); createBody(); createFoot(); return nr; } } class RenYaoBuilder { RenYao ry; public RenYaoBuilder (){ ry = new RenYao(); } public void createHead() { ry.setHead("人妖头"); } public void createBody() { ry.setBody("人妖身"); } public void createFoot() { ry.setFoot("人妖脚"); } public RenYao createRenYao() { createHead(); createBody(); createFoot(); return ry; } } class ShouRenBuilder { ShouRen sr; public ShouRenBuilder (){ sr = new ShouRen(); } public void createHead() { sr.setHead("兽头"); } public void createBody() { sr.setBody("兽身"); } public void createFoot() { sr.setFoot("兽脚"); } public ShouRen createShouRen() { createHead(); createBody(); createFoot(); return sr; } }
这时财务科的人发表意见了,如果我们的各大工厂都使用构造器的话,确实能省下不少工人的培训费用,但是硬件成本会加大不少阿(即重复代码),并且提出,我们的生产流程是一样的,都是先创建头,再创建身体,最后创建脚。
所以我们把组装的工作弄出一个组装器,然后要生产不同的人的时候仿佛不同的构造器,那么我们原先放在构造器中的组装步骤就能够重用了。于是我们就不知不觉的用上了建造者模式来生产。
class Ren { } class NanRen extends Ren { private String head; private String body; private String foot; public void setHead(String head) { this.head = head; } public void setBody(String body) { this.body = body; } public void setFoot(String foot) { this.foot = foot; } } class NvRen extends Ren { private String head; private String body; private String foot; public void setHead(String head) { this.head = head; } public void setBody(String body) { this.body = body; } public void setFoot(String foot) { this.foot = foot; } } class RenYao extends Ren { private String head; private String body; private String foot; public void setHead(String head) { this.head = head; } public void setBody(String body) { this.body = body; } public void setFoot(String foot) { this.foot = foot; } } class ShouRen extends Ren { private String head; private String body; private String foot; public void setHead(String head) { this.head = head; } public void setBody(String body) { this.body = body; } public void setFoot(String foot) { this.foot = foot; } } interface RenBuilder { public abstract void createHead(); public abstract void createBody(); public abstract void createFoot(); public abstract Ren getRen(); } class NanRenBuilder implements RenBuilder{ NanRen nr; public NanRenBuilder (){ nr = new NanRen(); } public void createHead() { nr.setHead("男人头"); } public void createBody() { nr.setBody("男人身"); } public void createFoot() { nr.setFoot("男人脚"); } public Ren getRen() { return nr; } } class NvRenBuilder implements RenBuilder{ NvRen nr; public NvRenBuilder (){ nr = new NvRen(); } public void createHead() { nr.setHead("女人头"); } public void createBody() { nr.setBody("女人身"); } public void createFoot() { nr.setFoot("女人脚"); } public Ren getRen() { return nr; } } class RenYaoBuilder implements RenBuilder{ RenYao ry; public RenYaoBuilder (){ ry = new RenYao(); } public void createHead() { ry.setHead("人妖头"); } public void createBody() { ry.setBody("人妖身"); } public void createFoot() { ry.setFoot("人妖脚"); } public Ren getRen() { return ry; } } class ShouRenBuilder implements RenBuilder{ ShouRen sr; public ShouRenBuilder (){ sr = new ShouRen(); } public void createHead() { sr.setHead("兽头"); } public void createBody() { sr.setBody("兽身"); } public void createFoot() { sr.setFoot("兽脚"); } public Ren getRen() { return sr; } } class RenDirector{ public Ren create(RenBuilder builder) { builder.createHead(); builder.createBody(); builder.createFoot(); return builder.getRen(); } } public class Builder { public static void main(String[] args) { ShouRenBuilder srBuilder = new ShouRenBuilder();//兽人建造核心 NanRenBuilder nanrBuilder = new NanRenBuilder();//男人建造核心 NvRenBuilder nvrBuilder = new NvRenBuilder();//女人建造核心 RenYaoBuilder ryBuilder = new RenYaoBuilder();//人妖建造核心 RenDirector rd = new RenDirector();//只有一个组装器,装上不同的建造核心,就能建出不同的人。 Ren sr = rd.create(srBuilder); Ren nanren = rd.create(nanrBuilder); Ren nvren = rd.create(nvrBuilder); Ren ry = rd.create(ryBuilder); } }
至此,创建型的五种设计模式就举例完了。后面的坑慢慢填。