0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

『増補改訂版Java言語で学ぶデザインパターン入門』をKotlinでやってみる(Template Method編)

Last updated at Posted at 2018-08-24

【経緯】

『増補改訂版Java言語で学ぶデザインパターン入門』をKotlinでやってみるの第三回目。
今回はTemplate Methodパターンです。

【コード】

Template Methodパターンは割とイメージしやすいですね。言葉のまんまですし。
スーパークラスで処理のテンプレートとなる枠組みを定義して、サブクラスでその具体的な内容を実装するデザインパターンです。
スーパークラス側で枠組みを定義するので、サブクラスから意識しなくてもロジックの共通化を行えるのが大きな利点かなと思います。

AbstractDisplay.kt
abstract class AbstractDisplay {
    // 抽象クラスメソッド
    abstract fun open()
    abstract fun print()
    abstract fun close()

    final fun display() {
        open()
        for (i in 0..4) {
            print()
        }
        close()
    }
}
CharDisplay.kt
abstract class AbstractDisplay {
class CharDisplay(char:String) : AbstractDisplay() {
    private var ch:String = char

    override fun open() {
        Log.d("CharDisp", "<<")
    }

    override fun print() {
        Log.d("CharDisp", ch)
    }

    override fun close() {
        Log.d("CharDisp", ">>")
    }
}
StringDisplay.kt
class StringDisplay(string:String) : AbstractDisplay() {
    private var str:String = string
    private var width:Int = str.toByteArray().size

    override fun open() {
        printLine()
    }

    override fun print() {
        Log.d("StringDis", "|" + str + "|")
    }

    override fun close() {
        printLine()
    }

    fun printLine() {
        Log.d("StringDis", "+")
        for (i in 1..width) {
            Log.d("StringDis", "-")
        }
        Log.d("StringDis", "+")
    }
}
MainActivity.kt
class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        var chDisplay:AbstractDisplay = CharDisplay("H")
        var strDisplay:AbstractDisplay = StringDisplay("Hello, world.")
        var jStrDisplay:AbstractDisplay = StringDisplay("こんにちは。")

        chDisplay.display()
        strDisplay.display()
        jStrDisplay.display()
    }
}
MainActivity.kt
class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        var chDisplay:AbstractDisplay = CharDisplay("H")
        var strDisplay:AbstractDisplay = StringDisplay("Hello, world.")
        var jStrDisplay:AbstractDisplay = StringDisplay("こんにちは。")

        chDisplay.display()
        strDisplay.display()
        jStrDisplay.display()
    }
}

Android Studioのログで確認しているので実際は改行入ってますが、クッソ見難いので省いています。

出力結果
<<HHHHH>>

+-------------+
|Hello, world.|
|Hello, world.|
|Hello, world.|
|Hello, world.|
|Hello, world.|
+-------------+

+------------------+
|こんにちは。|
|こんにちは。|
|こんにちは。|
|こんにちは。|
|こんにちは。|
+------------------+

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?