Kotlin Design Pattern: Visitor

The visitor pattern is used to separate a relatively complex set of structured data classes from the functionality that may be performed upon the data that they hold.

Setup

  • Visitor:
    Declare a Visite operation for each class of ConcreteElement in the object structure. The operation's name and signature identifies the class that sends the Visit request to the visitor. That lets the visitor determine the concrete class of the element being visited. Then the visitor can access the element directly through particular interface.
  • Concrete Visitor
    Implement each operation declared by Visitor. Each operation implements a fragment of the algorithm defined for the corresponding class of object in the structure. ConcreteVisitor provides the context for the algorithm and stores the local state. This states often accumulates results during the traversal of the structure.
  • Element
    Define an accept operation which takes a visitor as argument.
  • Concrete Element
    Implement the operation that takes a visitor as argument.
  • Client

Example 1

// Step 5: Client code

fun main(args: Array<String>) {
    val oneVisitor = OneVisitor()
    val sightSeeing = SightSeeing()

    sightSeeing.accept(oneVisitor)
}

// Step 1: Visitable Interface (Element interface)

interface Visitable{
    fun accept(visitor: Visitor)
}

// Step 2: Concrete Visitable (Concrete Element)

class Palace: Visitable{
    override fun accept(visitor: Visitor) {
        visitor.visit(this)
    }
}

class Museum: Visitable{
    override fun accept(visitor: Visitor) {
        visitor.visit(this)
    }
}

class Monument: Visitable{
    override fun accept(visitor: Visitor) {
        visitor.visit(this)
    }
}

class SightSeeing: Visitable{
    private val visitable = arrayOf(Palace(), Museum(), Monument())
    override fun accept(visitor: Visitor) {
        for (v in visitable)
            v.accept(visitor)
    }
}

// Step 3: Visitor Interface

interface Visitor{
    fun visit(visitable: Visitable)
    fun visit(visitable: Palace)
    fun visit(visitable: Museum)
    fun visit(visitable: Monument)
}

// Step 4: Concrete Visitor

class OneVisitor: Visitor{
    override fun visit(visitable: Visitable) {
        println("I am visiting ${visitable.javaClass.simpleName}")
    }

    override fun visit(visitable: Monument) {
        println("I am visiting ${visitable.javaClass.simpleName}")
    }

    override fun visit(visitable: Museum) {
        println("I am visiting ${visitable.javaClass.simpleName}")
    }

    override fun visit(visitable: Palace) {
        println("I am visiting ${visitable.javaClass.simpleName}")
    }
}

/**
        prints

        I am visiting Palace
        I am visiting Museum
        I am visiting Monument
        
 **/

Example 2

// Step 5: Client code

fun main(args: Array<String>) {
    val customer = OneCustomer()

    val shopList = arrayOf(Toothpaste(2), Tissues(6), Lotion(1))

    shopList.forEach { it.accept(customer) }

    println("Sum = ${customer.sum}")
}

// Step 1: Visitable Interface

interface Commodity{
    fun accept(visitor: Customer)
}

// Step 2: Concrete Visitable

class Toothpaste(private val quantity: Int): Commodity{
    override fun accept(visitor: Customer) {
        println("Article: ${this.javaClass.simpleName}, Quantity: $quantity")
        visitor.visit(visitable = this, quantity = quantity)
    }
}

class Tissues(private val quantity: Int): Commodity{
    override fun accept(visitor: Customer) {
        println("Article: ${this.javaClass.simpleName}, Quantity: $quantity")
        visitor.visit(visitable = this, quantity = quantity)
    }
}

class Lotion(private val quantity: Int): Commodity{
    override fun accept(visitor: Customer) {
        println("Article: ${this.javaClass.simpleName}, Quantity: $quantity")
        visitor.visit(visitable = this, quantity = quantity)
    }
}

// Step 3: Visitor Interface

interface Customer{
    fun visit(visitable: Toothpaste, quantity: Int)
    fun visit(visitable: Tissues, quantity: Int)
    fun visit(visitable: Lotion, quantity: Int)
}

// Step 4: Concrete Visitor

class OneCustomer: Customer{
    var sum: Int = 0

