简体   繁体   English

策略模式:如何使用构造函数作为枚举来创建类?

[英]Strategy Pattern: How to make classes with constructors as enums?

I'm creating a Strategy pattern in Java. 我正在用Java创建策略模式。

This is my Strategy Interface ActivationStrategy.java : 这是我的策略界面ActivationStrategy.java

public interface Strategy {
    public void strategyAlgo(JTextField textField);
}

I have different classes which implement the Interface. 我有实现接口的不同类。 Those classes have a constructor because it's necessary to passing values. 这些类具有构造函数,因为必须传递值。 For example: 例如:

FirstStrategy.java FirstStrategy.java

public class FirstStrategy implements Strategy{
    private JComboBox<String> _comboBox;

    public FirstStrategy(JComboBox<String> comboBox) {
        _comboBox = comboBox;
    }

    public void strategyAlgo(JTextField textField) {
        textField.addKeyListener(new myKeyHandler(_comboBox, textField));
    }

SecondStrategy.java SecondStrategy.java

public class SecondStrategy implements Strategy{
    private JPanel _panel;

    public secondStrategy(JPanel panel) {
        _panel = panel;
    }

    public void strategyAlgo(JTextField textField) {
        addActionHandlerToButton(addButton(_panel));
    }

    public JButton addButton(JPanel panel) {
        JButton button = new JButton("Click me");
        panel.add(button);
        return button;
    }

    public void addActionHandlerToButton(JButton okButton) {
        Action action = new AbstractAction(){    
            public void actionPerformed(ActionEvent e) {
                System.out.println("Hi");           
            }
        };
        button.addActionListener(action);           
    }
}

Now, I would like to make the classes as enums. 现在,我想将这些类作为枚举。 Something like: 就像是:

public enum strategies {

    FIRSTSTRATEGIE(){
      @Override
        public void strategyAlgo() {
        ...
        }
    },
    SECONDSTRATEGY(){
      @Override
        public void strategyAlgo() {
        ...
        }
        public JButton addButton(JPanel panel) {
        ...
        }
        public void addActionHandlerToButton(JButton okButton) {
        ...
        }
    };

    public abstract void strategyAlgo();

}

But what about the constructors? 但是构造函数呢?

My criteria when enum s are a viable choice? enum是可行的选择吗?

  1. When the number of instances are determined at compile time. 在编译时确定实例数时。
  2. When the state of each instance is determined at compile time. 在编译时确定每个实例的状态时。
  3. When your instances do not hold mutable state. 当您的实例不处于可变状态时。

In your case criterion 2. is not true . 在您的情况下,标准2.是不true Hence in my opinion you should not model your strategies as enum s. 因此,我认为您不应将策略建模为enum

You cannot create an arbitrary instance of an enum at runtime. 您不能在运行时创建枚举的任意实例。 Therefore, you cannot pass anything to an enum's constructor that is not known at compile time. 因此,您不能将任何在编译时未知的东西传递给枚举的构造函数。 You can pass dynamic arguments by changing list of arguments in your abstract method. 您可以通过更改抽象方法中的参数列表来传递动态参数。

You can create a constructor in an Enum but it can be private only, so you cannot use it to create an arbitrary instance at runtime. 您可以在Enum中创建构造函数,但只能是private ,因此您不能在运行时使用它来创建任意实例。 This way it is guaranteed that there is only one instance of a particular enum value. 这样可以保证只有一个特定枚举值的实例。

For example: 例如:

public enum Suit {
  SPADES("spades"),
  HEARTS("hearts"),
  CLUBS("clubs"),
  DIAMONDS("diamonds");

  private String label;

  private Suit(String label) {
    this.label = label;
  }

}

BTW, private keyword is optional for a constructor in case of an enum . 顺便说一句,对于enumprivate关键字对于构造函数是可选的。 It's private by default. 默认情况下是private的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM