Showing posts with label Composite. Show all posts
Showing posts with label Composite. Show all posts

Saturday, January 06, 2024

Composite Design Pattern using PHP

Teaching the Composite design pattern in PHP is a great way to introduce students to both advanced object-oriented programming concepts and design patterns. The Composite pattern is particularly useful for treating individual objects and compositions of objects uniformly. In the Composite pattern, you typically have a component interface, leaf objects, and composite objects. The component interface defines default behavior for all objects, leaf objects perform actual operations, and composite objects store child components (which can be leaf or composite objects). Let's create a simple example: a file system with directories and files. Directories can contain files or other directories. Step 1: Define the Component Interface FileComponent.php

Step 2: Create Leaf Objects
FileLeaf.php
name = $name;
        $this->size = $size;
    }

    public function getSize() {
        return $this->size;
    }

    public function getName() {
        return $this->name;
    }
}
Step 3: Create Composite Objects DirectoryComposite.php
name = $name;
        $this->children = [];
    }

    public function add(FileComponent $component) {
        $this->children[] = $component;
    }

    public function getSize() {
        $totalSize = 0;
        foreach ($this->children as $child) {
            $totalSize += $child->getSize();
        }
        return $totalSize;
    }

    public function getName() {
        return $this->name;
    }
}
Step 4: Demonstrate the Composite Pattern index.php
add($file1);
$directory->add($file2);

// Create a subdirectory and add it to the directory
$subdirectory = new DirectoryComposite("Subdirectory");
$subdirectory->add(new FileLeaf("SubFile1.txt", 110));
$directory->add($subdirectory);

// Display the size of the directory
echo "Total Size of '" . $directory->getName() . "': " . $directory->getSize() . " bytes";
Order of Creation
FileComponent.php:
Define the component interface. FileLeaf.php:
Implement the leaf objects. DirectoryComposite.php:
Implement the composite objects. index.php:
Demonstrate the usage of the Composite pattern. Expected Output When you run index.php,
you should see the total size of the directory, which includes the sizes of all files in it and its subdirectories.
The output will be:
Total Size of 'Directory': 630 bytes

Wednesday, April 21, 2021

Composite pattern PHP

This example illustrates the structure of the Composite design pattern and focuses on the following questions: What roles do these classes play? In what way the elements of the pattern are related?
  1. What classes does it consist of?
  2. What roles do these classes play?
  3. In what way the elements of the pattern are related?

The base Component can declare an interface for setting and accessing a parent of the component in a tree structure. It can also provide some default implementation for these methods.

In some cases, it would be beneficial to define the child-management operations right in the base Component class. This way, you won't need to expose any concrete component classes to the client code, even during the object tree assembly. The downside is that these methods will be empty for the leaf-level components.

You can provide a method that lets the client code figure out whether a component can bear children. The base Component may implement some default behavior or leave it to concrete classes (by declaring the method containing the behavior as abstract).
abstract class Component
{
    protected $parent;

    public function setParent(Component $parent)
    {
        $this->parent = $parent;
    }

    public function getParent(): Component
    {
        return $this->parent;
    }

    public function add(Component $component): void { }
    public function remove(Component $component): void { }

    public function isComposite(): bool
    {
        return false;
    }

    abstract public function operation(): string;
}
The Composite class represents the complex components that may have children. Usually, the Composite objects delegate the actual work to their children and then sum-up the result. A composite object can add or remove other components (both simple or complex) to or from its child list.

The Composite executes its primary logic in a particular way. It traverses recursively through all its children, collecting and summing their results. Since the composite's children pass these calls to their children and so forth, the whole object tree is traversed as a result.
include_once ('Component.php');

class Composite extends Component
{
    protected $children;

    public function __construct()
    {
        $this->children = new \SplObjectStorage();
    }

    public function add(Component $component): void
    {
        $this->children->attach($component);
        $component->setParent($this);
    }

    public function remove(Component $component): void
    {
        $this->children->detach($component);
        $component->setParent(null);
    }
    public function isComposite(): bool
    {
        return true;
    }

    public function operation(): string
    {
        $results = [];
        foreach ($this->children as $child) {
            $results[] = $child->operation();
        }

        return "Branch(" . implode("+", $results) . ")";
    }
}
The Leaf class represents the end objects of a composition. A leaf can't have any children.

