Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Assuming A1 is similar to B1, etc., I would implement it like this and giving them the same operation names:</p> <pre><code>public enum Customer { CUSTA, CUSTB; public void operation1(){ switch(this){ case CUSTA: // do A thing; break; case CUSTB: // do it the B way break; } } } </code></pre> <p>Could that fit into what you're trying to do?</p> <p>Of course if you're going to use an enum then you'll have to update the code when you get a new Customer type, but you'll probably have to update code anyway. One other and possibly better approach is to define an interface and use a Factory pattern; it might come out cleaner:</p> <pre><code>public interface Customer { void operation1(); } class CustomerFactory{ public static Customer getByName(String cust){ if(cust.equals("CUSTA")){ return new CustomerA(); } if(cust.equals("CUSTB")){ return new CustomerB(); } throw new IllegalArgumentException( "Customer type " + cust + " not supported."); } } class CustomerA implements Customer{ @Override public void operation1(){ /* A's implementation */ } } class CustomerB implements Customer{ @Override public void operation1(){ /* B's implementation */ } } </code></pre> <p>One last thing I would add that would let you add implementations of Customer that will load with no code changes so long as they are on your classpath is to use reflection and pass in class names to the Factory:</p> <pre><code>package test; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; public class CustomerTest{ public static void main(String[] arg){ CustomerFactory.getByName("test.CustomerA").operation1(); CustomerFactory.getByName("test.CustomerB").operation1(); CustomerFactory.getByName("test.CustomerC").operation1(); } } interface Customer { void operation1(); } class CustomerFactory{ public static Customer getByName(final String cust){ try{ Class&lt;? extends Customer&gt; customerSubclass = Class.forName(cust).asSubclass(Customer.class); Constructor&lt;? extends Customer&gt; constructor = customerSubclass.getDeclaredConstructor(new Class[0]); return constructor.newInstance(new Object[0]); } catch(ClassNotFoundException e){ System.err.println("Trouble finding .class file for class "+cust+". "+e); } catch(ClassCastException e){ System.err.println("Found .class file for "+cust+" but it doesn't implement Customer."+" "+e); } catch(NoSuchMethodException e){ System.err.println("Class "+cust+" doesn't provide a no-arg constructor. :("+" "+e); } catch(IllegalAccessException e){ System.err.println("No-arg constructor isn't accessible. :("+" "+e); } catch(InstantiationException e){ System.err.println("Couldn't instantiate?"+" "+e); } catch(InvocationTargetException e){ System.err.println("Umm... something else broke."+" "+e); } throw new IllegalArgumentException( "Customer type " + cust + " not supported."); } } class CustomerA implements Customer{ @Override public void operation1(){ /* A's implementation */ System.out.println("I'm an A doing operation1!"); } } class CustomerB implements Customer{ @Override public void operation1(){ /* B's implementation */ System.out.println("I'm a B doing operation1!"); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload