`
cjuexuan
  • 浏览: 13201 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

scala实现外观模式

 
阅读更多
package com.linewell.modeldesgin.facade

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.facade
/**
* 加密文件,子系统类
* 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.facade

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.facade

/**
*加密外观类
* 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.facade

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


}





分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics