Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #35601 > unrolled thread

Good pattern for realizing the out-param capability in Java?

Started bysandeep6699@yahoo.com
First post2016-09-14 09:17 -0700
Last post2016-09-14 16:29 -0700
Articles 8 — 6 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  Good pattern for realizing the out-param capability in Java? sandeep6699@yahoo.com - 2016-09-14 09:17 -0700
    Re: Good pattern for realizing the out-param capability in Java? Robert Klemme <shortcutter@googlemail.com> - 2016-09-14 18:43 +0200
    Re: Good pattern for realizing the out-param capability in Java? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-09-14 12:55 -0400
    Re: Good pattern for realizing the out-param capability in Java? j4n bur53 <janburse@fastmail.fm> - 2016-09-14 21:00 +0200
    Re: Good pattern for realizing the out-param capability in Java? Eric Douglas <e.d.programmer@gmail.com> - 2016-09-14 12:54 -0700
      Re: Good pattern for realizing the out-param capability in Java? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-09-14 17:25 -0400
        Re: Good pattern for realizing the out-param capability in Java? j4n bur53 <janburse@fastmail.fm> - 2016-09-14 23:45 +0200
    Re: Good pattern for realizing the out-param capability in Java? Lew <lewbloch@gmail.com> - 2016-09-14 16:29 -0700

#35601 — Good pattern for realizing the out-param capability in Java?

Fromsandeep6699@yahoo.com
Date2016-09-14 09:17 -0700
SubjectGood pattern for realizing the out-param capability in Java?
Message-ID<ec81f584-4e19-4f0b-ba29-924ef4e1a570@googlegroups.com>
I want to solicit the opinion of the Java experts about realizing the out-param capability in Java.  Suppose there is a method in a class where I would like to realize the following:

class MyClass {
    int myMethod(int inIntParam, int outIntParam, double outDoubleParam) {
        // Accepts inIntParam as input
        // Updates outIntParam and outDoubleParam
        // Returns an int
        …
    }
    …
}

Since primitive types are passed by values, and primitive type wrapper classes are immutable, I am thinking of having outIntParam and outDoubleParam as instance variables in the class, which get set prior to calling myMethod.  Then myMethod can make do with only one parameter i.e. 

class MyClass {
    protected int outIntParam;
    protected double outDoubleParam;

    int getOutIntParam() {
        return outIntParam;
    }

    double getOutDoubleParam() {
        return outDoubleParam;
    }
    // 
    // No Settors for these instance variables
    // …

    int myMethod(int inIntParam) {
        // Accepts inIntParam as input
     // Updates instance variables in outIntParam and outDoubleParam
    // Returns an int
    …
    }
    …
}

Kindly suggest if there is a preferred way to realize the same.

Regards,
Sandeep

[toc] | [next] | [standalone]


#35602

FromRobert Klemme <shortcutter@googlemail.com>
Date2016-09-14 18:43 +0200
Message-ID<e3td1dF3ufvU1@mid.individual.net>
In reply to#35601
On 14.09.2016 18:17, sandeep6699@yahoo.com wrote:
> I want to solicit the opinion of the Java experts about realizing
> the  out-param capability in Java.

It's been quite a while since I have seen this asked but apparently some 
topics do not die.

> Suppose there is a method in a class where
> I would like to realize the following:
>
> class MyClass {
>     int myMethod(int inIntParam, int outIntParam, double outDoubleParam) {
>         // Accepts inIntParam as input
>         // Updates outIntParam and outDoubleParam
>         // Returns an int
>         …
>     }
>     …
> }
>
> Since primitive types are passed by values, and primitive type
> wrapper  classes are immutable, I am thinking of having outIntParam and
> outDoubleParam as instance variables in the class, which get set prior
> to calling myMethod. Then myMethod can make do with only one parameter i.e.

This is a bad idea.  It will break under multithreaded usage.  Also, 
design wise this state does not belong into the instance.  From the 
instance's point of view this is purely transitional state which you now 
bind into the instance for ever.

> class MyClass {
>     protected int outIntParam;
>     protected double outDoubleParam;
>
>     int getOutIntParam() {
>         return outIntParam;
>     }
>
>     double getOutDoubleParam() {
>         return outDoubleParam;
>     }
>     //
>     // No Settors for these instance variables
>     // …
>
>     int myMethod(int inIntParam) {
>         // Accepts inIntParam as input
>      // Updates instance variables in outIntParam and outDoubleParam
>     // Returns an int
>     …
>     }
>     …
> }
>
> Kindly suggest if there is a preferred way to realize the same.

Your example is pretty generic and does not say much about the real use 
case.  In absence of more knowledge my advice is: create a class that 
contains the necessary state and return an instance of that or receive 
an instance of that class as parameter and manipulate it in the method - 
whichever is more appropriate for your use case.

Kind regards

	robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

[toc] | [prev] | [next] | [standalone]