Usually, it's the Leaf objects that do the actual work, whereas Composite objects only delegate to their sub-components.
class Leaf extends Component
{
    public function operation(): string
    {
        return "Leaf";
    }
}
The client code works with all of the components via the base interface. This way the client code can support the simple leaf components as well as the complex composites.
include_once ('Composite.php');
include_once ('Leaf.php');

function clientCode(Component $component)
{
    // ...

    echo "RESULT: " . $component->operation();

    // ...
}

$simple = new Leaf();
echo "Client: I've got a simple component:"<br/>";
clientCode($simple);
echo "\n\n";

$tree = new Composite();
$branch1 = new Composite();
$branch1->add(new Leaf());
$branch1->add(new Leaf());
$branch2 = new Composite();
$branch2->add(new Leaf());
$tree->add($branch1);
$tree->add($branch2);
echo "Client: Now I've got a composite tree:"<br/>";
clientCode($tree);
echo "<br/>"<br/>";

Thanks to the fact that the child-management operations are declared in the base Component class, the client code can work with any component, simple or complex, without depending on their concrete classes.
function clientCode2(Component $component1, Component $component2)
{
    // ...

    if ($component1->isComposite()) {
        $component1->add($component2);
    }
    echo "RESULT: " . $component1->operation();

    // ...
}

echo "Client: I don't need to check the components classes even when managing the tree:\n";
clientCode2($tree, $simple);
Now let's run this in a browser and we whould get:
Client: I've got a simple component:
RESULT: Leaf Client: Now I've got a composite tree:
RESULT: Branch(Branch(Leaf+Leaf)+Branch(Leaf))

Client: I don't need to check the components classes even when managing the tree: RESULT: Branch(Branch(Leaf+Leaf)+Branch(Leaf)+Leaf)

The Ray Code is AWESOME!!!
wikipedia

Find Ray on:

facebook
youtube
The Ray Code
Ray Andrade

Tuesday, April 20, 2021

Composite pattern java

In this tutoraial posting we will use the java.awt library.

Composite is a structural design pattern that allows composing objects into a tree-like structure and work with the it as if it was a singular object.

Composite became a pretty popular solution for the most problems that require building a tree structure. Composite’s great feature is the ability to run methods recursively over the whole tree structure and sum up the results.

We will be using the java.awt library in our demo. Let's create a package first. The name of the package will be shapes. Inside of this package we place the *interface* Shape.
import java.awt.*;

public interface Shape {
    int getX();
    int getY();
    int getWidth();
    int getHeight();
    void move(int x, int y);
    boolean isInsideBounds(int x, int y);
    void select();
    void unSelect();
    boolean isSelected();
    void paint(Graphics graphics);
}
Let's also create a base class we call BaseShape and it will implement Shape.
import java.awt.*;

abstract class BaseShape implements Shape {
    public int x;
    public int y;
    public Color color;
    private boolean selected = false;

    BaseShape(int x, int y, Color color) {
        this.x = x;
        this.y = y;
        this.color = color;
    }

    @Override
    public int getX() {
        return x;
    }

    @Override
    public int getY() {
        return y;
    }

    @Override
    public int getWidth() {
        return 0;
    }

    @Override
    public int getHeight() {
        return 0;
    }

    @Override
    public void move(int x, int y) {
        this.x += x;
        this.y += y;
    }

    @Override
    public boolean isInsideBounds(int x, int y) {
        return x > getX() && x < (getX() + getWidth()) &&
                y > getY() && y < (getY() + getHeight());
    }

    @Override
    public void select() {
        selected = true;
    }

    @Override
    public void unSelect() {
        selected = false;
    }

    @Override
    public boolean isSelected() {
        return selected;
    }

    void enableSelectionStyle(Graphics graphics) {
        graphics.setColor(Color.LIGHT_GRAY);

        Graphics2D g2 = (Graphics2D) graphics;
        float dash1[] = {2.0f};
        g2.setStroke(new BasicStroke(1.0f,
                BasicStroke.CAP_BUTT,
                BasicStroke.JOIN_MITER,
                2.0f, dash1, 0.0f));
    }

    void disableSelectionStyle(Graphics graphics) {
        graphics.setColor(color);
        Graphics2D g2 = (Graphics2D) graphics;
        g2.setStroke(new BasicStroke());
    }


