Bulder Design Pattern为具有大量参数的方法制定通用方法 [英] Bulider Design Pattern to make generic method for methods having large number of parameters

查看:95
本文介绍了Bulder Design Pattern为具有大量参数的方法制定通用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个界面 Itest ClassA & ClassB 正在实现此接口。 testA & testB 分别是这些类中的方法。

I have an interface Itest and ClassA & ClassB are implementing this interface. testA & testB are methods in these classes respectively.

testA(String a, String b, String c, D d, E e)

testB(String a, String b, String c, F f, G g) 

code> D , E F G 是自定义数据类型(与数据库相关)。我简化了实际上他们有更多参数的方法。

Here D, E, F, G are custom data types (related to databases). I simplified the methods actually they have more number of parameters.

我需要在 testAB 中在 Itest 接口并在两个类中实现,而不是使用自己的方法。

I need to make a generic method in testAB in Itest interface and implement that in both classes rather than having their own method.

testAB(String a, String b, String c, D d, E e, F f, G g)

由于参数数量更多,通用方法 testAB 将为用户带来痛苦,因为他必须传递这么多 null 值。

As the number of parameters is more, generic method testAB will be pain for the user as he has to pass so many null values.


  • 这是一个用于 Bulder Design Pattern 的用例吗?

如果是,如何使用这种设计模式实现?

If yes, how to achieve this using this design pattern?

推荐答案

看起来像您的核心要求是,您不希望客户端在不需要时传递其他参数。您可以使用简单的旧方法重新加载来解决您的问题:

Looks like the core requirement that you have is that you don't want the client to be passing additional parameters when they are not required. You can solve your problem using plain old method overloading :

更改您的 ITest 接口有一个叫的方法测试

public interface ITest {
     public void test(String a,String b,String c,D d,E e,F f,G g);
}

更改 A as如下:

public class A implements ITest {

     //this is an overload - v1
     public void test(String a,String b,String c,D d,E e) {
            //dispatch the call to the overriden method
            test(a,b,c,d,e,null,null);
     }

     //this is an overload - v2
     public void test(String a,String b,String c,E e,F f) {
           //dispatch the call to the overriden method
           test(a,b,c,null,null,e,f);
     }

     @Override
     //this is an overriden method - v3
     public void test(String a,String b,String c,D d,E e,F f,G g) {
            if(d!=null && e!=null) {
                //use a,b,c,d,e and do something
            } 

            if(f!=null && g!=null) {
                //use a,b,c,f,g and do something
            }
     }
}

现在,客户端代码可以调用他们想要的任何重载形式而不需要通过 null 。您的重载方法将简单地将调用调用到一个常见的方法(这给您的代码重用的优势):

Now the client code can call whichever overloaded form they want without the need to pass null. Your overloaded methods will simply dispatch the call to a common method (which gives you the advantage of code reuse):

classAObj.test("1","2","3",new D(),new E());//calls overloaded method - v1
classAObj.test("1","2","3",new F(),new G());//calls overloaded method - v2
classAObj.test("1","2","3",new D(),new E(),new F(),new G());//calls overriden method - v3

注意客户端代码不必担心在不需要时传递其他参数。还要注意客户端调用的清洁程度。也可以在 B 中进行类似的更改。

Notice how the client code does not have to worry about passing additional parameters when they are not required. Also notice how clean the client calls look. Similar changes can be made in B as well.

1。您可以选择使 ITest 一个抽象类。这将允许您使测试方法中的保护访问说明符。想要保护访问说明符的原因是限制客户端类能够访问该方法,并且始终通过重载的表单。如果您暂时使用界面,这是您可以考虑实施的附加功能。

1. You have an option to make ITest an abstract class. This will allow you to make the test method in it have the protected access specifier. The reason for wanting a protected access specifier is to restrict client classes from being able to access the method and always go through the overloaded forms instead. This is an add-on feature you can consider implementing in the future if you are stuck with using an interface at the moment.

2。您还可以利用泛型来避免每次都会引入一个新类的新类,但是从其他答案可以看出,很容易使代码复杂化。您还可以将重载方法添加到 ITest 界面,使其成为合同的一部分。但是,我故意将这些部分从我的答案中删除,因为您的问题的症结可以通过使用重载来解决。

2. You could also take advantage of Generics to avoid the need to write a new class every-time a new object type is introduced but as you can see from the other answers, this can easily complicate your code to a great extent. You could also add the overloaded methods to ITest interface to make it a part of its contract. However, I am deliberately leaving these parts out of my answer because the crux of your problem can be solved by using overloading.

3。 Builder 模式是一种创意模式。在这种特殊情况下,由于诸如 D E F G 是域对象。 Class A B 并不真正依赖于它们,而是将其用作数据源

3. The Builder pattern is a creational pattern. It's an overkill in this particular case since classes such as D,E,F and G are domain objects. Class A and B don't really depend on them in the real sense but use them as a source of data instead.

这篇关于Bulder Design Pattern为具有大量参数的方法制定通用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