10.14

软件设计的实验

 

实验2:简单工厂模式

本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:

1、理解简单工厂模式的动机,掌握该模式的结构;

2、能够利用简单工厂模式解决实际问题。

 
   

 


[实验任务一]:女娲造人

使用简单工厂模式模拟女娲(Nvwa)造人(Person),如果传入参数M,则返回一个Man对象,如果传入参数W,则返回一个Woman对象,如果传入参数R,则返回一个Robot对象。请用程序设计实现上述场景。

实验要求:

1.  画出对应的类图;

 

2.  提交源代码

  1. // 定义一个Person接口
    interface Person {
        void display();
    }

    // 实现Person接口的Man
    class Man implements Person {
        @Override
        public void display() {
            System.out.println("I am a man.");
        }
    }

    // 实现Person接口的Woman
    class Woman implements Person {
        @Override
        public void display() {
            System.out.println("I am a woman.");
        }
    }

    // 实现Person接口的Robot
    class Robot implements Person {
        @Override
        public void display() {
            System.out.println("I am a robot.");
        }
    }

    // 简单工厂类
    class Nvwa {
        public Person createPerson(String type) {
            if (type == null) {
                return null;
            }
            if (type.equalsIgnoreCase("M")) {
                return new Man();
            } else if (type.equalsIgnoreCase("W")) {
                return new Woman();
            } else if (type.equalsIgnoreCase("R")) {
                return new Robot();
            } else {
                return null;
            }
        }
    }

    // 测试类
    public class SimpleFactoryPatternDemo {
        public static void main(String[] args) {
            Nvwa nvwa = new Nvwa();

            Person person = nvwa.createPerson("M");
            if (person != null) {
                person.display();
            }

            person = nvwa.createPerson("W");
            if (person != null) {
                person.display();
            }

            person = nvwa.createPerson("R");
            if (person != null) {
                person.display();
            }
        }
    }

 

3.注意编程规范。

软件设计                  石家庄铁道大学信息学院

 

实验3:工厂方法模式

本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:

1、理解工厂方法模式的动机,掌握该模式的结构;

2、能够利用工厂方法模式解决实际问题。

 
   

 


[实验任务一]:加密算法

目前常用的加密算法有DES(Data Encryption Standard)和IDEA(International Data Encryption Algorithm)国际数据加密算法等,请用工厂方法实现加密算法系统。

实验要求:

1.  画出对应的类图;

 

2.提交该系统的代码,该系统务必是一个可以能够直接使用的系统,查阅资料完成相应加密算法的实现;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.SecureRandom;
import java.util.Base64;

public class DESEncryption {

    // 生成DES密钥
   
public static SecretKey generateKey() throws Exception {
        KeyGenerator keyGenerator = KeyGenerator.getInstance("DES");
        keyGenerator.init(56, SecureRandom.getInstanceStrong());
        return keyGenerator.generateKey();
    }

    // DES加密
   
public static String encrypt(String data, SecretKey key) throws Exception {
        Cipher cipher = Cipher.getInstance("DES");
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] encryptedData = cipher.doFinal(data.getBytes("UTF-8"));
        return Base64.getEncoder().encodeToString(encryptedData);
    }

    // DES解密
   
public static String decrypt(String encryptedData, SecretKey key) throws Exception {
        Cipher cipher = Cipher.getInstance("DES");
        cipher.init(Cipher.DECRYPT_MODE, key);
        byte[] data = cipher.doFinal(Base64.getDecoder().decode(encryptedData));
        return new String(data);
    }

    public static void main(String[] args) {
        try {
            // 生成密钥
           
SecretKey key = generateKey();
            String originalText = "Hello, World!";

            // 加密
           
String encryptedText = encrypt(originalText, key);
            System.out.println("Encrypted Text: " + encryptedText);

            // 解密
           
String decryptedText = decrypt(encryptedText, key);
            System.out.println("Decrypted Text: " + decryptedText);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 

3.注意编程规范。

实验3:工厂方法模式

本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:

1、理解工厂方法模式的动机,掌握该模式的结构;

2、能够利用工厂方法模式解决实际问题。

 
   

 

 

[实验任务一]:加密算法

目前常用的加密算法有DES(Data Encryption Standard)和IDEA(International Data Encryption Algorithm)国际数据加密算法等,请用工厂方法实现加密算法系统。

实验要求:

1.  画出对应的类图;

 

2.提交该系统的代码,该系统务必是一个可以能够直接使用的系统,查阅资料完成相应加密算法的实现;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.SecureRandom;
import java.util.Base64;

public class DESEncryption {

    // 生成DES密钥
   
public static SecretKey generateKey() throws Exception {
        KeyGenerator keyGenerator = KeyGenerator.getInstance("DES");
        keyGenerator.init(56, SecureRandom.getInstanceStrong());
        return keyGenerator.generateKey();
    }

    // DES加密
   
public static String encrypt(String data, SecretKey key) throws Exception {
        Cipher cipher = Cipher.getInstance("DES");
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] encryptedData = cipher.doFinal(data.getBytes("UTF-8"));
        return Base64.getEncoder().encodeToString(encryptedData);
    }

    // DES解密
   
public static String decrypt(String encryptedData, SecretKey key) throws Exception {
        Cipher cipher = Cipher.getInstance("DES");
        cipher.init(Cipher.DECRYPT_MODE, key);
        byte[] data = cipher.doFinal(Base64.getDecoder().decode(encryptedData));
        return new String(data);
    }

    public static void main(String[] args) {
        try {
            // 生成密钥
           
SecretKey key = generateKey();
            String originalText = "Hello, World!";

            // 加密
           
String encryptedText = encrypt(originalText, key);
            System.out.println("Encrypted Text: " + encryptedText);

            // 解密
           
String decryptedText = decrypt(encryptedText, key);
            System.out.println("Decrypted Text: " + decryptedText);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 

3.注意编程规范。

posted @ 2024-11-27 09:31  Code13  阅读(7)  评论(0)    收藏  举报