简体   繁体   中英

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. Its not a single value to be returned by these method* methods.

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?

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...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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