设计模式之Builder——购机篇  
               
              最近想买一台电脑用于学习,因此我就去了一家电脑公司,经过分析,选用了下面的配置: 
            CPU P2.4 
            主板 Intel 
            硬盘 80G 
            。。。 
            买过电脑的朋友可能都知道,我们选好配置后,电脑公司就会有专门的组装师(Assembler)来给我们装机。电脑(Computer)就是由这些东西(我们称之为Part)组成的。学过经济学的朋友可能都知道,如果这台组装好的电脑不卖掉,那它就不是商品(Commodity),而仅仅是台电脑而已。 
            1、 在这里,我们先定义商品(Commodity)类: 
            public class Commodity { 
             String commodity =""; 
             public Commodity (Part partA,Part partB,Part partC) 
              {//由各个部分组成 
             this. commodity = partA.part+"\n"; 
             this. commodity = product+partB.part+"\n"; 
             this. commodity = product+partC.part; 
             System.out.println("我的机器配置为:\n"+ commodity); 
             } 
            } 
            2、 下来我们再定义电脑的组成部分(Part)类: 
            public class Part { 
             String part=""; 
             public Part(String part){ 
             this.part = part; 
             } 
            } 
            3、 我们把电脑(Computer)定义成一个接口类: 
            public interface Computer { 
            //组装部件A 比如CPU 
             void buildPartA(); 
              
            //组装部件B 比如主板 
             void buildPartB(); 
              
            //组装部件C 比如硬盘 
             void buildPartC(); 
              
            //返回最后组装成品结果 (返回最后组装好的电脑) 
            //成品的组装过程不在这里进行,而是由组装师(Assembler)类完成的。 
            //从而实现了过程和部件的分离 
             Product getProduct(); 
            } 
            4、 定义电脑的组装师(Assembler)类: 
            public class Assembler { 
             private Computer computer; 
             public Assembler(Computer computer) { //主要任务是装电脑 
             this.computer = computer; 
             } 
              
            // 将部件partA partB partC最后组成复杂对象 
            //这里是将主板、CPU和硬盘组装成PC的过程 
             public void construct() { 
             computer.buildPartA(); 
             computer.buildPartB(); 
             computer.buildPartC(); 
             } 
            } 
            5、 我的电脑是对电脑(Computer)接口的具体实现,因此再定义MyComputer实现类: 
            public class MyComputer implements Computer { 
             Part partA, partB, partC; 
             public void buildPartA() { 
             partA = new Part("P42.4 CPU"); 
             } 
             public void buildPartB() { 
             partB = new Part("Inter 主板"); 
             } 
             public void buildPartC() { 
             partC = new Part("80G硬盘"); 
             } 
             public Product getProduct() { 
            //返回最后组装成品结果 
             Commodity myComputer = new Commodity (partA,partB,partC); 
             return myComputer; 
             } 
            } 
            6、 编写测试类: 
            public class MyComputerTest { 
             public static void main(String args[]){ 
             MyComputer myComputer = new MyComputer(); //组装我的电脑 
             Assembler assembler = new Assembler( myComputer 
              ); //派某一位组装师 
             assembler.construct(); //组装师进行组装过程 
             Commodity commodity = myComputer.getProduct(); 
              //卖给我的电脑(商品) 
             } 
            } 
            7、说明: 
            A:代码只用来学习Builder模式,要运行的话,必须要做一点改动。 
            B:将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。因为每个人的电脑配置可能都是不同的。 
            C:我们使用Builer是为了构建复杂对象的过程和它的部件解耦,也就是说将过程分的尽可能细,而且每一部分只用完成自己的功能即可(各司其职嘛)。 
               
             
             |