java模式:模板模式的简单理解

1.模板模式就是用虚类作为基类将几个要执行差不多操作中相同的部分提取出来,不同的部分各自实现!

2.下面给出简单栗子:

  我要进行的操作是将大象和狐狸放入冰箱,放入大象和狐狸有相同的步骤:开冰箱和关冰箱,这个操作在基类中实现就好,而不同的在于具体操作部分:

    一,大象太胖了,要测量并切片才能放入冰箱

    二,狐狸太臭了,要洗干净并擦干

所以程序如下:

1.基类:BasicFridgeOperation.java

复制代码
package com.learn.templateMode;

/**
 * Created by garfield on 9/15/2016.
 */
public abstract class BasicFridgeOperation {

    private void openFridge(){
        System.out.println("open the fridge door");
    }


    /**
     * different parts about one of the whole steps
     * the subclass will make different implements
     */
    protected abstract void operateFridge();

    private void closeFridge(){
        System.out.println("close the fridge door");
    }

    /**
     * the same operation steps
     */
    public void operation(){
        openFridge();
        operateFridge();
        closeFridge();
    }

}
复制代码

2,放入大象类:OperateElephant.java

复制代码
package com.learn.templateMode;

/**
 * Created by garfield on 9/15/2016.
 */
public class OperateElephant extends BasicFridgeOperation {
    /**
     * same function but different implement
     */
    protected void operateFridge() {
        System.out.println("measure the elephant");
        System.out.println("slice up the elephant");
        System.out.println("put the elephant in");
    }

}
复制代码

3,放入狐狸类:OperateFox.java

复制代码
package com.learn.templateMode;

/**
 * Created by garfield on 9/15/2016.
 */
public class OperateFox extends BasicFridgeOperation {
    /**
     * same function but different implement
     */
    protected void operateFridge() {
        System.out.println("clean up the fox");
        System.out.println("dry the fox");
        System.out.println("put the fox in");
    }
}
复制代码

4,测试:OperationTest.java

复制代码
package com.learn.templateMode;

/**
 * Created by garfield on 9/15/2016.
 */
public class OperationTest {
    public static void main(String[] args) {
        System.out.println("====== begin to deal with the elephant=========");
        BasicFridgeOperation basicFridgeOperation = new OperateElephant();
        basicFridgeOperation.operation();
        System.out.println("====== begin to deal with the fox=========");
        BasicFridgeOperation basicFridgeOperation2 = new OperateFox();
        basicFridgeOperation2.operation();
    }
}
复制代码

5,输出结果:

复制代码
====== begin to deal with the elephant=========
open the fridge door
measure the elephant
slice up the elephant
put in the elephant
close the fridge door
====== begin to deal with the fox=========
open the fridge door
clean up the fox
dry the fox
put in the fox
close the fridge door
复制代码

,that,s all.

posted @   但行好事-莫问前程  阅读(531)  评论(0编辑  收藏  举报
编辑推荐:
· 一个超经典 WinForm,WPF 卡死问题的终极反思
· ASP.NET Core - 日志记录系统(二)
· .NET 依赖注入中的 Captive Dependency
· .NET Core 对象分配(Alloc)底层原理浅谈
· 聊一聊 C#异步 任务延续的三种底层玩法
阅读排行:
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(一):从.NET IoT入
· .NET 开发的分流抢票软件,不做广告、不收集隐私
· ASP.NET Core - 日志记录系统(二)
· 一个超经典 WinForm,WPF 卡死问题的终极反思
· 实现windows下简单的自动化窗口管理
点击右上角即可分享
微信分享提示