哪个选项更好 - 将对象传递给方法与否? [英] Which option is better - Pass object to method or not ?

查看:79
本文介绍了哪个选项更好 - 将对象传递给方法与否?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于将对象传递给方法或使用键值在方法中创建对象的问题。哪种方法更好?



请考虑以下代码。在BuilderOrder类中,有一个名为GenerateKey的方法,它需要访问BuilderMaster对象。将BuilderMaster对象直接作为参数传递给方法或传递键值并在方法中创建BuilderMaster对象是一个好主意。



I have a question regarding passing object to method or creating an object within the method using key value. Which is a better approach ?

Consider below code. In BuilderOrder class there is a method called GenerateKey which needs to access BuilderMaster object. Is it a good idea to pass BuilderMaster object directly as a parameter to the method or pass key value and create BuilderMaster object within method.

public class BuilderMaster
{
    public DTO.BuilderMaster GetBuilderMaster(string systemNo)
    { 
        // rerurn builderMaster object
    }
}

public class BuilderOrder
{
    //Option 1 : Pass systemNo value from UI and create object .
    public bool GenerateKeys(string systemNo,int  noOfKeys)
    {
        BuilderMaster objBuilderMaster = new BuilderMaster();
        DTO.BuilderMaster objThisBuilder =  objBuilderMaster.GetBuilderMaster(systemNo);

        //use objThisBuilder's properties during key generation process.
    }

    //Option 2 : Pass object from UI
    public bool GenerateKey(DTO.BuilderMaster objThisBuilder, int noOfKeys)
    {
        //use objThisBuilder's properties during key generation process.
    }
}





选项1产生紧耦合,而选项2则没有。



现在考虑GenerateKey是由Web服务公开的方法,该服务由在不同计算机上运行的Web应用程序调用。与选项2相比,选项1创建了一个更轻的请求对象,如在选项1中,您只传递基本类型,而在选项2中,您传递的是整个构建器主对象。



哪一个更好的方法?



Option 1 creates tight coupling while Option 2 does not.

Now consider GenerateKey is method exposed by a web service which is being called by a web application running on a different machine. Option 1 creates a lighter request object compared to option 2 as in option 1 you are only passing primitive types while in Option 2 you are passing a whole builder master object.

Which is a better approach ?

推荐答案

这实际上不是一个设计模式问题。



我的答案是传递对象。



0)另一种方法是将方法与课程结合得太紧。

1)如果传入对象 - 特别是如果您使用接口 - 那么您可以传入其他/派生类。或模拟对象!

2)模块化 - 然后设置。

3)考虑将来会发生什么,如果还有其他东西需要设置。
That's not actually a Design Pattern question.

My answer is "pass the object".

0) The other way couples the method to the class too tightly.
1) If you pass in the object -- and especially if you use an Interface -- then you can pass in other/derived classes. Or mocked objects!
2) Modularity -- get then set.
3) Consider what happens in the future if there's something else to set.


作为测试器,它是简单的选项2.这使得模拟DTO.BuilderMaster的创建变得简单,这反过来又增加了代码的可测试性。此外,如果你需要一个不同的DTO.BuilderMaster实现,如果你将Buildmaster定义为一个接口,你可以提供接口的任何特定的实现。
As a Tester it is simple Option 2. This makes the creation of a mock DTO.BuilderMaster simple which in turns increases the testability of the code. Also if you need a different implementation of the DTO.BuilderMaster if you had the Buildmaster defined as an interface the you could supply any particular implmentation of the interface.


这篇关于哪个选项更好 - 将对象传递给方法与否?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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