Composite design pattern

In software engineering, the composite pattern is a partitioning design pattern. The composite pattern describes a group of objects that is treated the same way as a single instance of the same type of object. The intent of a composite is to "compose" objects into tree structures to represent part-whole hierarchies. Implementing the composite pattern lets clients treat individual objects and compositions uniformly.

[Wikipedia]

Intro

In this example we will create Soldiers and commandares into pikud merkaz , and pikud darom. This recruit will get the soldiers as an argument. Then we will combine the pikuds by recruting both into pikud "all". This merge will get the pikud as an argument.  

Soldier

public interface Soldier {
  public void printSoldierDetails();
}

BootcampSoldier

public class BootcampSoldier implements Soldier
{
    private String name;
    private long armyId;
    private String dateOfRecruit;

    public BootcampSoldier(long armyId, String name, String dateOfRecruit)
    {
        this.name = name;
        this.armyId = armyId;
        this.dateOfRecruit = dateOfRecruit;
    }

    @Override
    public void printSoldierDetails()
    {
        System.out.println("Soldier " + name+ ",ID: " + armyId + " which joined the army at " + dateOfRecruit);
    }
}

Commander

public class Commander implements Soldier
{
    private String name;
    private long armyId;
    private String dateOfRecruit;

    public Commander(long armyId, String name, String dateOfRecruit)
    {
        this.name = name;
        this.armyId = armyId;
        this.dateOfRecruit = dateOfRecruit;
    }

    @Override
    public void printSoldierDetails()
    {
        System.out.println("Commander " + name+ ",ID: " + armyId + " which joined the army at " + dateOfRecruit);
    }
}

Pikud

import java.util.ArrayList;

public class Pikud implements Soldier{
        private ArrayList allSoldiers = new ArrayList();
        private boolean isJail=false;
        @Override
        public void printSoldierDetails()
        {
            for(Soldier soldier:allSoldiers)
                soldier.printSoldierDetails();
        }

        public void recruitSoldier(Soldier s)
        {
            allSoldiers.add(s);
        }

        public void dissmissSoldier(Soldier s)
        {
            s.printSoldierDetails();
            System.out.println("Has been released from the army.");
            allSoldiers.remove(s);
        }

        public void insertToJail( Soldier s){
            isJail=true;
            s.printSoldierDetails();
            System.out.println("is now in jail.");
        }
}

Main

public class Main {
        /*
        In this example we will create Soldiers and commandares into pikud merkaz , and pikud darom. 
        This recruit will get the soldiers as  an argument. Then we will combine the pikuds by recruting both into pikud "all".
        This merge will get the pikud as an argument.  
        */
        
        public static void main (String[] args)
        {
            BootcampSoldier sol1 = new BootcampSoldier(5423261, "Tal Baum", "November 2010");
            BootcampSoldier sol2 = new BootcampSoldier(6487622, "Elad Shamailov", "December 2010");
            Pikud pikudDarom = new Pikud();
            pikudDarom.recruitSoldier(sol1);
            pikudDarom.recruitSoldier(sol2);

            Commander commander1 = new Commander(1113332, "Benny Gantz", "July 1978");
            Commander commander2 = new Commander(1237892, "Gadi Eizenkot ", "March 1982");

            Pikud pikudMerkaz = new Pikud();
            pikudMerkaz.recruitSoldier(commander1);
            pikudMerkaz.recruitSoldier(commander2);

            Pikud all = new Pikud();
            all.recruitSoldier(pikudDarom);
            all.recruitSoldier(pikudMerkaz);
            System.out.println("Print all the Soldiers details:");
            all.printSoldierDetails();
            System.out.println("----------------------------------------------");
            System.out.println("Inesrt a soldier into the prison:");
            all.insertToJail(sol2);
            System.out.println("----------------------------------------------");
            all.dissmissSoldier(sol1);
        }
}

Running Example

Print all the Soldiers details:
Soldier Tal Baum,ID: 5423261 which joined the army at November 2010
Soldier Elad Shamailov,ID: 6487622 which joined the army at December 2010
Commander Benny Gantz,ID: 1113332 which joined the army at July 1978
Commander Gadi Eizenkot ,ID: 1237892 which joined the army at March 1982
----------------------------------------------
Inesrt a soldier into the prison:
Soldier Elad Shamailov,ID: 6487622 which joined the army at December 2010
is now in jail.
----------------------------------------------
Soldier Tal Baum,ID: 5423261 which joined the army at November 2010
Has been released from the army.