类行为型模式——模板方法(TemplateMethod)

版权声明:本文为博主原创文章,但部分内容来源网络,如有侵权,告知即删! https://blog.csdn.net/chenyuan_jhon/article/details/78298298
  • 定义:
    模板方法模式定义了一个算法的步骤,并允许子类为一个或者多个步骤提供实现。
  • 概述
    本模式包含一个抽象类及多个实现类,一下分别介绍
    抽象类AbstractClass
    此类定义了算法的骨架,并确定了其执行顺序,一般是不允许改变的,其抽象方法需要子类去实现
    具体类(ConcreteClass)
    实现了抽象类的具体类,实现了算法骨架的某些方法。
    不得不说的是其钩子操作,可以实现改变类行为的功能(实例中有讲解)
  • 经典类图
    这里写图片描述

  • 实例(Kotlin语言)
    以下是抽象类

abstract class CaffeineBeverageWithHook {
    fun prepareRecipe(): Unit {
        boilWater()
        brew()
        pourInCup()
        if (customerWantsCondiments()) {
            addCondiments()
        }
    }

    abstract fun brew()
    abstract fun addCondiments()

    fun boilWater(): Unit {
        println("Boiling water")
    }

    fun pourInCup(): Unit {
        println("Pouring into cup ")
    }

    open fun customerWantsCondiments(): Boolean {
        return true
    }
}

以下是两个具体类

//有钩子行为
class CoffeeWithHook : CaffeineBeverageWithHook() {
    override fun brew() {
        println("Dripping Coffee through filter")
    }

    override fun addCondiments() {
        println("Adding Sugar and Milk")
    }

    override fun customerWantsCondiments(): Boolean {
        val userInput = getUserInput()
        return userInput.toLowerCase().startsWith("y")
    }

    private fun getUserInput(): String {
        var answer: String? = null
        println("Would you like mike and sugar with your coffee (y/n)?")
        val bufferedReader = BufferedReader(InputStreamReader(System.`in`))

        try {
            answer = bufferedReader.readLine()
        } catch(e: Exception) {
            println("IO error trying to read your answer")
        }

        if (answer ==null) {
            return "no"
        }

        return answer
    }
}
//无钩子行为
class TeaWithoutHook:CaffeineBeverageWithHook() {
    override fun brew() {
        println("Steeping the tea")
    }

    override fun addCondiments() {
        println("adding lemon")
    }
}

以下是测试类及测试结果

fun main(args: Array<String>) {

    //模板方法模式(template method)
    //茶 no hook
    val teaWithoutHook = TeaWithoutHook()
    println("Making tea...")
    teaWithoutHook.prepareRecipe()

    //咖啡 with hook
    val coffeeWithHook = CoffeeWithHook()
    println("Making coffee...")
    coffeeWithHook.prepareRecipe()
}

//测试结果

//选择yes 
Making tea...
Boiling water
Steeping the tea
Pouring into cup 
adding lemon
Making coffee...
Boiling water
Dripping Coffee through filter
Pouring into cup 
Would you like mike and sugar with your coffee (y/n)?
y
Adding Sugar and Milk

Process finished with exit code 0

//选择no
Making tea...
Boiling water
Steeping the tea
Pouring into cup 
adding lemon
Making coffee...
Boiling water
Dripping Coffee through filter
Pouring into cup 
Would you like mike and sugar with your coffee (y/n)?
no

Process finished with exit code 0

//选择no 时不会执行addCondiments()方法
  • 本例类图
    这里写图片描述

如有错误,请留言更正,或进580725421群讨论,以免误导其他开发者!!!

猜你喜欢

转载自blog.csdn.net/chenyuan_jhon/article/details/78298298