您的位置:首页 > 其它

scala实现外观模式

2015-08-28 21:41 344 查看
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")


}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: