June 2018
Intermediate to advanced
280 pages
7h 46m
English
The following code models an arithmetic expression calculator. The expression is constructed as a composite and has only one method—getValue. This gives the current value; for leaf, it is the leaf numeric value, and for composite nodes, it is the children-composed value:
package gof.structural.composite;publicclass Main{ publicstaticvoid main (String[] args) throws java.lang.Exception { ArithmeticComposite expr = new MinusOperand( new PlusOperand(new NumericValue(1), new NumericValue(4)), new NumericValue(2)); System.out.printf("Value equals %dn", expr.getValue()); }}
The client code creates a (1+4)-2 arithmetic expression and prints its value, as shown in the following code:
interface ArithmeticComposite { publicint getValue(); ...Read now
Unlock full access