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")


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值