结构型设计模式实验

1.适配器模式实例之算法适配
        现有一个接口DataOperation定义了排序方法sort(int[])和查找方法search(int[],int),已知类QuickSortquickSort(int[])方法实现了快速排序算法,类BinarySearchbinarySearch(int[],int)方法实现了二分查找算法。现使用适配器模式设计一个系统,在不修改源代码的情况下将类QuickSort和类BinarySearch的方法适配到DataOperation接口中。绘制类图并编程实现。
2.桥接模式实例之跨平台视频播放器
        如果需要开发一个跨平台视频播放器,可以在不同操作系统平台(如 WindowsLinuxUnix等)上播放多种格式的视频文件,如MPEGRMVBAVIWMV等常见视频格式。现使用桥接模式设计该播放器。
3.组合模式实例之杀毒软件
       使用组合模式设计一个杀毒软件(AntiVirus)的框架,该软件既可以对某个文件夹(Folder)杀毒,也可以对某个指定的文件(File)进行杀毒,文件种类包括文本包括TextFile、图片文件ImageFile、视频文件VideoFile。绘制类图并编程模拟实现。
4.装饰模式实例之界面显示构件库
        某软件公司基于面向对象技术开发了一套图形界面显示构件库VisualComponent。在使用该库构建某图形界面时,用户要求为界面定制一些特效显示效果,如带滚动条的窗体或透明窗体等。现使用装饰模式设计该构件库,绘制类图并编程模拟实现。
5.外观模式实例之文件加密
        某系统需要提供一个文件加密模块,加密流程包括三个操作,分别是读取源文件、加密、保存加密之后的文件。读取文件和保存文件使用流来实现,这三个操作相对独立,其业务代码封装在三个不同的类中。现在需要提供一个统一的加密外观类,用户可以直接使用该加密外观类完成文件的读取、加密和保存三个操作,而不需要与每一个类交互,使用外观模式设计该加密模块,要求编程模拟实现。
6.代理模式实例之日志记录代理
        在某应用软件中需要记录业务方法的调用日志,在不修改现有业务类的基础上为每一个类提供一个日志记录代理类,在中输出日志,如在业务方法method()调用之前输出方法method()被调用,调用时间为2012-10-10 101010”,调用之后如果没有抛异常则输出方法方法method()调用成功,否则输出方法method()调用失败。在代理类中调用真实业务类的业务方法,使用代理模式设计该日志记录模块结构,绘制类图并编程模拟实现。

一.适配器模式

类图

 

代码:

public interface DataOperation { //目标类

public void sort(int sort[], int i, int j);

public int search(int search[], int n);

}

 

public class AlgotithmAdapter implements DataOperation{ //适配器类

private QuickSort quick;

private BinarySearch binary;

public AlgotithmAdapter(QuickSort quick) {

this.quick = quick;

}

public AlgotithmAdapter(BinarySearch binary) {

this.binary = binary;

}

public void sort(int sort[], int i, int j) {

quick.quickSort(sort, i, j);

}

 

public int search(int search[], int n) {

return binary.binarySearch(search, n);

}

}

 

public class QuickSort { //适配者类

//划分数组

int partion(int array[], int p, int r) {

int x = array[r];

int i = p - 1;//注意这点,把i设成负值,然后作为移动的标志

int j;

for (j = p; j < r; j++) {

if (array[j] <= x) {

i++;

int temp = array[j];

array[j] = array[i];

array[i] = temp;

}

}

int temp = array[j];

array[j] = array[i + 1];

array[i + 1] = temp;

return i+1;//返回的应该是交换后的哨兵的位置

}

//递归解决每个划分后的小数组

void quickSort(int[] array, int p, int r) {

if (p < r) {

int q = partion(array, p, r);

quickSort(array, p, q - 1);

quickSort(array, q + 1, r);

}

}

}

 

public class BinarySearch { //适配者类

   public int binarySearch(int[] srcArray, int des){   

      

        int low = 0;   

        int high = srcArray.length-1;   

        while(low <= high) {   

            int middle = (low + high)/2;   

            if(des == srcArray[middle]) {   

                return middle;   

            }else if(des <srcArray[middle]) {   

                high = middle - 1;   

            }else {   

                low = middle + 1;   

            }  

        }  

        return -1;  

   }  

}

 

public class Client { //客户端类

public static void main(String[] args) {

int[] array = { 4, 3, 5, 2, 3, 6, 8, 9, 18, 12, 53, 20};

int i;

System.out.print("排序前:");

for (i=0; i<array.length; i++)

System.out.print(array[i]+" ");

BinarySearch binary = new BinarySearch();

AlgotithmAdapter algotithm1 = new AlgotithmAdapter(binary);

        System.out.println("\n通过二分查找得到数字5:位于数组的第"+(algotithm1.search(array, 5)+1)+"");  

QuickSort sort = new QuickSort();

AlgotithmAdapter algotithm2 = new AlgotithmAdapter(sort);

algotithm2.sort(array, 0, array.length - 1);

System.out.println("------------------------------");

System.out.print("排序后:");

for (i=0; i<array.length; i++)

System.out.print(array[i]+" ");

//int[] src = new int[] {1, 3, 5, 7, 8, 9};   

        System.out.println("\n通过二分查找得到数字5:位于数组的第"+(algotithm1.search(array, 5)+1)+"");  

}

}

二、桥接模式

