责任链模式实例笔记

以一个采购系统为例, 申请审批, 根据金额,有不同的审批人(主任、院长、副校长、校长)

1.类图

2. 请求

一个数据类

data class PurchaseRequest (
    val owner:String,
    val id: Int,
    var price:Float
)

3. 抽象处理者

abstract class Approver {
    var approver: Approver? = null
    var name: String

    constructor(name:String){
        this.name = name
    }

    abstract fun processRequest(request: PurchaseRequest)
}

其中,两个属性,一个为审批人的姓名name,一个为后继者 approver(组合)

抽象方法为处理请求

4.具体处理者

import android.util.Log

class DepartmentApprover(name: String) : Approver(name) {

    override fun processRequest(request: PurchaseRequest) {
        if (request.price < 3000) {
            Log.d(
                "DepartmentApprover",
                "$name 审批了 ${request.owner}的申请, 申请号为 ${request.id}, 金额为 ${request.price}"
            )
        } else {
            approver?.processRequest(request)
        }
    }
}
import android.util.Log

class CollegeApprover(name: String) : Approver(name) {

    override fun processRequest(request: PurchaseRequest) {
        if (request.price >= 3000 && request.price < 5000) {
            Log.d(
                "CollegeApprover",
                "$name 审批了 ${request.owner}的申请, 申请号为 ${request.id}, 金额为 ${request.price}"
            )
        } else {
            approver?.processRequest(request)
        }
    }
}
import android.util.Log

class ViceSchoolMasterApprover(name: String) : Approver(name) {

    override fun processRequest(request: PurchaseRequest) {
        if (request.price >= 5000 && request.price < 10000) {
            Log.d(
                "ViceSchoolMasterApprover",
                "$name 审批了 ${request.owner}的申请, 申请号为 ${request.id}, 金额为 ${request.price}"
            )
        } else {
            approver?.processRequest(request)
        }
    }
}
import android.util.Log

class SchoolMasterApprover(name: String) : Approver(name) {

    override fun processRequest(request: PurchaseRequest) {
        if (request.price >= 10000) {
            Log.d(
                "SchoolMasterApprover",
                "$name 审批了 ${request.owner}的申请, 申请号为 ${request.id}, 金额为 ${request.price}"
            )
        } else {
            approver?.processRequest(request)
        }
    }
}

5. 客户端调用

    @Test
    fun testIterator(){
        Log.d(TAG, "====================Iterator====================")

        var purchaseRequest1 = PurchaseRequest("小明", 1, 2000F)
        var purchaseRequest2 = PurchaseRequest("小花", 2, 4000F)
        var purchaseRequest3 = PurchaseRequest("小华", 3, 8000F)
        var purchaseRequest4 = PurchaseRequest("小强", 4, 10000F)

        var departmentApprover = DepartmentApprover("王主任")
        var collegeApprover = CollegeApprover("刘院长")
        var viceSchoolMasterApprover = ViceSchoolMasterApprover("张副校长")
        var schoolMasterApprover = SchoolMasterApprover("孙校长")

        //设置后继者
        departmentApprover.approver = collegeApprover
        collegeApprover.approver = viceSchoolMasterApprover
        viceSchoolMasterApprover.approver = schoolMasterApprover
        schoolMasterApprover.approver = departmentApprover

        departmentApprover.processRequest(purchaseRequest1)
        departmentApprover.processRequest(purchaseRequest2)
        departmentApprover.processRequest(purchaseRequest3)
        departmentApprover.processRequest(purchaseRequest4)

        schoolMasterApprover.processRequest(purchaseRequest1)
        viceSchoolMasterApprover.processRequest(purchaseRequest2)
        collegeApprover.processRequest(purchaseRequest3)
        departmentApprover.processRequest(purchaseRequest4)

    }

6. 结果输出

11-05 09:43:08.697 DesignPattern:                ====================Iterator==================== 
11-05 09:43:08.698 DepartmentApprover:           王主任 审批了 小明的申请, 申请号为 1, 金额为 2000.0 
11-05 09:43:08.698 CollegeApprover:              刘院长 审批了 小花的申请, 申请号为 2, 金额为 4000.0 
11-05 09:43:08.698 ViceSchoolMasterApprover:     张副校长 审批了 小华的申请, 申请号为 3, 金额为 8000.0 
11-05 09:43:08.698 SchoolMasterApprover:         孙校长 审批了 小强的申请, 申请号为 4, 金额为 10000.0 
11-05 09:43:08.698 DepartmentApprover:           王主任 审批了 小明的申请, 申请号为 1, 金额为 2000.0 
11-05 09:43:08.698 CollegeApprover:              刘院长 审批了 小花的申请, 申请号为 2, 金额为 4000.0 
11-05 09:43:08.698 ViceSchoolMasterApprover:     张副校长 审批了 小华的申请, 申请号为 3, 金额为 8000.0 
11-05 09:43:08.698 SchoolMasterApprover:         孙校长 审批了 小强的申请, 申请号为 4, 金额为 10000.0 

可以看出会根据金额,由分发到对应的审批人审批

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值