scalaは設計モードのテンプレート方法モードを実現する

16596 ワード

package com.linewell.modeldesgin.template

import com.linewell.modeldesgin.dao.AccessDAO

import scala.collection.mutable.ArrayBuffer

/**  *         * Created by ctao on 2015/9/2.  */ trait Account {

    /**  *       * @param account      * @param pass     * @return       */  def validate(account: String, pass: String): Boolean = {
        val params = ArrayBuffer[Any](account)
        val result = AccessDAO.checkUser(params)
        result.head.getOrElse("name", "null") == account && result.head.getOrElse("pass", "null") == pass
    }


    /**  *          */  def calculateInterest(): Unit

    /**  *       */  def display() = println("    ")

    /**  *       * @param account     * @param pass     */  def handle(account: String, pass: String): Unit = validate(account, pass) match {
        case true => calculateInterest()
            display()
        case false =>
            println("       ")
    }


}

/**  *       */ case object CurrentAccount extends Account {
    override def calculateInterest(): Unit = println("        ")
}

/**  *       */ case object SavingAccount extends Account {
    override def calculateInterest(): Unit = println("        ")
}


package com.linewell.modeldesgin.template

/**  *        * Created by ctao on 2015/9/2.  */ object Client extends App {
    val account1: Account = SavingAccount
    account1.handle("ct", "123")

    val account2: Account = SavingAccount
    account2.handle("ct", "1233")


    val account3: Account = CurrentAccount
    account3.handle("ct", "123")
}
package com.linewell.modeldesgin.template

/**  *          * Created by ctao on 2015/9/2.  */ sealed trait DataViewer {

    /**  *         */  def data(): Unit

    /**  *       */  private def convertData() = println("      XML  ")

    /**  *       */  private def displayData(): Unit = println(" XML      ")

    /**  *    xml    * @return       */  protected def isNotXmlData = true   /**  *       */  def process(): Unit = {
        data()

        /**  *     */  if (isNotXmlData) {
            convertData()
        }
        displayData()
    }

}

/**  *XML      */ case object XMLDataViewer extends DataViewer {
    override def data(): Unit = println(" XML       ")


     override def isNotXmlData = false }

/**  * JSON      */ case object JSONDataViewer extends DataViewer {
    override def data(): Unit = println(" JSON       ")
}

package com.linewell.modeldesgin.template

/**  *        * Created by ctao on 2015/9/2.  */ object ClientHook extends App {
    val dataView1 = XMLDataViewer
    val dataView2 = JSONDataViewer
    dataView1.process()
    dataView2.process()
}