#35603

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2016-09-14 12:55 -0400
Message-ID<nrbvdk$j4p$1@dont-email.me>
In reply to#35601
On 9/14/2016 12:17 PM, sandeep6699@yahoo.com wrote:
> I want to solicit the opinion of the Java experts about realizing the out-param capability in Java.  Suppose there is a method in a class where I would like to realize the following:
>
> class MyClass {
>     int myMethod(int inIntParam, int outIntParam, double outDoubleParam) {
>         // Accepts inIntParam as input
>         // Updates outIntParam and outDoubleParam
>         // Returns an int
>         …
>     }
>     …
> }
>
> Since primitive types are passed by values, and primitive type wrapper classes are immutable, I am thinking of having outIntParam and outDoubleParam as instance variables in the class, which get set prior to calling myMethod.  Then myMethod can make do with only one parameter i.e.
>
> class MyClass {
>     protected int outIntParam;
>     protected double outDoubleParam;
>
>     int getOutIntParam() {
>         return outIntParam;
>     }
>
>     double getOutDoubleParam() {
>         return outDoubleParam;
>     }
>     //
>     // No Settors for these instance variables
>     // …
>
>     int myMethod(int inIntParam) {
>         // Accepts inIntParam as input
>      // Updates instance variables in outIntParam and outDoubleParam
>     // Returns an int
>     …
>     }
>     …
> }
>
> Kindly suggest if there is a preferred way to realize the same.

     You're on the right track: The "out-param" is a reference to a
mutable object, and the method can change the values stored in that
object.  You could make the value fields accessible as in your
example, or you could use setXxx() methods for them; the choice is
yours (I'd prefer setXxx() in a "general-purpose" class, but might
use direct field access in a "tightly-bound" class, for example a
private nested class).

     One observation, though: There is no need to insist that the
mutable object be an instance of the same class that contains the
method wanting to mutate it.  For example, a simple wrapper like

	public class MutableInteger {
	    private int value;
	    public int getValue() {
	        return value;
	    }
	    public void setValue(int value) {
	        this.value = value;
	    }
	}

... could be used as an "out-parameter" in methods of MyClass,
YourClass, SecondClass, HistoryClass, and so on.  (If you intend
to use such a thing widely, you might want to add elaborations:
equals() and hashCode(), toString(), maybe extend Number, perhaps
implement Comparable.  But for "restricted" uses you might not
need all that extra baggage.)

-- 
esosman@comcast-dot-net.invalid
The Tooth Fairy has gone high-tech, now pays in Bitecoin.

[toc] | [prev] | [next] | [standalone]


#35604

Fromj4n bur53 <janburse@fastmail.fm>
Date2016-09-14 21:00 +0200
Message-ID<nrc6p4$cj4$1@news.albasani.net>
In reply to#35601
Low level granularity, use case agnostic approach:

     Value Holder Pattern:
     http://stackoverflow.com/questions/1403921/output-parameters-in-java
     A value holder for each out, or in-out parameter


High level granularity, use case oriented approach:

     Moment-Interval Archetype
 
http://www.step-10.com/SoftwareDesign/ModellingInColour/Moment-Interval.html
     A moment-interval for each function, that will also hold
     in out and in-out parameters, not only the in parameters

     (For object factories, this is popularized by Google
     as the builder pattern, but not all functions are object
     factories)

Bye

sandeep6699@yahoo.com schrieb:
> I want to solicit the opinion of the Java experts about realizing the out-param capability in Java.  Suppose there is a method in a class where I would like to realize the following:
>
> class MyClass {
>      int myMethod(int inIntParam, int outIntParam, double outDoubleParam) {
>          // Accepts inIntParam as input
>          // Updates outIntParam and outDoubleParam
>          // Returns an int
>          …
>      }
>      …
> }
>
> Since primitive types are passed by values, and primitive type wrapper classes are immutable, I am thinking of having outIntParam and outDoubleParam as instance variables in the class, which get set prior to calling myMethod.  Then myMethod can make do with only one parameter i.e.
>
> class MyClass {
>      protected int outIntParam;
>      protected double outDoubleParam;
>
>      int getOutIntParam() {
>          return outIntParam;
>      }
>
>      double getOutDoubleParam() {
>          return outDoubleParam;
>      }
>      //
>      // No Settors for these instance variables
>      // …
>
>      int myMethod(int inIntParam) {
>          // Accepts inIntParam as input
>       // Updates instance variables in outIntParam and outDoubleParam
>      // Returns an int
>      …
>      }
>      …
> }
>
> Kindly suggest if there is a preferred way to realize the same.
>
> Regards,
> Sandeep
>

[toc] | [prev] | [next] | [standalone]


#35605

FromEric Douglas <e.d.programmer@gmail.com>
Date2016-09-14 12:54 -0700
Message-ID<8959892d-cd75-4680-b836-260a971f3366@googlegroups.com>
In reply to#35601
On Wednesday, September 14, 2016 at 12:17:37 PM UTC-4, sande...@yahoo.com wrote:
> I want to solicit the opinion of the Java experts about realizing the out-param capability in Java.  Suppose there is a method in a class where I would like to realize the following:
> 
> class MyClass {
>     int myMethod(int inIntParam, int outIntParam, double outDoubleParam) {
>         // Accepts inIntParam as input
>         // Updates outIntParam and outDoubleParam
>         // Returns an int
>         …
>     }
>     …
> }

