Pregunta

I am currently using IKVM to gain access to a large Java library within a C# .Net project. The libraries entry point is a singleton and from there I am able to create objects and set an objects properties.

I have currently created a C# Facade Pattern around this singleton and do my object creation and parameter passing via this Facade. The functions within the Facade are all static. Is it normal that a Facade Pattern only contain static functions or have I just created an extra layer with very little value?

The original Java code would look something like this:

Code code = Singleton.Instance.CreateCode();
code.SetExtension("12345");
code.SetId("1");

SubCode subCode = Singleton.Instance.CreateSubCode();
subCode.SetRoot("6789");
subCode.SetId("2");

code.SetSubCode(subCode);

A simplified (without error checking) C# version looks something like this:

public static FacadePattern
{
    public static Code CreateCodeWithSubCode(string extension, string codeId, string root, string subCodeId)
    {
        Code code = Singleton.Instance.CreateCode();
        code.SetExtension(extension);
        code.SetId(codeId);

        SubCode subCode = Singleton.Instance.CreateSubCode();
        subCode.SetRoot(root);
        subCode.SetId(subCodeId);

        code.SetSubCode(subCode);

        return code;
    }

    public static CreateCodeForHP(string extension, string codeId)
    {
        Code code = Singleton.Instance.CreateCode();
        code.SetExtension(extension);
        code.SetId(codeId);
        code.SetUse(com.org.vocabulary.HP);

        return code;
    }
}
¿Fue útil?

Solución

No, there is nothing wrong with static methods here. You don't need an object, your just trying to hide the implementation of getting what you want. If you're going to be leveraging this method more than once, leave it, you'll save yourself 8 - 10 lines of code every time making it more stable and maintainable.

If you're not going to reuse it, then just put the body of the method where you need it.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top