将枚举方法链接到客户端类的成员变量

发布于 2024-08-04 09:49:01 字数 2519 浏览 8 评论 0原文

以下枚举结构执行某些操作,同时对客户端类保持不可知(出于封装原因)

    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
    }

客户端类

    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();
      }
}

我的问题是如何链接属于 MyEnum.A 和 MyEnum.C 的 method1() 的具体实现,以便它们在 Client 上操作类成员 varY,MyEnum.B 和 MyEnum.D 的 method1() 实现仅对 Client 类成员变量 varX 进行操作。

例如,在 main() 方法中:

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

因此,根据当前枚举状态,上述语句应仅对 varX 或 varY 进行操作并更改 aClient 上的状态。

假设:

aClient.performMethod1(MyEnum.A);

varY 表示的 aClient 的状态应更改为 varY+2。 varX 应保持不变。

通过简单的命名约定可以看到,此时 MyEnum 中的 varX 没有以任何方式链接到 Client 类中的 varX。我的想法倾向于变量的类型。由于我正在使用基元(int),因此无法区分。

您是否建议我在 Client 类中为每个 varX 和 varY 创建不同的自定义类型(以某种方式包装它们?)?

我希望我的问题不要太冗长。如果我不清楚,请告诉我。非常感谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

宣告ˉ结束 2024-08-11 09:49:01

您希望根据 MyEnum 的不同值,Client 发生不同的事情。如果 MyEnum 必须与客户端无关,则 Client 必须识别 MyEnum 的不同状态(Client 中的开关) .performMethod1)或单独的处理器类将实现该逻辑。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文