建造者模式可以将一个产品的内部表象与产品的生成过程分割开来,从而可以使一个建造过程生成具有不同的内部表象的产品对象。

UML图示:


1. com.cleversoft.designpatterns.builder2.PostalClert.java:
package com.cleversoft.designpatterns.builder2;

public class PostalClert {
    
public void construct(LetterBuilder letterBuilder) {
        letterBuilder.buildEnvelop();
        letterBuilder.buildStamp();
    }

}

2. com.cleversoft.designpatterns.builder2.LetterBuilder.java:
package com.cleversoft.designpatterns.builder2;

public abstract class LetterBuilder {
    
public abstract void buildEnvelop();

    
public abstract void buildStamp();

    
public abstract Letter getLetter();
}

3. com.cleversoft.designpatterns.builder2.OrdinaryMailBuilder.java:
package com.cleversoft.designpatterns.builder2;

public class OrdinaryMailBuilder extends LetterBuilder {
    
private Letter ordinaryMail = new OrdinaryMail();

    
public void buildEnvelop() {
        ordinaryMail.add(
"ordinaryEnvelop"0.1);
    }


    
public void buildStamp() {
        ordinaryMail.add(
"ordinaryStamp"1.0);
    }


    
public Letter getLetter() {
        
return ordinaryMail;
    }


}

4. com.cleversoft.designpatterns.builder2.RegisteredLetterBuilder.java:
package com.cleversoft.designpatterns.builder2;

public class RegisteredLetterBuilder extends LetterBuilder {
    
private Letter registeredLetter = new RegisteredLetter();

    
public void buildEnvelop() {
        registeredLetter.add(
"registeredEnvelop"2.0);
    }


    
public void buildStamp() {
        registeredLetter.add(
"registeredStamp"15.0);
    }


    
public Letter getLetter() {
        
return registeredLetter;
    }

}

5. com.cleversoft.designpatterns.builder2.Letter.java:
package com.cleversoft.designpatterns.builder2;

public interface Letter {

    
public abstract void add(String type, double price);

    
public abstract void list();

}

6. com.cleversoft.designpatterns.builder2.OrdinaryMail.java:
package com.cleversoft.designpatterns.builder2;

import java.util.Enumeration;
import java.util.Hashtable;

public class OrdinaryMail implements Letter {
    Hashtable
<String, Double> letter = new Hashtable<String, Double>();

    
/*
     * (non-Javadoc)
     * 
     * @see com.cleversoft.designpatterns.builder2.ILetter#add(java.lang.String,
     *      double)
     
*/

    
public void add(String type, double price) {
        letter.put(type, price);
    }


    
/*
     * (non-Javadoc)
     * 
     * @see com.cleversoft.designpatterns.builder2.ILetter#show()
     
*/

    
public void list() {
        System.out.println(
"Ordinary Letter List\n");
        Enumeration keys 
= letter.keys();
        
while (keys.hasMoreElements()) {
            String key 
= keys.nextElement().toString();
            String value 
= letter.get(key).toString();
            System.out.println(key 
+ " " + value + "");
        }


    }

}

7. com.cleversoft.designpatterns.builder2.RegisteredLetter.java:
package com.cleversoft.designpatterns.builder2;

import java.util.Enumeration;
import java.util.Hashtable;

public class RegisteredLetter implements Letter {
    Hashtable
<String, Double> letter = new Hashtable<String, Double>();

    
/*
     * (non-Javadoc)
     * 
     * @see com.cleversoft.designpatterns.builder2.ILetter#add(java.lang.String,
     *      double)
     
*/

    
public void add(String type, double price) {
        letter.put(type, price);
    }


    
/*
     * (non-Javadoc)
     * 
     * @see com.cleversoft.designpatterns.builder2.ILetter#show()
     
*/

    
public void list() {
        System.out.println(
"Registered Letter List\n");
        Enumeration keys 
= letter.keys();
        
while (keys.hasMoreElements()) {
            String key 
= keys.nextElement().toString();
            String value 
= letter.get(key).toString();
            System.out.println(key 
+ " " + value + "");
        }


    }

}

8. com.cleversoft.designpatterns.builder2.Main.java:
package com.cleversoft.designpatterns.builder2;

public class Main {

    
public static void main(String[] args) {
        LetterBuilder lb 
= new OrdinaryMailBuilder();
        PostalClert pc 
= new PostalClert();
        pc.construct(lb);

        Letter l 
= lb.getLetter();
        l.list();
    }

}


参考资料:
1. http://terrylee.cnblogs.com/archive/2005/12/19/299878.html
2. 《Java与模式》

欢迎大家访问我的个人网站 萌萌的IT人