Uml

 

源代码

AVIFile
package Bridge;

public class AVIFile implements VideoFile{
  public void decode(String osType, String fileName) {  
  System.out.println("操作系统:"+osType+"文件名称:"+fileName);  
  }  

}

LinuxVersion
package Bridge;

public class LinuxVersion extends OperatingSystemVersion {  
  public void play(String fileName) {  
  String osType = "Linux播放";  
  this.vf.decode(osType,fileName);  
  }  

}

MPEGFile
package Bridge;

public class MPEGFile implements VideoFile{  
  public void decode(String osType, String fileName) {  
  System.out.println("操作系统:"+osType+"文件名称:"+fileName);  
  }  

}


OperatingSystemVersion
package Bridge;

public abstract class OperatingSystemVersion {
  protected VideoFile vf;  
  
  public void setVideo(VideoFile vf) {  
  this.vf = vf;  
  }  
  
  public abstract void play(String fileName);  

}

RMVBFile
package Bridge;

public class RMVBFile implements VideoFile {  
  public void decode(String osType, String fileName) {  
  System.out.println("操作系统:"+osType+"文件名称:"+fileName);  
  }  

}

Test
package Bridge;

public class Test {

  public static void main(String[] args) {
    // TODO Auto-generated method stub
  VideoFile vf;  
  OperatingSystemVersion osType1 = new WindowVersion();  
  vf = new AVIFile();  
  osType1.setVideo(vf);  
  osType1.play("AVI");  
      
  OperatingSystemVersion osType2 = new LinuxVersion();  
  vf = new RMVBFile();  
  osType2.setVideo(vf);  
  osType2.play("RMVB");  
      
  OperatingSystemVersion osType3 = new UnixVersion();  
  vf = new MPEGFile();  
  osType3.setVideo(vf);  
  osType3.play("MPEG");

  }

}

UnixVersion
package Bridge;

public class UnixVersion extends OperatingSystemVersion {  
  public void play(String fileName) {  
  String osType = "Unix播放";  
  this.vf.decode(osType,fileName);  
  }  

}

VideoFile
package Bridge;

public interface VideoFile {  
  public void decode(String osType, String fileName);

}

WindowVersion
package Bridge;

public class WindowVersion extends OperatingSystemVersion {  
  public void play(String fileName) {  
  String osType = "Windows播放";  
  this.vf.decode(osType,fileName);  
  }  

}

WMVFile
package Bridge;

public class WMVFile implements VideoFile {  
  public void decode(String osType, String fileName) {  
  System.out.println("操作系统:"+osType+"文件名称:"+fileName);  
  }  

}

四、外观模式

Uml

 

代码:

 

(1)Client类:

 

public class Client {

 

public static void main(String[] args) {

try

{

String text="czy";

String address="c:设计模式";

EncryptFacade ec=new EncryptFacade();

ec.fileEncrypt(text,address);

}

catch(Exception e)

{

System.out.println(e.getMessage());

}

}

 

}

 

(2)EncryptFacade类:

 

public class EncryptFacade {

private FileReader reader;

private CipherMachine cipher;

private FileWriter writer;

public EncryptFacade()

{

reader=new FileReader();

cipher=new CipherMachine();

writer=new FileWriter();

}

public void fileEncrypt(String fileNameSrc,String fileNameDes)

{

String plainStr=reader.read(fileNameSrc);

String encryptStr=cipher.encrypt(plainStr);

writer.write(encryptStr,fileNameDes);

}

 

}

 

(3)FileReader类:

 

public class FileReader {

public String read(String fileNameSrc)

{

String fns=fileNameSrc;

System.out.println("原文件:"+fns);

return fns;

}

}

 

(4)FileWriter类:

 

public class FileWriter {

  

 public void write(String EncryText,String FileNameDes)

 {

 String et=EncryText;

 String fnd=FileNameDes;

 System.out.println("加密后文件:"+et);

 System.out.println("目标文件存放路径为:"+fnd);

 }

}

 

(5)CipherMachine类:

 

public class CipherMachine {

public String encrypt(String plainText)

{

String str="";

for (int i=0;i<plainText.length();i++)

{

char c= plainText.charAt(i);

if(c>='a'&&c<='z')

{

c+=6;

if(c>'z')c-=26;

if(c<'a')c+=26;

}

if(c>='A'&&c<='Z')

{

c+=6;

if(c>'Z')c-=26;

if(c<'A')c+=26;

}

str+=c;

}

return str;

}

 

}

五、代理模式

uml

 

 

  

源代码:

Log
package proxy;

public interface Log {
public void method();
}

LogProxy
package proxy;
import java.text.SimpleDateFormat;
import java.util.Date;

public class LogProxy implements Log{
  private LogRecord logRecord=new LogRecord();
  private SimpleDateFormat SDF=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  private String time=SDF.format(new Date());
  public void method() {
    System.out.println("方法method被调用!调用时间为:"+time);
    try{
    logRecord.method();    
    System.out.println("方法method()调用成功!");
    }catch(Exception e){
    System.out.println("方法method()调用失败!");
    }
    
    }

}

LogRecord
package proxy;

public class LogRecord implements Log{
  public void method() {
  System.out.println("真实业务方法被调用!");
  }

}

Test
package proxy;

public class Test {
  public static void main(String[] args) {
    LogProxy p= new LogProxy();
  p.method();
  }

}

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值