简体   繁体   English

实施工厂模式

[英]Implement Factory Pattern

Currently I'm busy with implement the Factory Pattern into my Java Code. 目前,我正忙于将Factory Pattern实施到Java代码中。 I've got these classes: 我有这些课:

CipherStorage 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 (Interface) CipherStorageProcessor(接口)

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

CipherStorageRSA CipherStorageRSA

public class CipherStorageRSA extends CipherStorage implements CipherStorageProcessor {

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

CipherStorageAES CipherStorageAES

public class CipherStorageAES extends CipherStorage implements CipherStorageProcessor {

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

CipherStorageFactory 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();

        }
    }
}

Is this code made any sense? 这段代码有意义吗? Is it correct to add parameters into the factory? 向工厂添加参数是否正确? Is there an better way to this? 有更好的方法吗?

Already thanks for any help. 非常感谢您的帮助。

PS: i removed the two interface functions from the classes to prevent a lot of code. PS:我从类中删除了两个接口函数,以防止编写大量代码。

EDIT: 编辑:

When i create an instance of RSA for example: 例如,当我创建RSA实例时:

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

I've got no access to the methods into the interface? 我无法访问界面中的方法吗?

  1. Regarding Factory Pattern 关于工厂模式

Your implementation of the factory pattern is correct. 您对工厂模式的实现是正确的。

Regarding constructor parameters of your concrete classes, you can preconfigure them into your factory object(as those parameters are common throughout different cipher objects that the factory creates), and use them to create instances within your factory method: 关于具体类的构造函数参数,您可以将它们预配置到工厂对象中(因为这些参数在工厂创建的不同密码对象中是通用的),并使用它们在工厂方法内创建实例:

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 
        //...
    }
}

Additionally, in this case, it is better to name your factory method like " createCipher() ", as it's returning a new instance every time. 此外,在这种情况下,最好将工厂方法命名为“ createCipher() ”,因为它每次都会返回一个新实例。

  1. Regarding the Interface Methods 关于接口方法

The reason why you can't access encryptdata() method is that, because you are casting the created cipher object to super type( CipherStorage ) and it doesn't have those methods. 之所以不能访问encryptdata()方法,是因为将创建的密码对象强制转换为超级类型( CipherStorage ),并且它没有这些方法。 One of the options you can do is, move the 2 methods from your interface to CipherStorage and declare them (as well as the class itself) as abstract, and you won't need an interface in that case: 您可以执行的选择之一是,将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);
}

Use the Factory Method pattern when 在以下情况下使用“工厂方法”模式

1.a class can't anticipate the class of objects it must create 1.一个类无法预期它必须创建的对象的类

2.a class wants its subclasses to specify the objects it creates 2.一个类希望其子类指定其创建的对象

3.classes delegate responsibility to one of several helper subclasses, and you want to localize the knowledge of which helper subclass is the delegate 3.将委托责任归类到几个帮助程序子类之一,并且您想定位哪个帮助程序子类是委托人的知识

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

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