设计模式第二次作业
github地址
1、要求:如果需要开发一个跨平台视频播放器,可以在不同操作系统平台(如Windows、Linux、UNIX等)上播放多种格式的视频文件,常见的视频格式包括MPEG、RMVB、AVI、WMV等。使用Bridge模式来设计。
github地址
类图:
源码:
//视频文件接口
public interface Video {
}
public abstract class AbstractVideoPlayer {
private Video video;
public abstract void play();
public void setVideo(Video video) {
this.video = video;
}
public Video getVideo() {
return video;
}
}
public class MPEG implements Video {
}
public class RMVB implements Video {
}
public class WMV implements Video {
}
public class AVI implements Video {
}
public class LinuxVideoPlayer extends AbstractVideoPlayer {
@Override
public void play() {
System.out.println(getVideo().getClass().getSimpleName() + " is playing on Linux");
}
}
public class UnixVideoPlayer extends AbstractVideoPlayer {
@Override
public void play() {
System.out.println(getVideo().getClass().getSimpleName() + " is playing on Unix");
}
}
public class WindowVideoPlayer extends AbstractVideoPlayer {
@Override
public void play() {
System.out.println(getVideo().getClass().getSimpleName() + " is playing on Windows");
}
}
public class VideoPlayerTest {
private AbstractVideoPlayer videoPlayer;
@Before
public void init(){
videoPlayer = new WindowVideoPlayer();
videoPlayer.setVideo(new RMVB());
}
@Test
public void test(){
videoPlayer.play();
}
}
2、杀毒软件(AntiVirus)既能对系统中不同类型的文件 TextFile、ImageFile、VideoFile杀毒,也能对文件夹的杀毒,对文件夹杀毒就是对其中所包含文件的杀毒。使用Composite模式来设计
github地址
类图:
源码:
public interface AntiVirus {
void antiVirus();
}
public class ImageFileAntiVirus implements AntiVirus {
@Override
public void antiVirus() {
System.out.println("scan the image file");
}
}
public class TextFileAntiVirus implements AntiVirus {
@Override
public void antiVirus() {
System.out.println("scan the text file");
}
}
public class VideoFileAntiVirus implements AntiVirus {
@Override
public void antiVirus() {
System.out.println("scan the video file");
}
}
public class FoldFileAntiVirus implements AntiVirus {
private List<AntiVirus> antiVirusArrayList = new ArrayList<>();
@Override
public void antiVirus() {
System.out.println("scan the Fold file(");
for(AntiVirus tmp : antiVirusArrayList){
tmp.antiVirus();
}
System.out.print(")");
}
public void add(AntiVirus antiVirus){
antiVirusArrayList.add(antiVirus);
}
public void remove(AntiVirus antiVirus){
antiVirusArrayList.remove(antiVirus);
}
}
public class AntiVirusTest {
private AntiVirus antiVirus;
@Before
public void init(){
FoldFileAntiVirus antiVirus1 = new FoldFileAntiVirus();
antiVirus1.add(new ImageFileAntiVirus());
antiVirus1.add(new TextFileAntiVirus());
FoldFileAntiVirus antiVirus2 = new FoldFileAntiVirus();
antiVirus2.add(new VideoFileAntiVirus());
antiVirus2.add(antiVirus1);
antiVirus = antiVirus2;
}
@Test
public void test(){
antiVirus.antiVirus();
}
}
3、要求:某系统提供一个数据加密功能,可以对字符串进行加密。最简单的加密算法通过对字母进行移位来实现,同时还提供稍复杂的逆向输出加密,还提供更为高级的求模加密。用户首先使用最简单的加密算法对字符串进行加密,如果觉得还不够可以对加密后的结果使用其他的加密算法进行二次加密,当然也可以进行第三次加密。使用Decrator模式来设计。
github地址
类图:
源码:
public interface StringEncryptor {
void encrypt();
}
public class ShiftEncryptor implements StringEncryptor{
@Override
public void encrypt() {
System.out.print(" shift");
}
}
public class EncryptorDecrator implements StringEncryptor {
private StringEncryptor stringEncryptor;
public EncryptorDecrator(StringEncryptor stringEncryptor){
this.stringEncryptor = stringEncryptor;
}
@Override
public void encrypt() {
stringEncryptor.encrypt();
}
}
public class AdvanceEncryptor extends EncryptorDecrator{
public AdvanceEncryptor(StringEncryptor stringEncryptor) {
super(stringEncryptor);
}
@Override
public void encrypt() {
super.encrypt();
reverse();
}
public void reverse(){
System.out.print(" | reverse");
}
}
public class ComplexEncryptor extends EncryptorDecrator{
public ComplexEncryptor(StringEncryptor stringEncryptor) {
super(stringEncryptor);
}
@Override
public void encrypt() {
super.encrypt();
mod();
}
public void mod(){
System.out.print(" | mod");
}
}
public class StringEncryptorTest {
private StringEncryptor stringEncryptor;
@Before
public void init(){
AdvanceEncryptor advanceEncryptor = new AdvanceEncryptor(new ShiftEncryptor());
ComplexEncryptor complexEncryptor = new ComplexEncryptor(advanceEncryptor);
stringEncryptor = complexEncryptor;
}
@Test
public void test(){
stringEncryptor.encrypt();
}
}
4、要求:某系统需要提供一个文件加密模块,加密流程包括:读源文件、加密、保存加密文件。读取文件和保存文件使用流来实现,三个业务相对独立,封装在不同的类中;现在需要提供一个统一的加密外观类,用户可以直接使用该加密外观类完成文件的读取、加密和保存三个操作。使用Facade模式来设计。
github地址
类图:
源码:
public class FileReader {
public void read(){
System.out.println("read the file");
}
}
public class EncryptEngine {
public void encrypt(){
System.out.println("doEncrypt");
}
}
public class FileWriter {
public void writer(){
System.out.println("writer the file");
}
}
public class FileEncryptor {
private FileReader fileReader = new FileReader();
private FileWriter fileWriter = new FileWriter();
private EncryptEngine encryptEngine = new EncryptEngine();
public void encryptFile(){
fileReader.read();
encryptEngine.encrypt();
fileWriter.writer();
}
}
public class FileEncryptorTest {
private FileEncryptor fileEncryptor;
@Before
public void init(){
fileEncryptor = new FileEncryptor();
}
@Test
public void test(){
fileEncryptor.encryptFile();
}
}
5、某论坛已注册用户和游客的权限不同,已注册用户拥有发帖、修改自己的注册信息等功能;游客只能看别人的帖子,没有其他权限。使用Proxy模式来设计。
github地址
类图:
源码:
public interface User {
void read();
void post();
void modifyInfo();
}
public class DefaultUser implements User {
@Override
public void read() {
System.out.println("read some articles");
}
@Override
public void post() {
System.out.println("post an article");
}
@Override
public void modifyInfo() {
System.out.println("modify some personal information");
}
}
public class UserProxy implements User{
private User user;
private Role role;
public UserProxy(User user, Role role) {
this.user = user;
this.role = role;
}
@Override
public void read() {
user.read();
}
@Override
public void post() {
if(checkRole()){
user.post();
}
}
@Override
public void modifyInfo() {
if(checkRole()){
user.modifyInfo();
}
}
public boolean checkRole(){
boolean isMember = role.equals(Role.MEMBER);
if(!isMember){
System.out.println("operation forbidden !");
}
return isMember;
}
public enum Role{
MEMBER, GEUST
}
}
public class ForumTest {
private User user;
@Before
public void init(){
user = new UserProxy(new DefaultUser(), UserProxy.Role.GEUST);
}
@Test
public void test(){
user.read();
user.post();
user.modifyInfo();
}
}