/composite-design-pattern-arithmetic-expression

Use design patterns to transform arithmetic expressions into a well-designed structure to calculate the answers.

Primary LanguagePHP

Challenge

Use design patterns to transform arithmetic expressions into a well-designed structure to calculate the answers.

For example:

Usage & Test

    $expression =
            new Divider(
                new Multiplier(
                    new Negate(
                        new Constant(5.0)
                        ),
                    new Divider(
                        new Constant(9.0), 
                        new Constant(6.0)
                        )
                ),
                new Adder(
                    new Constant(7.0),
                    new Subtracter(
                            new Constant(2.0), 
                            new Constant(1.5)
                            )
                )
            );
        

Solution

Use Composite design pattern:

Transform the question into a Composite structure: