设计模式学习笔记十:构造器模式

构造器(Builder)模式,也叫生成器模式,属于创建型模式之一。GoF设计模式一书描述此模式的意图是将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。适用场景:创建复杂对象,并且该对象的算法可以独立出来进行复用,构造过程必须允许被构造的对象有不同的表示。

本例子代码基于GoF Builder设计模式场景的Java实现,因为在阅读别人的构造器模式示例代码的时候发现都描述的都不全面,基本都是没有明确的表达出Director的作用。

代码如下:

 

package com.array7.builder;

import java.util.ArrayList;
import java.util.List;

public class Run {
    public static void main(String[] args) {
        TextConverter converter = null;
        RTFReader rtfReader = null;
        List<Integer> tokens = new ArrayList<Integer>();
        tokens.add(RTFReader.CHAR);
        tokens.add(RTFReader.PARA);
        tokens.add(RTFReader.FONT);
        tokens.add(RTFReader.FONT);
        tokens.add(RTFReader.CHAR);

        converter = new ASCIIConverter();
        rtfReader = new RTFReader(converter);
        rtfReader.parseRTF(tokens);
        // product
        String result = converter.getResult();
        System.out.print("======================ASCIIConverter result=====================\n");
        System.out.println(result);

        converter = new TeXConverter();
        rtfReader = new RTFReader(converter);
        rtfReader.parseRTF(tokens);
        result = converter.getResult();
        System.out.print("======================TeXConverter result=====================\n");
        System.out.println(result);

        converter = new TextWidgetConverter();
        rtfReader = new RTFReader(converter);
        rtfReader.parseRTF(tokens);
        result = converter.getResult();
        System.out.print("======================TextWidgetConverter result=====================\n");
        System.out.println(result);
    }
}

/**
 * Director
 */
class RTFReader {
    private TextConverter converter;

    public final static int CHAR = 0;
    public final static int FONT = 1;
    public final static int PARA = 2;

    public RTFReader(TextConverter converter) {
        this.converter = converter;
    }

    public void parseRTF(List<Integer> tokens) {
        // 此处体现了复用,这是在很多Builde代码中没有体现出来的。
        for (Integer token : tokens) {
            switch (token.intValue()) {
                case CHAR :
                    converter.convertCharacter();
                    continue;
                case FONT:
                    converter.convertFontChange();
                    continue;
                case PARA :
                    converter.convertParagraph();
                    continue;
                default:
                    continue;
            }
        }
    }
}

/**
 * Builder
 */
interface TextConverter {
    public void convertCharacter ();
    public void convertFontChange ();
    public void convertParagraph ();
    public String getResult();
}

/**
 * ConcreteBuilder1
 */
class ASCIIConverter implements TextConverter {
    private StringBuffer result = new StringBuffer();
    @Override
    public void convertCharacter() {
        result.append("ASCIIConverter.convertCharacter finished...\n");
    }

    @Override
    public void convertFontChange() {
        result.append("ASCIIConverter.convertFontChange finished...\n");
    }

    @Override
    public void convertParagraph() {
        result.append("ASCIIConverter.convertParagraph finished...\n");
    }

    @Override
    public String getResult() {
        return result.toString();
    }
}

/**
 * ConcreteBuilder2
 */
class TeXConverter implements TextConverter {
    private StringBuffer result = new StringBuffer();
    @Override
    public void convertCharacter() {
        result.append("TeXConverter.convertCharacter finished...\n");
    }

    @Override
    public void convertFontChange() {
        result.append("TeXConverter.convertFontChange finished...\n");
    }

    @Override
    public void convertParagraph() {
        result.append("TeXConverter.convertParagraph finished...\n");
    }

    @Override
    public String getResult() {
        return result.toString();
    }
}

/**
 * ConcreteBuilder3
 */
class TextWidgetConverter implements TextConverter {
    private StringBuffer result = new StringBuffer();
    @Override
    public void convertCharacter() {
        result.append("TextWidgetConverter.convertCharacter finished...\n");
    }

    @Override
    public void convertFontChange() {
        result.append("TextWidgetConverter.convertFontChange finished...\n");
    }

    @Override
    public void convertParagraph() {
        result.append("TextWidgetConverter.convertParagraph finished...\n");
    }

    @Override
    public String getResult() {
        return result.toString();
    }
}


输出结果:

======================ASCIIConverter result=====================
ASCIIConverter.convertCharacter finished...
ASCIIConverter.convertParagraph finished...
ASCIIConverter.convertFontChange finished...
ASCIIConverter.convertFontChange finished...
ASCIIConverter.convertCharacter finished...

======================TeXConverter result=====================
TeXConverter.convertCharacter finished...
TeXConverter.convertParagraph finished...
TeXConverter.convertFontChange finished...
TeXConverter.convertFontChange finished...
TeXConverter.convertCharacter finished...

======================TextWidgetConverter result=====================
TextWidgetConverter.convertCharacter finished...
TextWidgetConverter.convertParagraph finished...
TextWidgetConverter.convertFontChange finished...
TextWidgetConverter.convertFontChange finished...
TextWidgetConverter.convertCharacter finished...



版权声明:本文为博主原创文章,未经博主允许不得转载。

 

posted @ 2015-06-28 22:13  土豆条  阅读(202)  评论(0编辑  收藏  举报