Multiple Buttons, 1 ClickListener using generics, Strategy Pattern - All buttons only do the same operation

淺唱寂寞╮ 提交于 2019-12-13 04:19:10

问题


I have been looking at different ways of making Listeners in Android. I encapsulated the listener in a separate class called Click. I have also used the strategy pattern to encapsulate logic and that is interfaced with the enum type BinaryOperator. What I have is a simple calculator that you probably seen online already - 2 input numbers, 1 result, and 4 buttons (+, -, *, and /).

There is an error in my code, because no matter what button I press, it only does the division operation. I added Generics, and this issue may be from that.

By the way, I am trying different ways to create Listeners. I am not looking for "the best way", or "the standard way", I am looking to learn different things, so don't attack the method I used to create Listeners.

MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button btnAdd = (Button) findViewById(R.id.btnAddition);
    Button btnSub = (Button) findViewById(R.id.btnSubtract);
    Button btnMul = (Button) findViewById(R.id.btnMultiple);
    Button btnDiv = (Button) findViewById(R.id.btnDivide);

    txtNumber1 = (EditText) findViewById(R.id.txtNumber1);
    txtNumber2 = (EditText) findViewById(R.id.txtNumber2);
    txtResult = (EditText) findViewById(R.id.txtResult);

    btnAdd.setOnClickListener(new Click<>(Add.OPERATION).listenerOn(this));
    btnSub.setOnClickListener(new Click<>(Subtract.OPERATION).listenerOn(this));
    btnDiv.setOnClickListener(new Click<>(Divide.OPERATION).listenerOn(this));
    btnMul.setOnClickListener(new Click<>(Multiply.OPERATION).listenerOn(this));

}

Click.java

public class Click<T extends Operation> {

    private T operation;

    private View.OnClickListener listener;

    public Click(T operation) {

        this.operation = operation;

    }

    public View.OnClickListener listenerOn(final MainActivity UI) {

        if (listener != null) return listener;

        listener = new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                double total, num1, num2;

                num1 = Double.parseDouble(UI.txtNumber1.getText().toString());
                num2 = Double.parseDouble(UI.txtNumber2.getText().toString());

                total = operation.execute(num1, num2);

                UI.txtResult.setText(Double.toString(total) + " -v5");

            }
        };

        return listener;

    }

}

BinaryOperation.java

Where Add.OPERATION, Subtract.OPERATION, etc... come from

public enum BinaryOperation {

    Add (new Operation(new Add())),
    Subtract (new Operation(new Subtract())),
    Multiply (new Operation(new Multiple())),
    Divide (new Operation(new Divide()));

    public final Operation OPERATION;

    private BinaryOperation(Operation operation) {

        OPERATION = operation;

    }

}

Operation.java

public class  Operation {

    private static IOperationStrategy strategy;

    public Operation(IOperationStrategy strategy) { this.strategy = strategy; }

    public double execute(double a, double b) { return strategy.executeOperation(a, b); }

}

IOperationStrategy.java

public interface IOperationStrategy {

    public double executeOperation(double a, double b);

}

Add.java (One of the 4)

public class Add implements IOperationStrategy {

    @Override
    public double executeOperation(double a, double b) {

        return a + b;

    }
}

来源:https://stackoverflow.com/questions/24615590/multiple-buttons-1-clicklistener-using-generics-strategy-pattern-all-buttons

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!