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


Groups > comp.lang.java.programmer > #33795

Re: To deprecate or not

From Arne Vajhøj <arne@vajhoej.dk>
Newsgroups comp.lang.java.programmer
Subject Re: To deprecate or not
Date 2016-02-13 11:34 -0500
Organization Aioe.org NNTP Server
Message-ID <n9nlue$10li$2@gioia.aioe.org> (permalink)
References <d0bb6e79-0125-4075-880a-5f4988370fee@googlegroups.com>

Show all headers | View raw


On 2/12/2016 4:16 AM, mike wrote:
> I have an interface call it InterfaceTop that looks like this:
>
> public InterfaceTop {
>    public String getValue(String arg1);
>
> }
>
> Then we have two sub interfaces call them InterfaceSub1 and InterfaceSub2.
>
> They both extend IntefaceTop.
>
> Now we discovered that the method in Inteface top produces erroneous output in
> in implementation of InterfaceSub1. We need an additional parameter to get value:
>
> public String getValue(String arg1, String arg2);
>
> As I see it we have two alternatives:
>
> 1.Add 'public String getValue(String arg1, String arg2);' in InterfaceTop and
 > @deprecate public String getValue(String arg1);
>
> A consequence of this will be that I need to replace methods in implemetation. using
 > InterfaceSub2. And handle a null or empty String for arg2. But we 
still have a
 > common interface.
>
> 2. Add 'public String getValue(String arg1, String arg2);' to InterfaceSub1.
> I still need to implement both methods in implementation. Maybe let the "faulty" method
 > throw UnsupportedOperationException.
>
> What is your view on this?

Your option #1 is:

package february;

public class OptionOne {
	public static interface InterfaceTop {
		@Deprecated
		public String getValue(String arg1);
		public String getValue(String arg1, String arg2);
	}
	public static interface InterfaceSub1 extends InterfaceTop {
	}
	public static interface InterfaceSub2 extends InterfaceTop {
	}
	public static class ClassImpl1 implements InterfaceSub1 {
		@Override
		public String getValue(String arg1) {
			throw new UnsupportedOperationException("Missing argument");
		}

		@Override
		public String getValue(String arg1, String arg2) {
			return arg1 + arg2;
		}
	}
	public static class ClassImpl2 implements InterfaceSub2 {
		@Override
		public String getValue(String arg1) {
			return arg1;
		}
		@Override
		public String getValue(String arg1, String arg2) {
			return getValue(arg1); // I think this is convenient
		}
	}
	public static void main(String[] args) {
		InterfaceTop i1 = new ClassImpl1();
		System.out.println(i1.getValue("ABC", "DEF"));
		InterfaceTop i2 = new ClassImpl2();
		System.out.println(i2.getValue("ABC", null));
	}
}

Your option #2 is:

package february;

public class OptionTwo {
	public static interface InterfaceTop {
		public String getValue(String arg1);
	}
	public static interface InterfaceSub1 extends InterfaceTop {
		@Deprecated // I think this should be added
		public String getValue(String arg1);
		public String getValue(String arg1, String arg2);
	}
	public static interface InterfaceSub2 extends InterfaceTop {
	}
	public static class ClassImpl1 implements InterfaceSub1 {
		@Override
		public String getValue(String arg1) {
			throw new UnsupportedOperationException("Missing argument");
		}

		@Override
		public String getValue(String arg1, String arg2) {
			return arg1 + arg2;
		}
	}
	public static class ClassImpl2 implements InterfaceSub2 {
		@Override
		public String getValue(String arg1) {
			return arg1;
		}
	}
	public static void main(String[] args) {
		InterfaceSub1 i1 = new ClassImpl1();
		System.out.println(i1.getValue("ABC", "DEF"));
		InterfaceSub2 i2 = new ClassImpl2();
		System.out.println(i2.getValue("ABC"));
	}
}

I have an option #3 that uses a Java 8 feature to improve option #1:

package february;

public class OptionThree {
	public static interface InterfaceTop {
		public String getValue(String arg1);
		public default String getValue(String arg1, String arg2) {
			return getValue(arg1);
		}
	}
	public static interface InterfaceSub1 extends InterfaceTop {
	}
	public static interface InterfaceSub2 extends InterfaceTop {
	}
	public static class ClassImpl1 implements InterfaceSub1 {
		@Override
		public String getValue(String arg1) {
			throw new UnsupportedOperationException("Missing argument");
		}

		@Override
		public String getValue(String arg1, String arg2) {
			return arg1 + arg2;
		}
	}
	public static class ClassImpl2 implements InterfaceSub2 {
		@Override
		public String getValue(String arg1) {
			return arg1;
		}
	}
	public static void main(String[] args) {
		InterfaceTop i1 = new ClassImpl1();
		System.out.println(i1.getValue("ABC", "DEF"));
		InterfaceTop i2 = new ClassImpl2();
		System.out.println(i2.getValue("ABC"));
	}
}

I also have an option #4:

package february;

public class OptionFour {
	public static interface InterfaceTop {
		public String getValue(String... args); // note that it can be array 
or Map or custom interface/class
	}
	public static interface InterfaceSub1 extends InterfaceTop {
	}
	public static interface InterfaceSub2 extends InterfaceTop {
	}
	public static class ClassImpl1 implements InterfaceSub1 {
		@Override
		public String getValue(String... args) {
			if(args.length == 2) {
				return args[0] + args[1];
			} else {
				throw new UnsupportedOperationException("Invalid number of arguments");
			}
		}
	}
	public static class ClassImpl2 implements InterfaceSub2 {
		@Override
		public String getValue(String... args) {
			if(args.length == 1) {
				return args[0];
			} else {
				throw new UnsupportedOperationException("Invalid number of arguments");
			}
		}
	}
	public static void main(String[] args) {
		InterfaceTop i1 = new ClassImpl1();
		System.out.println(i1.getValue("ABC", "DEF"));
		InterfaceTop i2 = new ClassImpl2();
		System.out.println(i2.getValue("ABC", null));
	}
}

My preferences are:

if time to rework things properly then
     go for option #4
else
    if Java 8 then
       go for option #3
    else
       go for option #2
    end if
end if

I would never go for option #1.

Arne


Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

To deprecate or  not mike <mikaelpetterson@hotmail.com> - 2016-02-12 01:16 -0800
  Re: To deprecate or  not Eric Douglas <e.d.programmer@gmail.com> - 2016-02-12 05:59 -0800
    Re: To deprecate or not Arne Vajhøj <arne@vajhoej.dk> - 2016-02-13 11:02 -0500
      Re: To deprecate or not Eric Douglas <e.d.programmer@gmail.com> - 2016-02-15 05:12 -0800
        Re: To deprecate or not Arne Vajhøj <arne@vajhoej.dk> - 2016-02-15 11:42 -0500
  Re: To deprecate or not Arne Vajhøj <arne@vajhoej.dk> - 2016-02-13 11:34 -0500
  Re: To deprecate or  not Andreas Leitgeb <avl@logic.at> - 2016-02-15 00:53 +0000

csiph-web