views:

155

answers:

9

I'm having difficulties with some general OOP & Java approach. There are various ways to let classes/objects communicate with each other. To give a simple example:

  • I need object A to perform action X.
  • Object A needs P, Q and R to perform this action X.

Will then Object A retrieve P, Q and R by itself (within action X), or must these values be parameters for action X?

A: 

If the values can be reused, then pass them (or a part of the) as parameters. Otrherwise, create them as local variables inside X.

lmsasu
+4  A: 

This is too general a question to be answered concretely. Either approach can be good in certain situations. Some factors:

  • passing P, Q and R as parameters makes A easier to reuse and test (see Dependency Injection)
  • if P, Q and R are not used anywhere else outside A, they could be made method local
  • if P, Q and R are also used in other methods of A, they could be made members
Péter Török
A: 

Pass P, Q and R to A in some way, either by setting it as properties of A or by passing them to X (depending on if PQR are X-specific or if they may be needed by A in other situations too).

Emil Vikström
A: 

The answer is that it depends.

If P, Q and R have a strong, meaningful link to an instance of A then you might consider adding them as members of class A.

public class A {

  private P p;
  private Q q;
  private R r;

  public A(P p, Q q, R r) {
    this.p = p;
    this.q = q;
    this.r = r;
  }

  public void x() {
    p.doSomething();
    q.doSomething();
    r.doSomething();
  }

}

If P, Q and R are not, and are just "some values" that help action X perform it's task, then you might want to use them as parameters

public class A {

  public void x(P p, Q q, R r) {
    p.doSomething();
    q.doSomething();
    r.doSomething();
  }

}

The second version doesn't maintain any state so it is by definition thread-safe

Scobal
A: 

Approach 1

class A{
  ctor(P, Q, R)
  {
  }
  void X()
  {
    P.SomeMethod();
    Q.SomeMethod();
    R.SomeMethod();
  }
}

Approach 2

class A{
  void X(P, Q, R)
  {
    P.SomeMethod();
    Q.SomeMethod();
    R.SomeMethod();
  }
}
PieterG
A: 

Class-design is a subset of software design: so it all depends.

As the question is little subjective (everybody has a different approach) I will only say that I use this method, without saying that it's the best. Probably there are a lot of different ways.

public interface FuncX {

    public void actionX(FuncP p, FuncQ q, FuncR r);

}

And let classes implement this interface. If two classes are small but related, I let them implement both interfaces.

It makes each implementation very easy testable. To bootstrap the system, a main method must create instances of specific classes. This can be configurable, for instance.

public class MyFuncX implements FuncX, FuncP {

    public void actionX(FuncP p, FuncQ q, FuncR r) {
        ...
    }

    public void actionP(...) {
        ...
    }

}

// the caller:
FuncX x = new MyFuncX(); // dependency
FuncQ q = ...;
FuncR r = ...;

x.actionX(x, q, r);
Pindatjuh
A: 

It sounds to me like work for Composite Pattern.

You basically pass objects P, Q and R into A and then execute certain method inside all of them. This pattern is used when you need several objects to perform the same action. So, as example on wiki says, you'd basically do something as this:

graphic1.add(ellipse1);
graphic1.add(ellipse2);
graphic1.add(ellipse3);

graphic.print();

and graphic.print(); would call print method inside each of the ellipse objects.

Ondrej Slinták
A: 

In your project if the lifetime of the objects P,Q,R lies within the action X then create objects within the action itself. Instead if the lifetime does not depend on the action X then pass it as an argument. Also take care of the scope of the objects P,Q,R. I recommend you to go through the concepts like Association, Aggregation, Composition and Dependency before designing a class. Take a call based on the scope and lifetime of the objects after understanding those concepts.

liaK
A: 

Make it the most convenient way possible?

You could write a function/method to accept 18+ arguments, but it's not really convenient.

If the method which is going to do action A is always getting the same arguments (and you're very sure this won't change), I don't see why you need them passed by argument.

That said, I don't usually stick to standards and don't know what other OOP fundamentalists (hype gurus) would do in this situation.

Christian Sciberras