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")
}
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实现设计模式之外观模式
- 大话设计模式--外观模式 Facade -- C++实现实例
- 数据库连接池之外观模式+单例模式的实现
- scala实现设计模式之建造者模式
- 设计模式C++实现:包装器外观模式(Wrapper Facade)
- C++设计模式实现--外观(Facade)模式
- 设计模式C++实现(7)——外观模式、组合模式
- 设计模式---外观模式(C++实现)
- java语言实现结构型设计模式—外观模式
- scala实现设计模式之享元模式
- scala实现命令模式
- 设计模式----外观模式UML和实现代码(5个必须掌握的设计模式)
- 大话设计模式--外观模式 Facade -- C++实现实例
- 三层架构结合外观模式实现登录
- scala实现设计模式之单例设计模式
- scala实现备忘录模式
- 大话设计模式之外观模式(Java版实现) 买股票还是买基金在此揭晓
- Head First设计模式C++实现--第七章:适配器(Adapter)与外观(Facade)模式
- 外观模式(一):外观模式概述,外观模式结构与实现
- 设计模式C++实现(4)——外观模式、组合模式