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

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

    类图:

    public abstract class VideoFile
    {
    	public abstract void decode();
    }
    
    public abstract class MPEG extends VideoFile
    {
    
    	@Override
    	public void decode()
    	{
    
    	}
    }
    
    public abstract class AVI extends VideoFile
    {
    
    	@Override
    	public void decode()
    	{
    
    	}
    }
    
    public abstract class WMV extends VideoFile
    {
    
    	@Override
    	public void decode()
    	{
    
    	}
    }
    
    public abstract class RMVB extends VideoFile
    {
    
    	@Override
    	public void decode()
    	{
    
    	}
    }
    
    
    
    
    
    public abstract class VideoPlayer
    {
    	protected VideoFile videoFile;
    	public abstract void play();
    	public void setVideoFile(VideoFile videoFile)
    	{
    		this->videoFile = videoFile;
    	}
    
    }
    
    public abstract class WindowVideoPlayer extends VideoPlayer
    {
    	@Override
    	public void play()
    	{
    		videoFile.decode();
    		
    	}
    }
    
    public abstract class LinuxVideoPlayer extends VideoPlayer
    {
    	@Override
    	public void play()
    	{
    		videoFile.decode();
    		
    	}
    }
    
    public abstract class UnixVideoPlayer extends VideoPlayer
    {
    	@Override
    	public void play()
    	{
    		videoFile.decode();
    
    	}
    }
    
    
    public class Client
    {
    	public public static void main(String[] args) 
    	{
    		VideoPlayer videoPlayer = new WindowVideoPlayer();
    		videoFile videoFile = new RMVB();
    
    		videoPlayer.setVideoFile(videoFile);
    		videoPlayer.play();
    	}
    }
    

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

    类图:

    public abstract class FileAntiVirus
    {
    	public abstract boolean AntiVirus();
    }
    
    
    public class TextFileAntiVirus extends FileAntiVirus
    {
    	@Override
    	public boolean AntiVirus()
    	{
    
    	}
    }
    
    public class ImageFileAntiVirus extends FileAntiVirus
    {
    	@Override
    	public boolean AntiVirus()
    	{
    		
    	}
    }
    
    public class VideoFileAntiVirus extends FileAntiVirus
    {
    	@Override
    	public boolean AntiVirus()
    	{
    		
    	}
    }
    
    public class DirFileAntiVirus extends FileAntiVirus
    {
    	private List<FileAntiVirus> antiVirusArrayList = new ArrayList<>();
    
    	@Override
    	public boolean AntiVirus()
    	{
    		
    	}
    
    	public boolean add()
    	{
    
    	}
    
    	public boolean del()
    	{
    
    	}
    
    }
    

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

    类图:

    public abstract class Encryption
    {
    	public abstract boolean shiftEncrypt(String str);
    	public abstract boolean reverseEncrypt(String str);
    	public abstract boolean advancedEncrypt(String str);
    }
    
    public class Shift
    {
    	@Override
    	public String shiftEncrypt(String str)
    	{
    
    	}
    }
    
    public class Reverse
    {
    	@Override
    	public String shiftEncrypt(String str)
    	{
    		
    	}
    }
    
    public class Advanced
    {
    	@Override
    	public String shiftEncrypt(String str)
    	{
    		
    	}
    }
    
    
    
    public class EncryptorDecrator
    {
    	private Encryption encryptor;
    	private String encryptedStr;
    
    	public boolean encrypt(int NO,String str)
    	{
    		if(NO == 0)
    			encryptedStr = encryptor.shiftEncrypt(str);
    		else if(NO == 1)
    			encryptedStr = encryptor.reverseEncrypt(str);
    		else
    			encryptedStr = encryptor.advancedEncrypt(str);
    	}
    
    	public String getEncryptedStr()
    	{
    		return encryptedStr;
    	}
    }
    
    public class Client
    {
    	public static void main(String[] args) 
    	{
    		String str = "123456";
    		EncryptorDecrator dector;
    		dector.encrypt(1,str);
    		String str2 = dector.getEncryptedStr();
    	}
    }
    
    
    

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

    类图:

    public class Stream
    {
    	public boolean readStream(File file)
    	{
    
    	}
    
    	public boolean writeStream(File file)
    	{
    
    	}
    
    }
    
    
    public class Write
    {
    	public boolean write(File file)
    	{
    		new Stream().writeStream(file);
    	}
    }
    
    public class Read
    {
    	public File read(File file)
    	{
    		new Stream().readStream(file);
    	}
    }
    
    public class Encryptor
    {
    	public File encrypt(File file)
    	{
    
    	}
    }
    
    public class FileEncryptor
    {
    	private File encrypted_file;
    	private File read_file;
    
    	public boolean fileWrite(File file)
    	{
    		return new Write().write(file);
    	}
    
    	public boolean fileRead(File file)
    	{
    		read_file = new Read().read(file);
    	}
    
    	public boolean fileEncrypt(File file)
    	{
    		encrypted_file = new Encryptor().encrypt(file);
    	}
    
    	public File getEncryptedFile()
    	{
    		return encrypted_file;
    	}
    
    	public File getReadFile()
    	{
    		return read_file;
    	}
    }
    
    
    public class Client
    {
    	public static void main(String[] args) 
    	{
    		File file,file2;
    		FileEncryptor file_encryptor;
    		file_encryptor = new FileEncryptor().fileEncrypt(file);	
    		file2 = file_encryptor.getEncryptedFile();
    	}
    }
    

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

    类图:

    public interface class Man
    {
    	public abstract void view();
    }
    
    
    
    public class User implements Man
    {
    	public void post()
    	{
    
    	}
    
    	public void modifyInformation()
    	{
    
    	}
    
    	@Override
    	public void view()
    	{
    
    	}
    }
    
    
    public class Visitor implements Man
    {
    	@Override
    	public void view()
    	{
    		new User().view();
    	}
    }
    
    
    
    public class Client
    {
    	public static void main(String[] args) 
    	{
    			Man man = new Vistor();
    			vistor.view();
    	}
    }
    
  • 相关阅读:
    洛谷P1056_排座椅 贪心+桶排思想
    NOIP普及组相关
    洛谷P1464_Function 记忆化搜索
    Xcode的使用技巧
    MAC_XCODE清理
    输入框跟随键盘移动效果的实现
    #pragma的进阶用法
    iOS 逆向
    警告框
    UIImageView设置圆角的方式
  • 原文地址:https://www.cnblogs.com/maskerk/p/7825405.html
Copyright © 2011-2022 走看看