gpt4 book ai didi

使用凯撒密码对字符串进行 Java 装饰器模式

转载 作者:行者123 更新时间:2023-12-01 11:41:16 27 4
gpt4 key购买 nike

所以我尝试遵循装饰器模式来加密和解密字符串。我让加密部分工作得很好,但在从 main 调用解密时遇到问题。我想使用刚刚加密的字符串,而不是再次使用默认字符串。我怎样才能实现这个目标?下面是我的类(class)(类(class)很多,但很短)

字符串

// Blueprint for classes that will have decorators
public interface AString {

public String getAString();
}

纯字符串

public class PlainString implements AString {

String test = "thisIsATest";

public PlainString(){
System.out.println("Created plain string: " + test);
}
@Override
public String getAString() {
// TODO Auto-generated method stub
return test;
}
}

字符串装饰器

abstract class StringDecorator implements AString {

protected AString tempString;
public StringDecorator(AString newString){
tempString = newString;
}
public String getAString(){
return tempString.getAString();
}
}

加密写入器

public class EncryptingWriter extends StringDecorator{

public EncryptingWriter(AString newString) {
super(newString);

System.out.println("Adding Ceasar Cipher Encryption");

}
public String getAString(){

return encryptString((PlainString)tempString); //decorated
}

public String encryptString(PlainString aString){
String s = "";
int len = aString.test.length();

for(int i = 0; i < len; i++){
char c = (char)(aString.test.charAt(i) + 3);
if(c > 'z')
s += (char)(aString.test.charAt(i)) - (26-3);
else
s += (char)(aString.test.charAt(i) + 3);
}
return s;
}

}

解密阅读器

public class DecryptingReader  extends StringDecorator {

public DecryptingReader(AString newString) {
super(newString);

System.out.println("Adding Ceasar Cipher Decryption");

}
public String getAString(){
return decryptString((PlainString)tempString); //decorated
}

public String decryptString(PlainString aString){
String s = "";
int len = aString.test.length();
for(int i = 0; i < len; i++){
char c = (char)(aString.test.charAt(i) - 3);
if(c > 'z')
s += (char)(aString.test.charAt(i)) + (26+3);
else
s += (char)(aString.test.charAt(i) - 3);
}
return s;
}
}

DecoratorTester(主要)

public class DecoratorTester {

public static void main(String[] args) {
// TODO Auto-generated method stub

AString basicString = new EncryptingWriter(new PlainString());
String encryptedString = basicString.getAString();
System.out.println("EncryptedString: " + encryptedString);

DecryptingReader d = new DecryptingReader(basicString);
System.out.println(d.tempString.getAString());
//d.decryptString(d.tempString.getAString()); // how can I decrypt the string?
}

}

最佳答案

它应该是这样的(基于我的评论)。

我正在重命名您的类以使其更加合理,特别是在装饰器的上下文中:

interface StringProvider {  // an interface that will produce a String
String getStringValue();
}

class PlainStringProvider implements StringProvider {
private String stringValue;
public PlainStringProvider(String stringValue) {
this.stringValue = stringValue;
}
@Override
public String getStringValue() {
return this.stringValue;
}
}

class StringProviderDecorator implements StringProvider {
private StringProvider delegate;
public StringProviderDecorator(StringProvider delegate) {
this.delegate = delegate;
}
@Override
public String getStringValue() {
return this.delegate.getStringValue();
}
}

class EncryptStringProvider extends StringProviderDecorator {
public EncryptStringProvider(StringProviderDecorator delegate) {
super(delegate);
}
// your stuff about encryption
}


class DecryptStringProvider extends StringProviderDecorator {
public DecryptStringProvider (StringProviderDecorator delegate) {
super(delegate);
}
// your stuff about decryption
}

因此使用它们的代码如下所示:

String originalValue = "this is a test";

String encryptedValue
= new EncryptStringProvider(new PlainStringProvider(originalValue));

String decryptedValue
= new DecryptStringProvider(new PlainStringProvider(encryptedValue));

assert decryptedValue == originalValue;

关于使用凯撒密码对字符串进行 Java 装饰器模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29503918/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com