4

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;
    }
}
flip
  • 391
  • 1
  • 7
  • 15
  • Are you planning to reuse `CreateCodeWithSubCode` method? – Dennis Jul 28 '13 at 10:37
  • Yes. This function and many more like it are heavily re-used. – flip Jul 28 '13 at 10:44
  • imho that's rather the Builder pattern and not the facade pattern. And you should avoid static methods both for testability and since it prevents extension through inheritance (or through interfaces) – jgauffin Jul 28 '13 at 10:51
  • @PhillipPartridge: so, your code just fine. You're just hiding some operations in factory method. It's OK. – Dennis Jul 28 '13 at 11:00
  • @jgauffin I am simply creating a wrapper and certainly don't want to go about extending or interfacing the Java library. The Java library comes complete with unit tests and I am also able to unit test the C# code, so I think I am covered here. – flip Jul 28 '13 at 13:31

1 Answers1

3

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.

Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232