    @Override
    public void paint(Graphics graphics) {
        if (isSelected()) {
            enableSelectionStyle(graphics);
        }
        else {
            disableSelectionStyle(graphics);
        }

        // ...
    }
}
Let's create a Dot class. We will also put it in the *shapes* **package**.
import java.awt.*;

public class Dot extends BaseShape {
    private final int DOT_SIZE = 3;

    public Dot(int x, int y, Color color) {
        super(x, y, color);
    }

    @Override
    public int getWidth() {
        return DOT_SIZE;
    }

    @Override
    public int getHeight() {
        return DOT_SIZE;
    }

    @Override
    public void paint(Graphics graphics) {
        super.paint(graphics);
        graphics.fillRect(x - 1, y - 1, getWidth(), getHeight());
    }
}
Let's also add a Circle to our shapes package.
import java.awt.*;

public class Circle extends BaseShape {
    public int radius;

    public Circle(int x, int y, int radius, Color color) {
        super(x, y, color);
        this.radius = radius;
    }

    @Override
    public int getWidth() {
        return radius * 2;
    }

    @Override
    public int getHeight() {
        return radius * 2;
    }

    public void paint(Graphics graphics) {
        super.paint(graphics);
        graphics.drawOval(x, y, getWidth() - 1, getHeight() - 1);
    }
}
Let's also create a Rectangle shape class.
import java.awt.*;

public class Rectangle extends BaseShape {
    public int width;
    public int height;

    public Rectangle(int x, int y, int width, int height, Color color) {
        super(x, y, color);
        this.width = width;
        this.height = height;
    }

    @Override
    public int getWidth() {
        return width;
    }

    @Override
    public int getHeight() {
        return height;
    }