    override fun visit(visitable: Lotion, quantity: Int) {
        sum += quantity * 30
    }

    override fun visit(visitable: Tissues, quantity: Int) {
        sum += quantity * 10
    }

    override fun visit(visitable: Toothpaste, quantity: Int) {
        sum += quantity * 15
    }
}

/**
        prints

        Article: Toothpaste, Quantity: 2
        Article: Tissues, Quantity: 6
        Article: Lotion, Quantity: 1
        Sum = 120

 **/
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子幔烛,更是在濱河造成了極大的恐慌枉侧,老刑警劉巖实苞,帶你破解...
    沈念sama閱讀 211,884評(píng)論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)阱当,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,347評(píng)論 3 385
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來糜工,“玉大人弊添,你說我怎么就攤上這事∑《罚” “怎么了表箭?”我有些...
    開封第一講書人閱讀 157,435評(píng)論 0 348
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)钮莲。 經(jīng)常有香客問我,道長(zhǎng)彼水,這世上最難降的妖魔是什么崔拥? 我笑而不...
    開封第一講書人閱讀 56,509評(píng)論 1 284
  • 正文 為了忘掉前任,我火速辦了婚禮凤覆,結(jié)果婚禮上链瓦,老公的妹妹穿的比我還像新娘。我一直安慰自己盯桦,他們只是感情好慈俯,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,611評(píng)論 6 386
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著拥峦,像睡著了一般贴膘。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上略号,一...
    開封第一講書人閱讀 49,837評(píng)論 1 290
  • 那天刑峡,我揣著相機(jī)與錄音洋闽,去河邊找鬼。 笑死突梦,一個(gè)胖子當(dāng)著我的面吹牛诫舅,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播宫患,決...
    沈念sama閱讀 38,987評(píng)論 3 408
  • 文/蒼蘭香墨 我猛地睜開眼刊懈,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了娃闲?” 一聲冷哼從身側(cè)響起俏讹,我...
    開封第一講書人閱讀 37,730評(píng)論 0 267
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎畜吊,沒想到半個(gè)月后泽疆,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,194評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡玲献,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,525評(píng)論 2 327
  • 正文 我和宋清朗相戀三年殉疼,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片捌年。...
    茶點(diǎn)故事閱讀 38,664評(píng)論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡瓢娜,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出礼预,到底是詐尸還是另有隱情眠砾,我是刑警寧澤,帶...
    沈念sama閱讀 34,334評(píng)論 4 330
  • 正文 年R本政府宣布托酸,位于F島的核電站褒颈,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏励堡。R本人自食惡果不足惜谷丸,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,944評(píng)論 3 313
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望应结。 院中可真熱鬧刨疼,春花似錦、人聲如沸鹅龄。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,764評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽扮休。三九已至迎卤,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間肛炮,已是汗流浹背止吐。 一陣腳步聲響...
    開封第一講書人閱讀 31,997評(píng)論 1 266
  • 我被黑心中介騙來泰國(guó)打工宝踪, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人碍扔。 一個(gè)月前我還...
    沈念sama閱讀 46,389評(píng)論 2 360
  • 正文 我出身青樓瘩燥,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親不同。 傳聞我的和親對(duì)象是個(gè)殘疾皇子厉膀,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,554評(píng)論 2 349

推薦閱讀更多精彩內(nèi)容

  • 今天跟gay蜜微信聊天時(shí)我在問他為什么要發(fā)文說“對(duì)不起,同性戀不上癮”二拐,因?yàn)槲沂遣煌馑^點(diǎn)的服鹅。在我看來,我們每個(gè)...
    叁肆伍閱讀 1,522評(píng)論 27 25
  • 之前報(bào)了研發(fā)本部的設(shè)計(jì)質(zhì)量部百新,今天晚上去面試了企软,一共要倆人,也一共有倆人面試饭望,所以成功率是100%吧仗哨,哈哈。 不過...
    青青子衿_5457閱讀 131評(píng)論 0 0
  • 如果我渡過了深邃的海洋 如果我翻越了高聳的山川 如果我穿梭人山人海只為相遇 如果我愿不顧一切追尋你的蹤跡 如果我再...
    萬一lxq閱讀 236評(píng)論 0 4