Methods don't normally need to pass out more than one value, so when they do I either create a custom object or pass out an array.  What advantage would you get from creating a generic custom class to pass out 2 values?

[toc] | [prev] | [next] | [standalone]


#35606

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2016-09-14 17:25 -0400
Message-ID<nrcf7k$ed3$1@dont-email.me>
In reply to#35605
On 9/14/2016 3:54 PM, Eric Douglas wrote:
> On Wednesday, September 14, 2016 at 12:17:37 PM UTC-4, sande...@yahoo.com wrote:
>> I want to solicit the opinion of the Java experts about realizing the out-param capability in Java.  Suppose there is a method in a class where I would like to realize the following:
>>
>> class MyClass {
>>     int myMethod(int inIntParam, int outIntParam, double outDoubleParam) {
>>         // Accepts inIntParam as input
>>         // Updates outIntParam and outDoubleParam
>>         // Returns an int
>>         …
>>     }
>>     …
>> }
>
> Methods don't normally need to pass out more than one value, so when they do I either create a custom object or pass out an array.  What advantage would you get from creating a generic custom class to pass out 2 values?

     I disagree with the premise: It is fairly common for a computation
to generate more than one result.  On the other hand, I agree with the
approach: It is (I feel) better to return the multiple results in a
single instance of a "holder" class.  java.awt.Shape's getBounds()
method returns its *four* outputs in a java.awt.Rectangle instance,
and there are many similar examples among the standard classes.

     Occasionally, though, the "out-param" simply cannot be avoided.
The various sort() methods of java.util.Arrays, for example, would be
a good deal less attractive if they left their argument arrays
unmodified and instead returned brand-new arrays as method results ...

-- 
esosman@comcast-dot-net.invalid
The Tooth Fairy has gone high-tech, now pays in Bitecoin.

[toc] | [prev] | [next] | [standalone]


#35607

Fromj4n bur53 <janburse@fastmail.fm>
Date2016-09-14 23:45 +0200
Message-ID<nrcgda$tup$1@news.albasani.net>
In reply to#35606
Eric Sosman schrieb:
>      I disagree with the premise: It is fairly common for a computation
> to generate more than one result.

Yes, this is kind of a flaw of the Java language,
and it was sometimes under discussion to introduce
many result functions and also many value assignments.

I think the Go programming language has this. (But
the have ugly exception handling on the other hand)

But this language extension hasn't happend yet, and
I think its a pitty, since I guess it could be
mapped to the existing byte code, maybe not to the
existing class format.

The promise that object creation is cheap, and stack
allocated by compiler optimizations doesn't really
matrialize. Try for example run code that runs fine
on a windows box, on a android device.

This is at least my recent impression.

Bye

[toc] | [prev] | [next] | [standalone]


#35610

FromLew <lewbloch@gmail.com>
Date2016-09-14 16:29 -0700
Message-ID<3cfbca14-f8b1-49ee-8bc4-a71d31bf1a36@googlegroups.com>
In reply to#35601
sande...@ wrote:
> I want to solicit the opinion of the Java experts about realizing the out-param capability in Java.  Suppose there is a method in a class where I would like to realize the following:
> 
> class MyClass {
>     int myMethod(int inIntParam, int outIntParam, double outDoubleParam) {
>         // Accepts inIntParam as input
>         // Updates outIntParam and outDoubleParam
>         // Returns an int
>         …
>     }
>     …
> }
> 
> Since primitive types are passed by values, and primitive type wrapper classes are immutable, I am thinking of having outIntParam and outDoubleParam as instance variables in the class, which get set prior to calling myMethod.  Then myMethod can make do with only one parameter i.e. [sic]
> 
> class MyClass {
>     protected int outIntParam;
>     protected double outDoubleParam;
> 
>     int getOutIntParam() {
>         return outIntParam;
>     }
> 
>     double getOutDoubleParam() {
>         return outDoubleParam;
>     }
>     // 
>     // No Settors for these instance variables
>     // …
> 
>     int myMethod(int inIntParam) {
>         // Accepts inIntParam as input
>      // Updates instance variables in outIntParam and outDoubleParam
>     // Returns an int
>     …
>     }
>     …
> }
> 
> Kindly suggest if there is a preferred way to realize the same.

Robert Klemme already told you about the goofiness of using instance variables for anything other than instance state. You can use an aggregate type as a return type:

public class SomeUseful
{
  public static class OutParams<T. U>
  {
    public T paramT;
    public T anotherT;
    public U paramU;
  }

  public OutParams<Integer, Double> calculateParams(int input)
  {
      OutParams<Integer, Double> out = new OutParams<>();
      out.paramT = calculateFirst(input);
      out.anotherT = calculateSecond(input);
      out.paramU = calculateThird(input);
      return out;
  }
}

-- 
Lew

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web