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")
}
scala实现外观模式
猜你喜欢
转载自cjuexuan.iteye.com/blog/2238821
今日推荐
周排行