views:

90

answers:

1

Hello,

The following enum structure performs certain operations while remaining agnostic to the client class (for encapsulation reasons)

    public enum MyEnum implements Commands{
    A{
     public int method1(int varY) {   
      return varY+2;
     }

     public MyEnum method2(){
                       return MyEnum.B;
              }

             //...other methods implementing Commands interface
    },
    B{

     public int method1(int varX) {
      return varX-2;
     }


               public MyEnum method2(){
                       return MyEnum.C;
              }

     //...other methods implementing Commands interface
    },
    C{

     public int method1(int varY) {
      return varY-2;
     }

     public MyEnum method2(){
                        return MyEnum.D;
               }


              //...other methods implementing Commands interface
    },
    D{

     public int method1(int varX) {
      return varX+2;
     }

     public MyEnum method2(){
                      return MyEnum.A;
                 }


             //...other methods implementing Commands interface
    }

The client class

    public class Client {
    private int varX;
    private int varY;
    private MyEnum enum;

    MyEnum getEnum(){
     return enum;
    }

    int varX(){
     return varX;
    }

    int getVarY(){
     return varY;
    }

    public Client(int varX, int varY, MyEnum enum){
     this.varX = varX;
     this.varY = varY;
     this.enum = enum;
    }

    public void performMethod1(MyEnum enum){  
     varX = getEnum().method1(getVarX()); //???
     varY = getEnum().method1(getVarY()); //???
    }

     public void performMethod2(...){
                enum = getEnum().method2();
      }
}

My question is how to link the specific implementations of method1() belonging to MyEnum.A and MyEnum.C so that they operate on Client class member varY, and the method1() implementations of MyEnum.B and MyEnum.D to operate only on Client class member variable varX.

For example within a main() method:

Client aClient = new Client(aVarX, aVarY, anEnum);    
aClient.performMethod1(aClient.getEnum());

So, depending in the current enum state, the above statement should operate only on varX or varY and change the state on aClient.

Let's say for:

aClient.performMethod1(MyEnum.A);

The state of aClient represented by varY should be changed to varY+2. varX should remain unchanged.

As you can see by the simple naming convention, at this time varX in MyEnum is not linked to varX in Client class in any way. My thoughts lean toward the type of the variables. Since I am working with primitives (int) there is no way to distinguish..

Would you recommend I create different custom types for each of the varX and varY (wrap them somehow?) in Client class?

I hope I am not too verbose with the question. Please let me know in case I am not being clear with it. Many thanks.

+1  A: 

You want different things to happen to the Client depending on different values of the MyEnum. If the MyEnum has to be client agnostic then either the Client will have to recognize different states of the MyEnum (a switch in Client.performMethod1) or a separate processor class will have implement that logic.

Hemal Pandya
I see the point, but the objective is to avoid switch/if statements in the Client. The enum has all the alternatives already built-inFor example in another method not shown in the MyEnum, let's say method2, I could operate on the MyEnum cases itself. And then, in Client I could wrap this in performMethod2(MyEnum enum) and change the enum state of Client
denchr
I edited my original post to include method2() in MyEnum. It is better shown now that, Client has the ability to simply delegate to MyEnum by performMethod2(), keep its state and behavior encapsulated, while MyEnum is basically Client agnostic - no references to any objects that use its enumerations. -- So since this is possible with a MyEnum variable, it should be possible with the varX and varY primitives, right?
denchr