简体   繁体   English

执行业务功能时收集数据的对象模式

[英]Object pattern to collect the data while executing a business function

I have a class that performs a business function and there are multiple methods being called by the entry-point method. 我有一个执行业务功能的类,入口点方法调用了多种方法。

public class Tool
{
     public void runTool()
     {
         methodA();
         methodB();
         methodC();
         printToolSummary();
     }
}

Each of these methods method* called perform a specific sub-function and I would like to collect the results/attributes of interest in each of these methods into a summary object. 这些方法中的每个method*被称为执行特定的子功能,我想将这些方法中每个方法的感兴趣结果/属性收集到一个摘要对象中。 Its not a single value to be returned by these method* methods. 这些method*不是要返回的单个值。

I was thinking of a single object that contains all the attributes of interest, pass this object to each method and let that method set the attribute, but somehow it doesn't sound right to work with a mutable object as there is no way to prevent methodA from updating an attribute it shouldn't, or am I worrying too much? 我当时在想一个包含所有感兴趣属性的对象,将该对象传递给每个方法并让该方法设置属性,但是以某种方式使用可变对象听起来并不正确,因为无法防止methodA从不应该更新属性,还是我担心太多?

If you're concerned about the methods touching some part of the object that they shouldn't then the object you're passing into the methods should implement several interfaces with each one only exposing the properties that you want the acting method to touch. 如果您担心方法不应该接触对象的某些部分,那么您传递给方法的对象应实现多个接口,每个接口仅公开您希望作用方法接触的属性。

So methodA could expect a class of type A and methodB could expect a class of type B where A and B are interfaces... 所以methodA可以期望一个类型为A的类,而methodB可以期望一个类型为B的类,其中A和B是接口...

Otherwise there's not really anything wrong with your approach and it's fairly common. 否则,您的方法实际上并没有什么错,这很普遍。

How about using a complex object that contains sub-classes for each of your methods. 如何使用包含每个方法的子类的复杂对象。 For example: 例如:

public class ResultA { // some attributes for Method A }

public class ResultB { // some attributes for Method B }

public class ResultC { // some attributes for Method C }


public class Result {

    private ResultA resultA;
    private ResultB resultB;
    private ResultC resultC;
}

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

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