    @Override
    public void paint(Graphics graphics) {
        super.paint(graphics);
        graphics.drawRect(x, y, getWidth() - 1, getHeight() - 1);
    }
}
```

Lastly we'll create a CompoundShape class that will be extended with the BaseShape class.
import java.awt.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class CompoundShape extends BaseShape {
    protected List <TheRayCode.composite.shapes.Shape> children = new ArrayList<>();

    public CompoundShape(TheRayCode.composite.shapes.Shape... components) {
        super(0, 0, Color.BLACK);
        add(components);
    }

    public void add(TheRayCode.composite.shapes.Shape component) {
        children.add(component);
    }

    public void add(TheRayCode.composite.shapes.Shape... components) {
        children.addAll(Arrays.asList(components));
    }

    public void remove(TheRayCode.composite.shapes.Shape child) {
        children.remove(child);
    }

    public void remove(TheRayCode.composite.shapes.Shape... components) {
        children.removeAll(Arrays.asList(components));
    }

    public void clear() {
        children.clear();
    }

    @Override
    public int getX() {
        if (children.size() == 0) {
            return 0;
        }
        int x = children.get(0).getX();
        for (TheRayCode.composite.shapes.Shape child : children) {
            if (child.getX() < x) {
                x = child.getX();
            }
        }
        return x;
    }

    @Override
    public int getY() {
        if (children.size() == 0) {
            return 0;
        }
        int y = children.get(0).getY();
        for (TheRayCode.composite.shapes.Shape child : children) {
            if (child.getY() < y) {
                y = child.getY();
            }
        }
        return y;
    }

    @Override
    public int getWidth() {
        int maxWidth = 0;
        int x = getX();
        for (TheRayCode.composite.shapes.Shape child : children) {
            int childsRelativeX = child.getX() - x;
            int childWidth = childsRelativeX + child.getWidth();
            if (childWidth > maxWidth) {
                maxWidth = childWidth;
            }
        }
        return maxWidth;
    }

    @Override
    public int getHeight() {
        int maxHeight = 0;
        int y = getY();
        for (TheRayCode.composite.shapes.Shape child : children) {
            int childsRelativeY = child.getY() - y;
            int childHeight = childsRelativeY + child.getHeight();
            if (childHeight > maxHeight) {
                maxHeight = childHeight;
            }
        }
        return maxHeight;
    }

    @Override
    public void move(int x, int y) {
        for (TheRayCode.composite.shapes.Shape child : children) {
            child.move(x, y);
        }
    }

    @Override
    public boolean isInsideBounds(int x, int y) {
        for (TheRayCode.composite.shapes.Shape child : children) {
            if (child.isInsideBounds(x, y)) {
                return true;
            }
        }
        return false;
    }

    @Override
    public void unSelect() {
        super.unSelect();
        for (TheRayCode.composite.shapes.Shape child : children) {
            child.unSelect();
        }
    }

    public boolean selectChildAt(int x, int y) {
        for (Shape child : children) {
            if (child.isInsideBounds(x, y)) {
                child.select();
                return true;
            }
        }
        return false;
    }

    @Override
    public void paint(Graphics graphics) {
        if (isSelected()) {
            enableSelectionStyle(graphics);
            graphics.drawRect(getX() - 1, getY() - 1, getWidth() + 1, getHeight() + 1);
            disableSelectionStyle(graphics);
        }

        for (TheRayCode.composite.shapes.Shape child : children) {
            child.paint(graphics);
        }
    }
}
Let's create an editor package and place in it the ImageEditor class.
import TheRayCode.composite.shapes.CompoundShape;
import TheRayCode.composite.shapes.Shape;

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class ImageEditor {
    private EditorCanvas canvas;
    private CompoundShape allShapes = new CompoundShape();

    public ImageEditor() {
        canvas = new EditorCanvas();
    }

    public void loadShapes(Shape... shapes) {
        allShapes.clear();
        allShapes.add(shapes);
        canvas.refresh();
    }

    private class EditorCanvas extends Canvas {
        JFrame frame;

        private static final int PADDING = 10;

        EditorCanvas() {
            createFrame();
            refresh();
            addMouseListener(new MouseAdapter() {
                @Override
                public void mousePressed(MouseEvent e) {
                    allShapes.unSelect();
                    allShapes.selectChildAt(e.getX(), e.getY());
                    e.getComponent().repaint();
                }
            });
        }

        void createFrame() {
            frame = new JFrame();
            frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            frame.setLocationRelativeTo(null);

            JPanel contentPanel = new JPanel();
            Border padding = BorderFactory.createEmptyBorder(PADDING, PADDING, PADDING, PADDING);
            contentPanel.setBorder(padding);
            frame.setContentPane(contentPanel);

            frame.add(this);
            frame.setVisible(true);
            frame.getContentPane().setBackground(Color.LIGHT_GRAY);
        }

        public int getWidth() {
            return allShapes.getX() + allShapes.getWidth() + PADDING;
        }

        public int getHeight() {
            return allShapes.getY() + allShapes.getHeight() + PADDING;
        }

        void refresh() {
            this.setSize(getWidth(), getHeight());
            frame.pack();
        }

        public void paint(Graphics graphics) {
            allShapes.paint(graphics);
        }
    }
}
Now we create a Demo class.
import TheRayCode.composite.editor.ImageEditor;
import TheRayCode.composite.shapes.Circle;
import TheRayCode.composite.shapes.CompoundShape;
import TheRayCode.composite.shapes.Dot;
import TheRayCode.composite.shapes.Rectangle;

import java.awt.*;

public class Demo {
    public static void main(String[] args) {
        System.out.println("The Ray Code is AWESOME!!!");
        ImageEditor editor = new ImageEditor();

        editor.loadShapes(
                new Circle(10, 10, 10, Color.BLUE),

                new CompoundShape(
                        new Circle(110, 110, 50, Color.RED),
                        new Dot(160, 160, Color.RED)
                ),

                new CompoundShape(
                        new Rectangle(250, 250, 100, 100, Color.GREEN),
                        new Dot(240, 240, Color.GREEN),
                        new Dot(240, 360, Color.GREEN),
                        new Dot(360, 360, Color.GREEN),
                        new Dot(360, 240, Color.GREEN)
                )
        );
    }
}
Let's compile and run. We should get:
![Composite](/UMLs/images/Composite/Composite2.png) The Ray Code is AWESOME!!!
The Ray Code is AWESOME!!!
wikipedia

Find Ray on:

facebook
youtube
The Ray Code
Ray Andrade

Monday, April 19, 2021

Composite pattern c#

TheComposite design pattern is a structural design pattern that lets you compose objects into tree structures and then work with those structures as if they were individual objects.

The component interface declares common operations for both simple and complex objects of a particular composition. The base Component class declares common operations for both the simple and complex objects of a composition.

The base Component may implement some default behavior and leave it to concrete classes (by declaring the methods containing the behavior as abstract). In some cases, it would be beneficial to define the child-management operations right in the base Component class. This way, you won't need to expose any concrete component classes to the client code, even during the object tree assembly. The downside is that these methods will be empty for the leaf-level components. You can provide a method that lets the client code figure out whether a component can bear children.
abstract class Component
{
    public Component() { }
    public abstract string Operation();
    public virtual void Add(Component component)
    {
        throw new NotImplementedException();
    }
    public virtual void Remove(Component component)
    {
        throw new NotImplementedException();
    }
    public virtual bool IsComposite()
    {
        return true;
    }
}
The Leaf class represents the end objects of a composition. A leaf can't have any children.

The Leaf is extend by Component. Usually, it's the Leaf objects that do the actual work, whereas Composite objects only delegate to their sub-components.
class Leaf : Component
{
    public override string Operation()
    {
        return "Leaf";
    }
    public override bool IsComposite()
    {
        return false;
    }
}
The Composite class represents the complex components that may have children. It too is extended with the Component class. Usually, the Composite objects delegate the actual work to their children and then *sum-up* the result.

The Composite executes its primary logic in a particular way. It traverses recursively through all its children, collecting and summing their results. Since the composite's children pass these calls to their children and so forth, the *whole* object tree is traversed as a result.
class Composite : Component
{
    protected List<Component> _children = new List<Component>();
    public override void Add(Component component)
    {
        this._children.Add(component);
    }
    public override void Remove(Component component)
    {
         this._children.Remove(component);
    }
    public override string Operation()
    {
        int i = 0;
        string result = "Branch(";
        foreach (Component component in this._children)
        {
            result += component.Operation();
            if (i != this._children.Count - 1)
            {
                result += "+";
            }
            i++;
        }
        return result + ")";
    }
}
The client code works with all of the components via the base interface.

Thanks to the fact that the child-management operations are declared in the base Component class, the client code can work with any component, simple or complex, without depending on their concrete classes.
class Client
{
    public void ClientCode(Component leaf)
    {
        Console.WriteLine($"RESULT: {leaf.Operation()}\n");
    }
    public void ClientCode2(Component component1, Component component2)
    {
        if (component1.IsComposite())
        {
            component1.Add(component2);
        }
            
        Console.WriteLine($"RESULT: {component1.Operation()}");
    }
}
This way the client code can support the simple leaf components well as the complex composites. Let's put this altogether in the Program class.
class Program
{
    static void Main(string[] args)
    {
        Client client = new Client();

        Leaf leaf = new Leaf();
        Console.WriteLine("Client: I get a simple component:");
        client.ClientCode(leaf);

        Composite tree = new Composite();
        Composite branch1 = new Composite();
        branch1.Add(new Leaf());
        branch1.Add(new Leaf());
        Composite branch2 = new Composite();
        branch2.Add(new Leaf());
        tree.Add(branch1);
        tree.Add(branch2);
        Console.WriteLine("Client: Now I've got a composite tree:");
        client.ClientCode(tree);

        Console.Write("Client: I don't need to check the components classes even when managing the tree:\n");
        client.ClientCode2(tree, leaf);
    }
}
When we compile it and run we should get:
Client: I get a simple component:
RESULT: Leaf

Client: Now I've got a composite tree:
RESULT: Branch(Branch(Leaf+Leaf)+Branch(Leaf))

Client: I don't need to check the components classes even when managing the tree:
RESULT: Branch(Branch(Leaf+Leaf)+Branch(Leaf)+Leaf)

The Ray Code is AWESOME!!!
wikipedia

Find Ray on:

facebook
youtube
The Ray Code
Ray Andrade

Saturday, April 17, 2021

Composite pattern c++

The Composite is a structural design pattern that allows composing objects into a tree-like structure and work with the it as if it was a singular object. Using the Composite pattern makes sense only when the core model of your app can be represented as a tree. The Composite pattern suggests that you work with the objects through a common interface which declares a method for doing some work. The base Component class declares common operations for both simple and complex objects of a composition.
Optionally, the base Component can declare an interface for setting and accessing a parent of the component in a tree structure. It can also provide some default implementation for these methods.
In some cases, it would be beneficial to define the child-management operations right in the base Component class. This way, you won't need to expose any concrete component classes to the client code, even during the object tree assembly. The downside is that these methods will be empty for the leaf-level components.
#include <iostream>

class Component {
protected:
    Component *parent_;

public:
    virtual ~Component() {}
    void SetParent(Component *parent) {
        this->parent_ = parent;
    }
    Component *GetParent() const {
        return this->parent_;
    }
    virtual void Add(Component *component) {}
    virtual void Remove(Component *component) {}
    virtual bool IsComposite() const {
        return false;
    }
    virtual std::string Operation() const = 0;
};
You can provide a method that lets the client code figure out whether a component can bear children. The base Component may implement some default behavior or leave it to concrete classes (by declaring the method containing the behavior as abstract). The Leaf class represents the end objects of a composition. A leaf can't have any children. Usually, it's the Leaf objects that do the actual work, whereas Composite objects only delegate to their sub-components.
class Leaf : Component
{
    public override string Operation()
    {
        return "Leaf";
    }
    public override bool IsComposite()
    {
        return false;
    }
};
The Composite class represents the complex components that may have children. Usually, the Composite objects delegate the actual work to their children and then "sum-up" the result. A composite object can add or remove other components (both simple or complex) to or from its child list. Have in mind that this method removes the pointer to the list but doesn't frees the memory, you should do it manually or better use smart pointers.
#include <list>
#include "Component.h"

class Composite : public Component {
protected:
    std::list<Component *> children_;

public:
    void Add(Component *component) override {
        this->children_.push_back(component);
        component->SetParent(this);
    }
    void Remove(Component *component) override {
        children_.remove(component);
        component->SetParent(nullptr);
    }
    bool IsComposite() const override {
        return true;
    }
The Composite executes its primary logic in a particular way. It traverses recursively through all its children, collecting and summing their results. Since the composite's children pass these calls to their children and so forth, the whole object tree is traversed as a result.
    std::string Operation() const override {
        std::string result;
        for (const Component *c : children_) {
            if (c == children_.back()) {
                result += c->Operation();
            } else {
                result += c->Operation() + "+";
            }
        }
        return "Branch(" + result + ")";
    }
};
The Leaf class represents the end objects of a composition. A leaf can't have any children. Usually, it's the Leaf objects that do the actual work, whereas Composite objects only delegate to their sub-components. Now let's go to the main.cpp The client code works with all of the components via the base interface.
#include "Leaf.h"
#include "Composite.h"
void ClientCode(Component *component) {
    // ...
    std::cout << "RESULT: " << component->Operation();
    // ...
}
Thanks to the fact that the child-management operations are declared in the base Component class, the client code can work with any component, simple or complex, without depending on their concrete classes.
void ClientCode2(Component *component1, Component *component2) {
    if (component1->IsComposite()) {
        component1->Add(component2);
    }
    std::cout << "RESULT: " << component1->Operation();
}
This way the client code can support the simple leaf components...
int main() {
    Component *simple = new Leaf;
    std::cout << "Client: I've got a simple component:\n";
    ClientCode(simple);
    std::cout << "\n\n";
...as well as the complex composites.
    Component *tree = new Composite;
    Component *branch1 = new Composite;

    Component *leaf_1 = new Leaf;
    Component *leaf_2 = new Leaf;
    Component *leaf_3 = new Leaf;
    branch1->Add(leaf_1);
    branch1->Add(leaf_2);
    Component *branch2 = new Composite;
    branch2->Add(leaf_3);
    tree->Add(branch1);
    tree->Add(branch2);
    std::cout << "Client: Now I've got a composite tree:\n";
    ClientCode(tree);
    std::cout << "\n\n";

    std::cout << "Client: I don't need to check the components classes even when managing the tree:\n";
    ClientCode2(tree, simple);
    std::cout << "\n";

    delete simple;
    delete tree;
    delete branch1;
    delete branch2;
    delete leaf_1;
    delete leaf_2;
    delete leaf_3;
    return 0;
}    
Now let's compile and run. We should get: ```run Client: I've got a simple component: RESULT: Leaf Client: Now I've got a composite tree: RESULT: Branch(Branch(Leaf+Leaf)+Branch(Leaf)) Client: I don't need to check the components classes even when managing the tree: RESULT: Branch(Branch(Leaf+Leaf)+Branch(Leaf)+Leaf) ```
The Ray Code is AWESOME!!!
wikipedia

Find Ray on:

facebook
youtube
The Ray Code
Ray Andrade

JavaScript Command Design Pattern Explained | UML, Code & SWOT Analysis

🕹️ What Is the Command Design Pattern? The Command Design Pattern is one of the 11 Behavioral Design Patterns described ...