scala实现设计模式之访问者模式

package com.linewell.modeldesgin.visitor

import scala.collection.mutable.ArrayBuffer

/**  * 访问者模式  * 员工特质:抽象元素类  * Created by ctao on 2015/9/2.  */ trait Employee {
    /**  * 接受一个抽象的访问者  * @param handler 抽象访问者  */  def accept(handler: Department)
}

/**  * 正式员工:具体元素类  * @param name 姓名  * @param weeklyWage 周薪  * @param workTime 工作时间  */ case class FullTimeEmployee(name: String, weeklyWage: Double, workTime: Int) extends Employee {
    override def accept(handler: Department): Unit = handler.visit(this)
}

/**  * 兼职员工:具体元素类  * @param name 姓名  * @param hourWage 时薪  * @param workTime 工作时间  */ case class PartTimeEmployee(name: String, hourWage: Double, workTime: Int) extends Employee {
    override def accept(handler: Department): Unit = handler.visit(this)
}

/**  * 部门特质,抽象访问者特质  */ trait Department {
    /**  * 访问正式员工  * @param employee 正式员工  */  def visit(employee: FullTimeEmployee): Unit

    /**  * 访问兼职员工  * @param employee 兼职员工  */  def visit(employee: PartTimeEmployee): Unit
}


/**  * 财务部,具体访问者类  */ class FADepartment extends Department {
    override def visit(employee: FullTimeEmployee): Unit = {
        var weekWage = employee.weeklyWage
        val workTime = employee.workTime
        workTime match {
            case x if x >= 40 => weekWage = weekWage + (workTime - 40) * 100
            case x if x < 40 => weekWage = weekWage - (40 - workTime) * 80
                if (weekWage < 0) {
                    weekWage = 0
                }
        }
        println(s"正式员工${employee.name}实际工资为${weekWage}")
    }

    override def visit(employee: PartTimeEmployee): Unit = {
        val workTime = employee.workTime
        println(s"兼职员工${employee.name}实际工资为${employee.hourWage * workTime}")

    }
}


/**  * 人力资源部,具体访问者  */ class HRDepartment extends Department {
    override def visit(employee: FullTimeEmployee): Unit = {
        val workTime = employee.workTime
        println(s"正式员工${employee.name}实际上班时间为${workTime}小时")
        workTime match {
            case x if x >= 40 => println(s"正式员工${employee.name}加班时间为${workTime - 40}")
            case x if x < 40 => println(s"正式员工${employee.name}请假时间为${40 - workTime}")
        }
    }

    override def visit(employee: PartTimeEmployee): Unit =
        println(s"兼职员工${employee.name}实际上班时间为${employee.workTime}")
}

/**  * 员工列表类,对象结构  */ class EmployeeArray {
    private var employees = new ArrayBuffer[Employee]()

    /**  * 添加员工  * @param employee 员工  */  def addEmployee(employee: Employee) = employee match {
        case e if employees.exists(e.eq(_)) => println("已经添加")
        case _ => employees += employee
    }

    /**  * 访问  * @param department 部门  */  def accept(department: Department) = employees.foreach(_.accept(department))
}

package com.linewell.modeldesgin.visitor

/**  * 测试客户端  * Created by ctao on 2015/9/2.  */ object Client extends App {
    /**  * 员工  */  val fte1: Employee = FullTimeEmployee("a", 500, 40)
    val fte2: Employee = FullTimeEmployee("b", 600, 38)
    val fte3: Employee = FullTimeEmployee("c", 550, 44)
    val fte4: Employee = PartTimeEmployee("d", 15, 33)
    val fte5: Employee = PartTimeEmployee("e", 17, 20)

    val employeeBuffer = new EmployeeArray
    /**  * 添加员工  */  employeeBuffer.addEmployee(fte1)
    employeeBuffer.addEmployee(fte1)
    employeeBuffer.addEmployee(fte2)
    employeeBuffer.addEmployee(fte3)
    employeeBuffer.addEmployee(fte4)
    employeeBuffer.addEmployee(fte5)

    /**  * 部门  */  val dep: Department = new HRDepartment
    /**  * 访问  */  employeeBuffer.accept(dep)
}

你可能感兴趣的:(scala实现设计模式之访问者模式)