模板方法设计模式

(1)Java的写法

首先新建一个模板,假如这套模板适用于任何软件开发流程

/**
 * 创建一个模板
 * 就以生产软件为场景(方法名和类名也简单化了)
 */
public abstract class MuBanFF {

    //需求分析
    protected abstract void buildA();
    //概要设计
    protected abstract void buildB();
    //详细设计
    protected abstract void buildC();
    //编码
    protected abstract void buildD();
    //测试
    protected abstract void buildE();
    //软件交付
    protected abstract void buildF();
    //验收
    protected abstract void buildG();
    //维护
    protected abstract void buildH();

    /**
     * 执行
     */
    public final void build(){
        buildA();
        buildB();
        buildC();
        buildD();
        buildE();
        buildF();
        buildG();
        buildH();
    }
}

QQ产品:

/**
 * QQ
 */
public class QQSoftWare extends MuBanFF{
    @Override
    protected void buildA() {

    }

    @Override
    protected void buildB() {

    }

    @Override
    protected void buildC() {

    }

    @Override
    protected void buildD() {

    }

    @Override
    protected void buildE() {

    }

    @Override
    protected void buildF() {

    }

    @Override
    protected void buildG() {

    }

    @Override
    protected void buildH() {

    }
}

微信产品:

/**
 * 微信
 */
public class WeiXinSoftWare extends MuBanFF {
    @Override
    protected void buildA() {

    }

    @Override
    protected void buildB() {

    }

    @Override
    protected void buildC() {

    }

    @Override
    protected void buildD() {

    }

    @Override
    protected void buildE() {

    }

    @Override
    protected void buildF() {

    }

    @Override
    protected void buildG() {

    }

    @Override
    protected void buildH() {

    }
}

淘宝产品:

/**
 * 淘宝
 */
public class TaoBaoSoftWare extends MuBanFF {
    @Override
    protected void buildA() {

    }

    @Override
    protected void buildB() {

    }

    @Override
    protected void buildC() {

    }

    @Override
    protected void buildD() {

    }

    @Override
    protected void buildE() {

    }

    @Override
    protected void buildF() {

    }

    @Override
    protected void buildG() {

    }

    @Override
    protected void buildH() {

    }
}

开始生产:

    MuBanFF muBanFF1 = new QQSoftWare();
    muBanFF1.build();

    MuBanFF muBanFF2 = new WeiXinSoftWare();
    muBanFF2.build();

    MuBanFF muBanFF3 = new TaoBaoSoftWare();
    muBanFF3.build();

(2)结合Android

一个Android应用需要创建很多Activity, 然而Activity有很多共同之处,这个时候我们可以写一个基类来简化Activity处理流程,这个基类我们就采用模板方法设计模式。
写基类之前,我们需要搞清楚Activity需要做什么事?
1.设置基础布局;
2.初始化公共导航栏(可以写在基础布局中);
3.初始化基类数据
4.初始化控件
5.初始化数据
6.初始化监听
7.Activity跳转
8.点击事件

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ViewFlipper;

/**
 * 基类(模板方法设计模式)
 * Created by Q on 2018/3/1.
 */

public abstract class BaseActivity extends AppCompatActivity implements View.OnClickListener{

    protected Toolbar toolbar;
    private ViewFlipper mContentView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //初始化布局
        setContentView(R.layout.layout_base);
        mContentView = (ViewFlipper) findViewById(R.id.layout_container);
        toolbar = (Toolbar) findViewById(R.id.base_tool_bar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);

        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, 1);
        mContentView.addView(getRootView(), lp);

        //初始化基类数据
        initBaseData();
        //初始化控件
        initView();
        //初始化数据
        initData();
        //初始化监听
        initListener();
    }

    /**初始化基类数据*/
    private void initBaseData(){}{

    }
    /**初始化布局*/
    protected abstract View getRootView();
    /**初始化控件*/
    protected abstract void initView();
    /**初始化数据*/
    protected abstract void initData();
    /**初始化点击事件*/
    protected abstract void initListener();

    @Override
    public void onClick(View view) {

    }

    /**
     * 启动Activity
     * @param clazz
     */
    protected void startActivity(Class clazz){
        Intent in = new Intent(this,clazz);
        startActivity(in);
    }
}

你可能感兴趣的:(模板方法设计模式)