scala設計モードの外観モードを実現

9185 ワード

package com.linewell.modeldesgin.pattern

import java.io.{FileNotFoundException, IOException}

import scala.io.Source

/**
 *  * Created by ctao on 2015/8/28.
 */
class FileReader {

    def read(fileNameSrc: String): String = {
        println("    ,    :")
        /**
         *     
         */
        var target = ""
        try {
            for(s<- Source.fromFile(fileNameSrc)){
                target += s.toString
            }
        } catch {
            case io: IOException => io.printStackTrace()
            case noFile: FileNotFoundException => noFile.printStackTrace()
        }
        target

    }
}
package com.linewell.modeldesgin.pattern

/**
 *  * Created by ctao on 2015/8/28.
 */
class CipherMachine {
    def encrypt(plainText: String): String = {
        println("    ,        :")
        var es = ""
        for (i <- 0 until plainText.length) {
            es += String.valueOf(plainText.charAt(i) % 7)
        }
        print(es)
        es
    }
}
package com.linewell.modeldesgin.pattern

import java.io.{FileNotFoundException, IOException, PrintWriter}

/**
 *  * Created by ctao on 2015/8/28.
 */
class FileWriter {
    def write(encryptStr: String, fileNameDes: String): Unit = {
        print("    ,    :")
        try {
            val out = new PrintWriter(fileNameDes)
            out.print(encryptStr)
            out.close()
        } catch {
            case io: IOException => io.printStackTrace()
            case noFile: FileNotFoundException => noFile.printStackTrace()
            case _ => println("    ")
        }
    }
}
package com.linewell.modeldesgin.pattern

/**
 *     
 * Created by ctao on 2015/8/28.
 */

class EncryptFacade {
    private val fileReader = new FileReader
    private val cipherMachine = new CipherMachine
    private val fileWriter = new FileWriter

    def fileEncrypt(fileNameSrc: String, fileNameDes: String): Unit = {
        fileWriter.write(cipherMachine.encrypt(fileReader.read(fileNameSrc)), fileNameDes)
    }
}






package com.linewell.modeldesgin.pattern

/**
 *      
 * Created by ctao on 2015/8/28.
 */
object Client extends App {
    val encryptFacade = new EncryptFacade
    encryptFacade.fileEncrypt("hello", "des")


}