简体   繁体   English

仅具有静态功能的Facade Pattern是不必要的层

[英]Is a Facade Pattern with only static functions an unnecessary layer

I am currently using IKVM to gain access to a large Java library within a C# .Net project. 我目前正在使用IKVM访问C#.Net项目中的大型Java库。 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. 我目前在这个单例周围创建了一个C#Facade Pattern,并通过这个Facade进行对象创建和参数传递。 The functions within the Facade are all static. Facade中的功能都是静态的。 Is it normal that a Facade Pattern only contain static functions or have I just created an extra layer with very little value? Facade Pattern只包含静态函数或者我刚刚创建了一个价值非常小的额外图层,这是正常的吗?

The original Java code would look something like this: 原始的Java代码看起来像这样:

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: 简化(无错误检查)C#版本看起来像这样:

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;
    }
}

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. 如果您不止一次地利用这种方法,那么保留它,每次使用它可以节省8到10行代码,使其更加稳定和可维护。

If you're not going to reuse it, then just put the body of the method where you need it. 如果您不打算重复使用它,那么只需将方法的主体放在您需要的位置即可。

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

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