行動のデザインパターン:テンプレートモード

テンプレートモードは、一般的に行動デザインパターンとして使用され、主なアイデアは、テンプレート内のプロセススケルトンコードを定義することである、とスケルトンコードで使用される方法のいくつかは、これらのメソッドは、達成するために、サブクラスに委ね実装されていません。私たちは、事業開発のシナリオを持っている場合は、様々なクレジット・チャネルの信用報告書からの問合せに対する当社の業務システムのニーズは、各アクチュエータ出力の内容は同じではありませんが、彼らは、元のレポートのコンテンツへのアクセス、パースに接続を確立するために3つのステップに分かれていますレポート作成と保存。このように、我々は、テンプレートクラスを定義します。

public abstract class AbstractReportTemplate {    protected Logger logger = LoggerFactory.getLogger(getClass());    public final void doTask(){        doConnect();        getReport();        parseRepot();    }    /**     * 建立连接     */    public abstract void doConnect();    /**     * 获取报告     */    public abstract void getReport();    /**     * 解析报告并落库     */    public abstract void parseRepot();}

我々は今、抽象メソッドの上に達成するために、我々はこのテンプレートクラスを継承限りとすることを、信用調査会社をドッキングしたい場合は、タスクがそのまま流れることができます。

public class AReport extends AbstractReportTemplate {    public void doConnect() {        logger.info("do connect");    }    public void getReport() {        logger.info("get report");    }    public void parseRepot() {        logger.info("parseReport");    }}

メソッドの呼び出し:

public static void main(String[] args){        AbstractReportTemplate aReport = new AReport();        aReport.doTask();    }

目に見える、非常に単純なテンプレートモードを実装します。今私は、ソースコード内のいくつかのテンプレートパターンを紹介します。

1.jdk入力ストリームクラスは抽象読み出し方法を定義するテンプレートクラスであります

public int read(byte b[], int off, int len) throws IOException {        if (b == null) {            throw new NullPointerException();        } else if (off < 0 || len < 0 || len > b.length - off) {            throw new IndexOutOfBoundsException();        } else if (len == 0) {            return 0;        }        int c = read();        if (c == -1) {            return -1;        }        b[off] = (byte)c;        int i = 1;        try {            for (; i < len ; i++) {                c = read();                if (c == -1) {                    break;                }                b[off + i] = (byte)c;            }        } catch (IOException ee) {        }        return i;    }        public abstract int read() throws IOException;

以下のように多くの実装クラスは、あります。

ここのするByteArrayInputStreamは、次のとおりです。

public synchronized int read() {        return (pos < count) ? (buf[pos++] & 0xff) : -1;    }

データソースはAbstractDataSourceInitializerを2.jdbc初期化するために使用され

@PostConstruct  protected void initialize() {    if (!isEnabled()) {      return;    }    ResourceDatabasePopulator populator = new ResourceDatabasePopulator();    String schemaLocation = getSchemaLocation();    if (schemaLocation.contains(PLATFORM_PLACEHOLDER)) {      String platform = getDatabaseName();      schemaLocation = schemaLocation.replace(PLATFORM_PLACEHOLDER, platform);    }    populator.addScript(this.resourceLoader.getResource(schemaLocation));    populator.setContinueOnError(true);    customize(populator);    DatabasePopulatorUtils.execute(populator, this.dataSource);  }    protected abstract DataSourceInitializationMode getMode();  protected abstract String getSchemaLocation();

実現BatchDataSourceInitializer

@Override  protected DataSourceInitializationMode getMode() {    return this.properties.getInitializeSchema();  }  @Override  protected String getSchemaLocation() {    return this.properties.getSchema();  }

テキスト出典ます。https://github.com/jinjunzhu/design-pattern.git

マイクロチャネル公共番号、歓迎の注意、一緒に成長することを学びます

公開された33元の記事 ウォンの賞賛2 ビュー40000 +

おすすめ

転載: blog.csdn.net/zjj2006/article/details/104968457