繁体   English   中英

实施工厂模式

[英]Implement Factory Pattern

目前,我正忙于将Factory Pattern实施到Java代码中。 我有这些课:

CipherStorage

public class CipherStorage {

    protected String transformation;
    protected String provider;
    protected String providerPair;

    public CipherStorage(String transformation, String provider, String providerPair){
        this.transformation = transformation;
        this.provider = provider;
        this.providerPair = providerPair;
    }
}

CipherStorageProcessor(接口)

public interface CipherStorageProcessor {
    byte[] encryptData(String keyName, String input, @Nullable SharedPreferences pref);
    byte[] decryptData(String keyName, byte[] encrypted, @Nullable SharedPreferences pref);
}

CipherStorageRSA

public class CipherStorageRSA extends CipherStorage implements CipherStorageProcessor {

    public CipherStorageRSA(String transformation, String provider,String providerPair){
        super(transformation, provider, providerPair);
    }
}

CipherStorageAES

public class CipherStorageAES extends CipherStorage implements CipherStorageProcessor {

    public CipherStorageAES(String transformation, String provider, String providerPair){
        super(transformation, provider, providerPair);
    }
}

CipherStorageFactory

public class CipherStorageFactory {

    public CipherStorage getCipher(String cipherType) {
        if (cipherType == null) {
            return null;
        }
        if (cipherType.equalsIgnoreCase("AES")) {
            return new CipherStorageAES();

        } else if (cipherType.equalsIgnoreCase("RSA")) {
            return new CipherStorageRSA();

        }
    }
}

这段代码有意义吗? 向工厂添加参数是否正确? 有更好的方法吗?

非常感谢您的帮助。

PS:我从类中删除了两个接口函数,以防止编写大量代码。

编辑:

例如,当我创建RSA实例时:

CipherStorage RSA = CipherStorageFactory.getCipher("RSA");

我无法访问界面中的方法吗?

  1. 关于工厂模式

您对工厂模式的实现是正确的。

关于具体类的构造函数参数,您可以将它们预配置到工厂对象中(因为这些参数在工厂创建的不同密码对象中是通用的),并使用它们在工厂方法内创建实例:

public class CipherStorageFactory {
    private String transformation;
    private String provider;
    private String providerPair;

    public CipherStorageFactory(String transformation, String provider, String providerPair){
        this.transformation = transformation;
        this.provider = provider;
        this.providerPair = providerPair;
    }

    public CipherStorage getCipher(String cipherType) {
        //...
        if (cipherType.equalsIgnoreCase("AES")) {
            return new CipherStorageAES(transformation, provider, providerPair);
        } else 
        //...
    }
}

此外,在这种情况下,最好将工厂方法命名为“ createCipher() ”,因为它每次都会返回一个新实例。

  1. 关于接口方法

之所以不能访问encryptdata()方法,是因为将创建的密码对象强制转换为超级类型( CipherStorage ),并且它没有这些方法。 您可以执行的选择之一是,将2个方法从接口移至CipherStorage并将它们(以及类本身)声明为抽象,在这种情况下,您将不需要接口:

public abstract class CipherStorage {
    public abstract byte[] encryptData(String keyName, String input, @Nullable SharedPreferences pref);
    public abstract byte[] decryptData(String keyName, byte[] encrypted, @Nullable SharedPreferences pref);
}

在以下情况下使用“工厂方法”模式

1.一个类无法预期它必须创建的对象的类

2.一个类希望其子类指定其创建的对象

3.将委托责任归类到几个帮助程序子类之一,并且您想定位哪个帮助程序子类是委托人的知识

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM