Foggy day

Android(Java) - composite design pattern example 본문

Android

Android(Java) - composite design pattern example

jinhan38 2020. 12. 26. 20:53

This post is the example of composite design pattern without detail explanation.

If you know definition or concept of composite design pattern, I recommend searching other articles on Google.

 

 

Some explanations are commented out in the code.

 

 

 

 

Classes used

 

1. AbstractExpression

abstract public class AbstractExpression {
    public abstract int operate();
}

 

2. AbstractOperationExpression

abstract public class AbstractOperationExpression extends AbstractExpression {
    protected ArrayList<AbstractExpression> operandList = new ArrayList<>();

    public final void addOperandExpression(AbstractExpression operandExpression){
        operandList.add(operandExpression);
    }
    
}

 

3. NumberExpression

public class NumberExpression extends AbstractExpression {

    private int value;

    public NumberExpression(int value) {
        this.value = value;
    }

    @Override
    public int operate() {
        return value;
    }
}

 

4. Calculator

public class Calculator {

    private AbstractExpression expression;

    public void setExpression(AbstractExpression expression) {
        this.expression = expression;
    }
    public Calculator() {
        super();
    }

    public int calculate() {
        return expression.operate();
    }

}

 

5. AddOperationExpression

public class AddOperationExpression extends AbstractOperationExpression{

    @Override
    public int operate() {
        AbstractExpression firstOperandExpression = operandList.get(0);
        AbstractExpression secondOperandExpression = operandList.get(1);

        int firstResult = firstOperandExpression.operate();
        int secondResult = secondOperandExpression.operate();
        return firstResult + secondResult;
    }
}

 

6. SubtractOperationExpression

public class SubtractOperationExpression extends AbstractOperationExpression {

    @Override
    public int operate() {
        AbstractExpression firstOperandExpression = operandList.get(0);
        AbstractExpression secondOperandExpression = operandList.get(1);

        int firstResult = firstOperandExpression.operate();
        int secondResult = secondOperandExpression.operate();
        return firstResult - secondResult;
    }
}

 

7. MultiplyOperationExpression

public class MultiplyOperationExpression extends AbstractOperationExpression{
    @Override
    public int operate() {
        AbstractExpression firstOperandExpression = operandList.get(0);
        AbstractExpression secondOperandExpression = operandList.get(1);

        int firstResult = firstOperandExpression.operate();
        int secondResult = secondOperandExpression.operate();
        return firstResult * secondResult;
    }
}

 

8. DivideOperationExpression

public class DivideOperationExpression extends AbstractOperationExpression {
    @Override
    public int operate() {
        AbstractExpression firstOperandExpression = operandList.get(0);
        AbstractExpression secondOperandExpression = operandList.get(1);

        int firstResult = firstOperandExpression.operate();
        int secondResult = secondOperandExpression.operate();
        return firstResult / secondResult;
    }
}

 

9. CompositeClientJava

public class CompositeClientJava extends CommonActivity {

    private static final String TAG = "CompositeClientJava";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
            setContentView(R.layout.composite_client_java);

        Calculator calculator = new Calculator();
        int firstNumber = 100;
        int secondNumber = 20;


        //NumberExpression is Class that save a value and is used to parameter
        NumberExpression firstNumberEquation = new NumberExpression(firstNumber);
        NumberExpression secondNumberEquation = new NumberExpression(secondNumber);

        //The variable type of setExpression method is AbstractExpression class.
        //Because NumberExpression extends AbstractExpression class, it can be used as a parameter of setExpression function
        calculator.setExpression(firstNumberEquation);
        Log.d(TAG, "onCreate: firstNumber : " + calculator.calculate());

        calculator.setExpression(secondNumberEquation);
        Log.d(TAG, "onCreate: secondNumberEquation : " + calculator.calculate());


        //AddOperationExpression class extends AbstractOperationExpression class.
        //Upcast AddOperationExpression to AbstractOperationExpression

        AbstractOperationExpression addOperationExpression = new AddOperationExpression();
        //parameter type of addOperandExpression function is AbstractExpression class.
        //If call addOperandExpression function, the value is added to operandList in AbstractOperationExpression class
        addOperationExpression.addOperandExpression(firstNumberEquation);
        addOperationExpression.addOperandExpression(secondNumberEquation);

        //Now, you can get calculated results using calculator class
        calculator.setExpression(addOperationExpression);
        Log.d(TAG, "onCreate: add Result : " + calculator.calculate());

        AbstractOperationExpression subtractOperationExpression = new SubtractOperationExpression();
        subtractOperationExpression.addOperandExpression(firstNumberEquation);
        subtractOperationExpression.addOperandExpression(secondNumberEquation);
        calculator.setExpression(subtractOperationExpression);
        Log.d(TAG, "onCreate: subtract result : " + calculator.calculate());


        AbstractOperationExpression multiplyOperationExpression = new MultiplyOperationExpression();
        multiplyOperationExpression.addOperandExpression(firstNumberEquation);
        multiplyOperationExpression.addOperandExpression(secondNumberEquation);
        calculator.setExpression(multiplyOperationExpression);
        Log.d(TAG, "onCreate: multiply result : " + calculator.calculate());

        AbstractOperationExpression divideOperationExpression = new DivideOperationExpression();
        divideOperationExpression.addOperandExpression(firstNumberEquation);
        divideOperationExpression.addOperandExpression(secondNumberEquation);
        calculator.setExpression(divideOperationExpression);
        Log.d(TAG, "onCreate: divide result : " + calculator.calculate());

    }

}

 

 

 

result