سؤال

I have a class with a bunch of fields, all private (subclasses access a few with protected getters). I need to pass most of those fields into a method in another class that will format them and generate output. Is it ok to have a method in the class with the fields that'll pass them all across? Or do these circumstances suggest that I should implement some other relationship between the two classes, as they seem closely coupled because of this?

Further information: class A represents Employees, class B's only responsibility is to format the output of the program.

هل كانت مفيدة؟

المحلول

Are you asking if it is OK to do the following?

public class A {
 private B myB = new B();
 private String myUnformattedName = "some information";

 public String getFormattedInfo() {
   return myB.formatInfo(myUnformattedName);
 }
}

That's perfectly OK.

Marking a field as private just means that only the containing class should be able to access it...

If you mean something else it's best to pop some code in your question to give people the context


OK, so there's no way to set the values here but you can see here two different ways to call the formatter. When the parameter list gets past three or four items then it gets difficult to read.

In this instance I'd just pass A into the formatter and have a get method for each value you want B to be able to read.

public class A {
 private B myB = new B();
 private String myUnformattedName = "some information";
 private String myUnformattedNameOne = "some information";
 private String myUnformattedNameTwo = "some information";
 private String myUnformattedNameThree = "some information";
 private String myUnformattedNameFour = "some information";
 private String myUnformattedNameFive = "some information";
 private String myUnformattedNameSix = "some information";

 public String getFormattedInfo() {
   //pass the object itself and use get methods
   return myB.formatInfo(this); 
 }

 public String getFormattedInfoLong() {
   //this is OK but gets difficult to read the longer the 
   //parameter list gets
   return myB.formatInfo(myUnformattedName, myUnformattedNameOne, 
      myUnformattedTwo, myUnformattedNameThree, myUnformattedNameFour,
      myUnformattedNameFive, myUnformattedNameSix); 
 }

 //getters
 public String getUnformattedName() {
    return myUnformattedName;
 }

 public String getUnformattedNameOne() {
    return myUnformattedNameOne;
 }

 //etc

}

نصائح أخرى

I would actually suggest the Visitor pattern.

Class A has a method that accepts a visitor, which in turn has a well-defined public method, such as this:

First, the visited class allows some class with well defined interface in, does not pass it's own data to the outside.

public class A {
    int data;

    public void getFormattedBy(Formatter f) {
       f.format(data);
    }
}

The interface of the visitor, allows for multiple formatters

public interface Formatter {
    void format (int data);
}

A formatter, that is allowed into the visited class.

public class B implements Formatter {
    public void format(int data) {
        // do the formatting and printing
    }
}

This way you simply call

A a = new A();
B b = new B(); // the formatter
a.getFormattedBy(b);

In the end, a visitor (the formatter) can visit many classes that allow allow the visitor in (probably by implementing an interface on their own), and a visited class can be visited by many visitors.

I think it's perfectly fine to pass them across, as long as they either have primitive type or are immutable.

If the callee can modify them when not being supposed to, then you have a design issue.

It's OK as far as the class B is not modifing them. If the class does do, pass immutable instances to it.

You can make Class B a utility class and have static methods only on it.

Then inside your Class A you can have sth like:

public String formatMyVariables() {

return B.format(a,b,c,d);

}

I assume the output you mentioned is a String but it can be anything really.

You should consider that if your Employee fields are primitive values (int, boolean) or immutable ones (like String for example), then you can let other classes read them without worrying.

Protecting fields with private is the way not to expose the internal working of your object, which can solely be accessed via its public API. However, when your class really represents a business object (ie a group of values that identify an entity) then it's perfectly safe to let others read internal fields.

best way to transfer data are the DTO objects.

these objects only contains the instance variables (with setters and getters) as the data which you want to transfer!

there should be no behaviours in this class

for example if you want to pass a Employee data , do as follows

class EmployeeBean
{
private String name;
private String age;

public void setName(String n)
{
name=n;
}

public String getName()
{
return name;
}


public void setAge(int n)
{
age=n;
}

public int getAge()
{
return age;
}

}

you can now create the EmployeeBean class, populate data in its instance variables and then pass this object as parameter to the method in the other class where it can be formatted

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top