zoukankan      html  css  js  c++  java
  • 设计模式第二次作业

    1、要求:如果需要开发一个跨平台视频播放器,可以在不同操作系统平台(如Windows、Linux、UNIX等)上播放多种格式的视频文件,常见的视频格式包括MPEG、RMVB、AVI、WMV等。使用Bridge模式来设计。

    public abstract class Platform {
        //定义一个抽象的产品对象,不知道具体是什么产品
        private Video video;
        //构造函数,由子类定义传递具体的产品进来
        public Platform(Video video){
            this.video = video;
        }
        public void play(){
            this.video.bePlayed();
        }
    }
    
    public class Windows extends Platform {
        public Windows(Video video){
            super(video);
        }
        public void play(){
            super.play();
            System.out.println("window播放..");
        }
    }
    
    public class Unix extends Platform {
        public Unix(Video video){
            super(video);
        }
        public void play(){
            super.play();
            System.out.println("Unix..");
        }
    }
    
    public class Linux extends Platform {
        public Linux(Video video){
            super(video);
        }
        public void play(){
            super.play();
            System.out.println("Linux..");
        }
    }
    
    public abstract class Video {
        public abstract void bePlayed();
    }
    
    public class AVI extends Video {
        public void bePlayed(){
            System.out.println("播放AVI格式的视频");
        }
    }
    
    public class MPEG extends Video {
        public void bePlayed(){
            System.out.println("播放MPEG格式的视频");
        }
    }
    
    public class RMVB extends Video {
        public void bePlayed(){
            System.out.println("播放RMVB格式的视频");
        }
    }
    
    public class WMV extends Video {
        public void bePlayed(){
            System.out.println("播放WMV格式的视频");
        }
    }
    
    //测试类
    public class Client {
        public static void main(String[] args){
            Windows windows = new Windows(new AVI());
             windows.play();
            Linux linux = new Linux(new MPEG());
            linux.play();
            Unix unix = new Unix(new WMV());
            unix.play();
        }
    }
    

    2、要求:杀毒软件(AntiVirus)既能对系统中不同类型的文件 TextFile、ImageFile、VideoFile杀毒,也能对文件夹的杀毒,对文件夹杀毒就是对其中所包含文件的杀毒。使用Composite模式来设计。

    public abstract class File {
        public abstract void killVirus();
    }
    
    public class ImageFile extends File {
        private String fileName;
        public void killVirus(){
            System.out.println("对"+fileName+"杀毒");
        }
        public ImageFile(String fileName){
            this.fileName = fileName;
        }
    }
    
    public class TextFile extends File{
        private String fileName;
        public void killVirus(){
            System.out.println("对"+fileName+"杀毒");
        }
        public TextFile(String fileName){
            this.fileName = fileName;
        }
    }
    
    public class VideoFile extends File {
        private String fileName;
        public void killVirus(){
            System.out.println("对"+fileName+"杀毒");
        }
        public VideoFile(String fileName){
            this.fileName = fileName;
        }
    }
    
    //测试类 
    import java.util.ArrayList;
    public class Folder extends File {
        private String fileName;
        private ArrayList<File> fileList = new ArrayList<File>();
        public void killVirus(){
            System.out.println("对文件夹"+fileName+"杀毒");
            for (Object obj:fileList)
                ((File)obj).killVirus();
        }
        public void add(File file){
            fileList.add(file);
        }
        public void remove(File file){
            fileList.remove(file);
        }
        public Folder(String fileName){
            this.fileName = fileName;
        }
        public File getChild(int n){
            return fileList.get(n);
        }
    }
    
    public class Client {
        public static void main(String[] args){
            File file1,file2,file3;
            Folder folder1,folder2;
            file1 = new ImageFile("图片文件");
            file2 = new TextFile("文本文件");
            file3 = new VideoFile("音频文件");
            folder1 = new Folder("总文件夹");
            folder2 = new Folder("子文件夹");
            folder2.add(file1);
            folder2.add(file2);
            folder1.add(file3);
            folder1.add(folder2);
            folder1.killVirus();
        }
    }
    

    3、要求:某系统提供一个数据加密功能,可以对字符串进行加密。最简单的加密算法通过对字母进行移位来实现,同时还提供稍复杂的逆向输出加密,还提供更为高级的求模加密。用户首先使用最简单的加密算法对字符串进行加密,如果觉得还不够可以对加密后的结果使用其他的加密算法进行二次加密,当然也可以进行第三次加密。使用Decrator模式来设计。

    public interface DataEncrypt {
        public void encrypt();
    }
    
    public class SfitEncrypt implements DataEncrypt {
        public SfitEncrypt(){
            System.out.println("字符串加密");
        }
    
        public void encrypt(){
            System.out.println("对字符串进行加密");
        }
    
    }
    
    public class AddEncrypt implements DataEncrypt{
        private DataEncrypt dataEncrypt;
        public AddEncrypt(DataEncrypt dataEncrypt){
            this.dataEncrypt = dataEncrypt;
        }
        public void encrypt(){
           dataEncrypt.encrypt();
        }
    }
    
    public class ReverseEncrypt extends AddEncrypt {
        public ReverseEncrypt(DataEncrypt dataEncrypt){
            super(dataEncrypt);
            System.out.println("进行第二次加密");
        }
        public void reverse(){
            System.out.println("逆向输出二次加密");
        }
    
    }
    
    public class ModEncrypt extends AddEncrypt{
        public ModEncrypt(DataEncrypt dataEncrypt){
            super(dataEncrypt);
            System.out.println("进行第三次加密");
        }
        public void mod(){
            System.out.println("求模第三次加密");
        }
    }
    
    public class Client {
        public static void main(String args[]){
            DataEncrypt sfit;
            sfit = new SfitEncrypt();
            sfit.encrypt();
            System.out.println("---------");
            ReverseEncrypt reverseEncrypt = new ReverseEncrypt(sfit);
            reverseEncrypt.reverse();
            System.out.println("---------");
            ModEncrypt modEncrypt = new ModEncrypt(reverseEncrypt);
            modEncrypt.mod();
        }
    
    }
    
    

    4、要求:某系统需要提供一个文件加密模块,加密流程包括:读源文件、加密、保存加密文件。读取文件和保存文件使用流来实现,三个业务相对独立,封装在不同的类中;现在需要提供一个统一的加密外观类,用户可以直接使用该加密外观类完成文件的读取、加密和保存三个操作。使用Facade模式来设计。

    public class ReadFile {
        public String Read(String fileName){
            System.out.println("读取"+fileName);
            return fileName;
        }
    }
    
    public class WriteFile {
        public void write(String fileName,String address){
            System.out.println("把"+fileName+"写入到"+address);
        }
    }
    
    public class CipherFile {
        public String cipher(String file){
            System.out.println("给"+file+"加密");
            return file;
        }
    }
    
    public class EncryptModule {
        private ReadFile readFile;
        private WriteFile writeFile;
        private CipherFile cipherFile;
        public EncryptModule(){
            readFile = new ReadFile();
            writeFile = new WriteFile();
            cipherFile = new CipherFile();
        }
        public void fileEncrypt(String fileName,String address){
            String filename = readFile.Read(fileName);
            String encrypt = cipherFile.cipher(fileName);
            writeFile.write(filename,address);
        }
    
    }
    
    //测试类
    public class Client {
        public static void main(String []args){
            String text = "汇报文件";
            String address = "word文档";
            EncryptModule et = new EncryptModule();
            et.fileEncrypt(text,address);
        }
    }
    

    5、要求:某论坛已注册用户和游客的权限不同,已注册用户拥有发帖、修改自己的注册信息等功能;游客只能看别人的帖子,没有其他权限。使用Proxy模式来设计。

    public interface AllUsers {
        //public void login(String user,String password);
        public void readNote();
        public void modifyNote();
        public void publishNote();
        public void modifyInfo();
    }
    
    public class EnrollUser implements AllUsers{
        private User user=null;
        public EnrollUser(User _user){
            this.user=_user;
        }
        public void readNote(){
            if (user.getLevel()==0){
                System.out.println("游客没有权限");
            }else if (user.getLevel()==1){
            System.out.println("用户浏览帖子");}
        }
        public void publishNote(){
            if (user.getLevel()==0){
                System.out.println("游客没有权限");
            }else if (user.getLevel()==1){
            System.out.println("用户发布新帖");}
        }
        public void modifyNote(){
            if (user.getLevel()==0){
                System.out.println("游客没有权限");
            }else if (user.getLevel()==1){
            System.out.println("用户修改帖子");}
        }
        public void modifyInfo(){
            if (user.getLevel()==0){
                System.out.println("游客没有权限");
            }else if (user.getLevel()==1){
            System.out.println("用户修改个人信息");}
        }
    }
    
    
    public class User implements AllUsers {
        private int level=0;
        public User(int level){
            this.level=level;
        }
        public void readNote(){
            System.out.println("浏览帖子");
        }
        public void publishNote(){
            System.out.println("发布新帖");
        }
        public void modifyNote(){
            System.out.println("修改帖子");
        }
        public void modifyInfo(){
            System.out.println("修改个人信息");
        }
        public int getLevel(){
             return level;
        }
    }
    
    
    //测试类
    public class Client {
        public static void main(String[] args){
            //目标对象
           User user = new User(1);
           //代理对象
          EnrollUser enrollUser = new EnrollUser(user);
          enrollUser.publishNote();
          enrollUser.readNote();
          enrollUser.modifyNote();
          enrollUser.modifyInfo();
        }
    }
    
    
    
  • 相关阅读:
    angular 三大核心函数
    mongodb 怎样检测 安装成功 以及mongodb的一些增删改查命令
    前端自动化工具 -- gulp https://angularjs.org/
    ECMAscript一些方法的使用
    HBuilder使用技巧
    angular 本地存储
    WPF控件开发(2) 自动完成(AutoComplete)-1
    orecle查询关键字段存在的存储过程或Job
    flash 反编译 + 重新发布
    javascript高级程序设计--简介
  • 原文地址:https://www.cnblogs.com/zjp17/p/7832454.html
Copyright © 2011-2022 走看看