scala实现设计模式之访问者模式
2015-09-02 21:05
295 查看
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) }
相关文章推荐
- JS 案例-添加节点
- php-fpm 启动参数及重要配置详解
- Container With Most Water —— Leetcode
- Apache httpd问题汇总
- SpringMVC简单入门(注解)<学习随笔>
- scala实现设计模式之模版方法模式
- 八大金刚齐上阵 微信硬件能解决商业化症结?
- 最长重复子串与最长重复子序列
- window上用cmd,把sqlite3的整个库按表导出到一个目录
- int string相互转换
- shell判断文件,目录是否存在或者具有权限
- [转]谈谈我对攻读计算机研究生的看法
- 正则表达式语法规则
- android__实践(短信发送器)
- android开发游记:android studio工程 转换为 eclipse工程
- HDU-2066 一个人的旅行
- HAProxy 配置说明 动静分离
- 方法的三个关键参数
- lintcode-有效回文字符串
- 回溯法求解迷宫问题