Composite Design Pattern

Composite is a structural design pattern that allows you to compose objects into tree structures and then work with these structures as if they were individual objects.

The application needs to manipulate a hierarchical collection of “primitive” and “compound” objects. Processing of a primitive object is handled in one way and processing of a composite object is handled differently. You don’t want to have to query the “type” of each object before trying to process it.

Define an abstract base class (Component) that specifies the behavior to be exerted uniformly on all primitive and composite objects. Subclass the Primitive and Composite classes outside of the Component class. Each composite object “docks” itself only with the abstract type component, since it manages its “children”.

Use this pattern whenever you have “compounds that contain components, each of which could be a compound.”

Let’s look at the example I wrote as below.

PHP Code Sample

<?php

/**
 * This is the common interface of the composite pattern, declares the necessary operations here.
 * this might be a set of complex operations under here in a real world application.
 */
interface Common
{
   public function report();
}

/**
 * Leaf class where there is no any children under this.
 */
class Individual implements Common
{
   private $name;
   private $total;

   public function __construct($name,$total)
   {
       $this->name = $name;
       $this->total = $total;
   }
   public function report()
   {
       echo "$this->name got $this->total\n";
   }
}

/**
 * composite class where has complex component structure.
 * getting to each target children to get the final result
 */
class Composition implements Common
{
   public $report_list = [];

   public function addRecord($list)
   {
       $this->report_list[] = $list;
   }

  /**
   * traversing child nodes.
   */
   public function report()
   {
       foreach($this->report_list as $list) {
           $list->report();
       }
   }
}

/**
 * client code can now access both individual and composite class without acknowleding the concrete implementations.
 */
$p1 = new Individual("Mg Mg",100);
$p2 = new Individual("Hla Hla",200);

$list = new Composition();
$list->addRecord($p1);
$list->addRecord($p2);
$list->report();

?>


C# Code Sample

using System;
using System.Collections.Generic;

namespace HelloWorld
{
    /**
     * This is the common interface of the composite pattern, declares the necessary operations here.
     * this might be a set of complex operations under here in a real world application.
     */
    interface Component
    {
        string report();
    }

    /**
     * Leaf class where there is no any children under this.
     */
    class Individual : Component
    {
       
        private string _name;
        private int _total;

        public Individual(string name,int total)
        {
            this._name = name;
            this._total = total;
        }
        public string report()
        {
            return _name + " got " + _total;
        }
    }

    /**
     * composite class where has complex component structure.
     * getting to each target children to get the final result
     */
    class Composition : Component
    {
        protected List<Component> _lists = new List<Component>();

        public string report()
        {
            int i = 0;
            string result = "";

            foreach (Component component in this._lists)
            {
                result += component.report();
                if (i != this._lists.Count - 1)
                {
                    result += "\n";
                }
                i++;
            }

            return result;
        }

        public void Add(Component component)
        {
            this._lists.Add(component);
        }

    }

    class Program
    {
        static void Main(string[] args)
        {

            /**
             * client code can now access both individual and composite class without acknowleding the concrete implementations.
             */
            var p1 = new Individual("Mg Mg", 100);
            var p2 = new Individual("Hla Hla", 200);

            var list = new Composition();
            list.Add(p1);
            list.Add(p2);
            Console.Write(list.report());
        }
    }

}

By Yuuma.



アプリ関連ニュース

お問い合わせはこちら

お問い合わせ・ご相談はお電話、またはお問い合わせフォームよりお受け付けいたしております。

tel. 06-6454-8833(平日 10:00~17:00)

お問い合わせフォーム