-1
\$\begingroup\$

Assume that we have a class with multiple constructors which may look like the following:

public class C 
{
      private String id;
      private int a;
      private int b;
      private int c;

     C(String id, int a){}
     C(String id, int a, int b){}
     C(String id, int a, int b, int c){}
     C(C[] cs){}

     // This method will be used to print objects created by C(String id, int a)
     public void printC1() {}

     // This method will be used to print objects created by C(String id, int a, int b)
     public void printC2() {}

     // This method will be used to print objects created by C(String id, int a, int b, int c)
     public void printC3() {}
}

The methods printC1, printC2, printC3 have a lot of similarities. Could anyone support me with some hints how I should redefine the class C in order to follow design principles like DRY, ...

\$\endgroup\$

closed as off-topic by 200_success, Sᴀᴍ Onᴇᴌᴀ, Stephen Rauch, Dannnno, Roland Illig May 18 '18 at 20:15

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – 200_success, Sᴀᴍ Onᴇᴌᴀ, Stephen Rauch, Dannnno, Roland Illig
If this question can be reworded to fit the rules in the help center, please edit the question.

  • \$\begingroup\$ I'm afraid this question does not match what this site is about. Code Review is about improving existing, working code. The example code that you have posted is not reviewable in this form because it leaves us guessing at your intentions. Unlike Stack Overflow, Code Review needs to look at concrete code in a real context. Please see Why is hypothetical example code off-topic for CR? \$\endgroup\$ – Dannnno May 18 '18 at 19:54
  • \$\begingroup\$ Unfortunately, we don't provide code, we review code you've written. Depending on your problem, another site of the StackExchange network can help you. Please see our help center for more information. \$\endgroup\$ – Dannnno May 18 '18 at 19:55
  • \$\begingroup\$ Noting the above comments about reviewing code - not providing it - I offer the following hint: research "method overloading" for java. Or if the print method depends on how the constructor is invokes, set a private flag and then use a series of conditionals in the single print method. Try to encapsulate and hide as many decisions as possible from your user. This is not an answer because your question does not fit within the spirit of CodeReview. \$\endgroup\$ – AJD May 18 '18 at 21:41
2
\$\begingroup\$

Use varargs in your constructor. Have one print method that prints any of a, b, c that exist. Also, why are you printing from a method on C? That's probably a bad design.

public final class C {

    private String id;
    private int[] values;

    public C(final String id, final int... values) {
        this.id = id;

        if ((values == null) || (values.length > 3)) { /* do something */ }
        this.values = Arrays.copyOf(values, values.length);
    }

    public void print() {
        for (final int value : values) { /* do something */ }
    }
\$\endgroup\$
0
\$\begingroup\$

You can write one part of code in printC1 and then just call it from printC2. Add one line to print additional information In C2 and from C3 just call printC2.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.