Design patterns 我的代码是否实现了工厂模式?这是一个糟糕的设计吗?

Design patterns 我的代码是否实现了工厂模式?这是一个糟糕的设计吗?,design-patterns,Design Patterns,我不熟悉软件设计模式,并尝试在我的应用程序中实现工厂设计(创造性模式),以便使用密钥进行加密/解密 我想确定这是工厂模式,如果它是一个糟糕的设计。还有,如果你能帮助改善它 我的代码如下: 抽象基类: public abstract class EncryptDecrypt { protected Key getKey() { Key key = new SecretKeySpec(getKeyValue().getBytes(), getAlgorithm()); return

我不熟悉软件设计模式,并尝试在我的应用程序中实现工厂设计(创造性模式),以便使用密钥进行加密/解密

我想确定这是工厂模式,如果它是一个糟糕的设计。还有,如果你能帮助改善它

我的代码如下:

抽象基类:

public abstract class EncryptDecrypt {
protected Key getKey() {
    Key key = new SecretKeySpec(getKeyValue().getBytes(), getAlgorithm());
    return key;
}
protected Cipher getCipher() throws NoSuchAlgorithmException, NoSuchPaddingException {
    Cipher c = Cipher.getInstance(getAlgorithm());
    return c;
}

protected abstract String getKeyValue();
protected abstract String getAlgorithm();

public final String encryptText(String valueToEnc) throws IllegalBlockSizeException, BadPaddingException, InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException {
    String keyValue = getKeyValue();
    String algorithm = getAlgorithm();
    Key key = getKey();
    Cipher c = getCipher();
    c.init(Cipher.ENCRYPT_MODE, key);
    byte[] encValue = c.doFinal(valueToEnc.getBytes());
    String encryptedValue = new String(Base64.getEncoder().encode(encValue));
    return encryptedValue;
}

public final String decryptText(String encryptedValue) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException {
    String keyValue = getKeyValue();
    String algorithm = getAlgorithm();
    Key key = getKey();
    Cipher c = getCipher();
    c.init(Cipher.DECRYPT_MODE, key);
    byte[] decordedValue = Base64.getDecoder().decode(encryptedValue);
    byte[] decValue = c.doFinal(decordedValue);
    String decryptedValue = new String(decValue);
    return decryptedValue;
}
}
下面是一个示例AES实现:

public class AESEncryptDecrypt extends EncryptDecrypt {

@Override
protected String getKeyValue() {
    return "ThisIsA Key 1234";
}

@Override
protected String getAlgorithm() {
    return "AES";
}
}
客户端类:

public class Test {

public static void main(String[] args) throws Exception {
    EncryptDecrypt ed = new AESEncryptDecrypt();
    String msg = "Text message@yahoo.com";
    String e = ed.encryptText(msg);
    System.out.println(e);
    System.out.println(ed.decryptText(e));
}

}

非常感谢您花时间回答。

工厂模式[1]是隐藏和简化对象创建的一种方法。例如,如果您有许多
Encrypt Decrypt
算法的实现,并且每个算法都具有不同的初始化和配置,那么对于用户来说,使用这些实现将很困难,因为有太多和不同的配置,要使用它们,用户将需要阅读大量信息,以了解如何创建他需要的对象。因此,工厂模式有助于提供一个独特的界面,用户可以使用所有不同的
encrypt-decrypt
算法和方法来创建它们,而无需知道如何初始化它们

假设你在一家有很多食物的餐馆里,你想点些东西。如果没有菜单,你觉得会怎么样?你需要检查所有种类的食物和准备工作,以确定你想要什么。Factory就像一个菜单,您可以将所有选项组合在一起并组织起来以便于使用

现在,如果您查看
EncryptDecrypt
用法

EncryptDecrypt ed = new AESEncryptDecrypt();
String msg = "Text message@yahoo.com";
String e = ed.encryptText(msg);
System.out.println(e);
System.out.println(ed.decryptText(e));
您将注意到,它没有创建任何对象,它与菜单不同。实际上,
EncryptDecrypt
本身就是完成所有工作的对象。但另一方面,你有

protected Key getKey() {
    Key key = new SecretKeySpec(getKeyValue().getBytes(), getAlgorithm());
    return key;
}
protected Cipher getCipher() throws NoSuchAlgorithmException, NoSuchPaddingException {
    Cipher c = Cipher.getInstance(getAlgorithm());
    return c;
}
这更像是一个工厂,在那里你向用户隐藏对象(密码和密钥)的创建,用户甚至不需要知道这些对象的存在

因此,我的答案是,
EncryptDecrypt
不是工厂,为了做到这一点,需要将对象使用的对象创建(工厂)分开

参考文献:


[1] .

您的设计看起来更像模板设计模式。