scala設計モデルを実現する仲介者モデル

37676 ワード

package com.linewell.modeldesgin.mediator

import scala.collection.mutable.ArrayBuffer

/**  *      ,     ,      * Created by ctao on 2015/9/1.  */ abstract class AbstractSoftware(val name: String) {
    private var persons = new ArrayBuffer[Person]()

    /**  *       * @param person     */  def addPerson(person: Person): Unit = {
        if (persons.forall(!person.eq(_))) {
            println(s" ${name}  ${person.name}")
            persons += person
        } else {
            println(s"  ${person.name}   ${name} ")
        }
    }

    /**  *       * @param person     */  def removePerson(person: Person): Unit = {
        if (persons.exists(person.eq(_))) {
            persons -= person
        } else {
            println("       ")
        }
    }

    /**  *       * @param person      * @param message     */  def notify(person: Person, message: String): Unit = {
        if (persons.exists(person.eq(_))) {
            persons.filter(!person.eq(_)).foreach(p => println(s"${p.name} ${person.name}     :$message"))
        } else {
            println(s"${person.name}       :$name")
        }

    }

    /**  *     * @param send      * @param receive      * @param message     */  def pm(send: Person, receive: Person, message: String): Unit = send match {
        case p if persons.exists(p.eq(_)) => receive match {
            case r if persons.exists(r.eq(_)) => println(s"${send.name}    :$message  ${receive.name}")
            case _ => println(s"   ${receive.name}          ")
        }
        case _ => println(s"   ${send.name}          ")
    }
}

/**  * qq * @param name     */ class QQSoftware(name: String) extends AbstractSoftware(name) {
    override def notify(person: Person, message: String): Unit = {
        println(s"   qq :$name")
        super.notify(person, message)
    }

    override def pm(send: Person, receive: Person, message: String): Unit = {
        println(s"  qq      ")
        super.pm(send, receive, message)
    }


}

/**  * msn * @param name     */ class MSNSoftware(name: String) extends AbstractSoftware(name) {
    override def notify(person: Person, message: String): Unit = {
        println(s"   msn :$name")
        super.notify(person, message)
    }

    override def pm(send: Person, receive: Person, message: String): Unit = {
        println(s"  msn      ")
        super.pm(send, receive, message)
    }


}


/**  *        * @param name     */ abstract class Person(val name: String) {
    /**  *       * @param software     */  def setAbstractSoftware(software: AbstractSoftware)

    /**  *     * @param message     */  def speak(message: String): Unit

    /**  *       * @param person     */  def remove(person: Person): Unit

    /**  *       * @param person     */  def add(person: Person): Unit

    /**  *     * @param person      * @param message     */  def privateChat(person: Person, message: String):Unit
}

/**  *  * @param name     */ class Admin(name: String) extends Person(name) {
    private var abstractSoftware: AbstractSoftware = null   def setAbstractSoftware(software: AbstractSoftware) = abstractSoftware = software

    override def speak(message: String) = abstractSoftware.notify(this, message)

    /**  *     * @param person     */  def remove(person: Person) = abstractSoftware.removePerson(person)

    /**  *     * @param person     */  def add(person: Person) = {
        println(s"${name}      ${person.name}   ")
        abstractSoftware.addPerson(person)
    }

    /**  *     * @param person      * @param message     */  def privateChat(person: Person, message: String) = abstractSoftware.pm(this, person, message)
}

/**  *  * @param name     */ class Member(name: String) extends Person(name) {
    private var abstractSoftware: AbstractSoftware = null   def setAbstractSoftware(software: AbstractSoftware) = abstractSoftware = software

    override def speak(message: String) = abstractSoftware.notify(this, message)

    /**  *       * @param person     */  override def add(person: Person): Unit = {
        println(s"${name}")

    }

    /**  *  * @param person     */  override def remove(person: Person): Unit = {
        if (person.eq(this)) {
            println(s"$name${abstractSoftware.name}")
            abstractSoftware.removePerson(person)
        } else {
            println(s"${name}")
        }
    }

    /**  *     * @param person      * @param message     */  def privateChat(person: Person, message: String) = abstractSoftware.pm(this, person, message)
}

package com.linewell.modeldesgin.mediator

/**  *        * Created by ctao on 2015/9/1.  */ object Client extends App {
    val admin: Person = new Admin("admin")
    val member1: Person = new Member("member1")
    val member2: Person = new Member("member2")
    val member3: Person = new Member("member3")
    val member4: Person = new Member("member4")

    val qqSoftware: AbstractSoftware = new QQSoftware("    ")
    admin.setAbstractSoftware(qqSoftware)
    member1.setAbstractSoftware(qqSoftware)
    member2.setAbstractSoftware(qqSoftware)
    member3.setAbstractSoftware(qqSoftware)
    member4.setAbstractSoftware(qqSoftware)

    admin.add(admin)
    admin.add(member1)
    admin.add(member2)
    admin.add(member3)
    admin.add(member4)
    admin.add(member1)

    admin.speak("hello")
    admin.remove(member1)

    member1.speak("hi")

    member2.add(member1)
    member2.remove(member2)

    member2.speak("admin")

    member3.privateChat(admin, "  ")
    member3.privateChat(member2, "  ")



    member2.privateChat(admin, "  ")

    println("------------------------------------------")

    val msnSoftware: AbstractSoftware = new MSNSoftware("     ")
    admin.setAbstractSoftware(msnSoftware)
    member1.setAbstractSoftware(msnSoftware)
    member2.setAbstractSoftware(msnSoftware)
    member3.setAbstractSoftware(msnSoftware)
    member4.setAbstractSoftware(msnSoftware)


    admin.add(admin)
    admin.add(member1)
    admin.add(member2)
    admin.add(member3)
    admin.add(member4)
    admin.add(member1)

    admin.speak("hello")
    admin.remove(member1)

    member1.speak("hi")

    member2.add(member1)

    member2.speak("admin")

    member2.privateChat(member3,"test")

}