学校包含若干个学院, 学院又包含若干个专业, 其中就是组合关系(树形结构)。直接上代码
1. 抽象基类
import java.lang.UnsupportedOperationException
abstract class AbstractOrganizationComponent {
protected var name: String
protected var description: String
constructor(name: String, description: String) {
this.name = name
this.description = description
}
open fun addChild(component: AbstractOrganizationComponent) {
throw UnsupportedOperationException()
}
open fun removeChild(component: AbstractOrganizationComponent) {
throw UnsupportedOperationException()
}
abstract fun printIntroduce()
}
2. 专业 (叶子节点)
import android.util.Log
class Department(name: String, description: String) :
AbstractOrganizationComponent(name, description) {
override fun printIntroduce() {
//println("${super.name} : ${super.description}")
Log.d("Department", "$name : $description")
}
}
3. 学院(组合节点)
import android.util.Log
class College(name: String, description: String) :
AbstractOrganizationComponent(name, description) {
private var departmentsList = mutableListOf<AbstractOrganizationComponent>()
override fun addChild(component: AbstractOrganizationComponent){
departmentsList.add(component)
}
override fun removeChild(component: AbstractOrganizationComponent) {
departmentsList.remove(component)
}
override fun printIntroduce() {
//println("========================${super.name}, ${super.description} ===========")
Log.d("College", "========================$name, $description ===========")
for (i in departmentsList.indices) {
departmentsList[i].printIntroduce()
}
}
}
4. 学校(组合节点)
import android.util.Log
class University(name: String, description: String) :
AbstractOrganizationComponent(name, description) {
private var collegesList = mutableListOf<AbstractOrganizationComponent>()
override fun addChild(component: AbstractOrganizationComponent){
collegesList.add(component)
}
override fun removeChild(component: AbstractOrganizationComponent) {
collegesList.remove(component)
}
override fun printIntroduce() {
//println("========================$name, $description ===========")
Log.d("University", "========================$name, $description ===========")
for (i in collegesList.indices) {
collegesList[i].printIntroduce()
}
}
}
5.测试代码
// *********************** Composite design pattern ****************************
@Test
fun testComposite() {
var softWareDepartment = Department("软件工程专业", "这个专业就业前景很好")
var networkDepartment = Department("网络工程专业", "这个专业也不错")
var computerCollege = College("计算机学院", "这是个一流学院")
computerCollege.addChild(softWareDepartment)
computerCollege.addChild(networkDepartment)
var communicationDepartment = Department("通信工程专业", "这个专业有点难学")
var electronicDepartment = Department("电子工程专业", "这个专业容易一点")
var infoCollege = College("信息工程学院", "这是个技术性很强的学院")
infoCollege.addChild(communicationDepartment)
infoCollege.addChild(electronicDepartment)
var qinghuaUniversity = University("清华大学", "这是一所顶级大学")
qinghuaUniversity.addChild(computerCollege)
qinghuaUniversity.addChild(infoCollege)
qinghuaUniversity.printIntroduce()
}
// *********************** Composite design pattern ****************************
6. 运行结果
09-15 21:59:59.566 D University: ========================清华大学, 这是一所顶级大学 ===========
09-15 21:59:59.566 D College : ========================计算机学院, 这是个一流学院 ===========
09-15 21:59:59.567 D Department: 软件工程专业 : 这个专业就业前景很好
09-15 21:59:59.567 D Department: 网络工程专业 : 这个专业也不错
09-15 21:59:59.567 D College : ========================信息工程学院, 这是个技术性很强的学院 ===========
09-15 21:59:59.567 D Department: 通信工程专业 : 这个专业有点难学
09-15 21:59:59.567 D Department: 电子工程专业 : 这个专业容易一点