Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

How to implement Composite Design Pattern in Java ?.

Program to demonstrate how to implement Composite Design Pattern in Java.

1. Geographical.java 

package com.hubberspot.designpattern.structural.composite;

// This acts as a interface for every State(Leaf) and
// Country(Composite)
public abstract class GeographicalArea {

    // Name of the region 
    private String areaName;    

    public GeographicalArea(String areaName) {

        this.areaName = areaName;
    }    

    public String getAreaName() {
        return areaName;
    }

    // Create methods which will throw UnsupportedOperationException

    public void addGeographicalArea(GeographicalArea geographicalArea) {

        throw new UnsupportedOperationException(
                "Invalid Operation. Not Supported");
    }
    public GeographicalArea getGeographicalArea(int areaIndex) {

        throw new UnsupportedOperationException(
                "Invalid Operation. Not Supported");
    }

    public long getPopulation() {

        throw new UnsupportedOperationException(
                "Invalid Operation. Not Supported");
    }

    // Create a abstract method which will be implemented
    // by State and Country respectively
    public abstract void displayAreaInfo();

}



2. Country.java


package com.hubberspot.designpattern.structural.composite;

import java.util.ArrayList;
import java.util.List;

// It extends GeographicalArea and has a list of 
// objects containing the GeographicalArea such as 
// State in this case.
public class Country extends GeographicalArea {

    List< GeographicalArea > listOfGeographicalArea = new ArrayList< GeographicalArea >();

    public Country(String areaName) {
        super(areaName);
    }

    // adding states to country
    @Override
    public void addGeographicalArea(GeographicalArea geographicalArea)
            throws UnsupportedOperationException {
        listOfGeographicalArea.add(geographicalArea);
    }

    // getting states one by one based on index
    @Override
    public GeographicalArea getGeographicalArea(int areaIndex)
            throws UnsupportedOperationException {

        return listOfGeographicalArea.get(areaIndex);

    }

    // displaying the information for the country
    @Override
    public void displayAreaInfo() {

        System.out.println("Country name : " + getAreaName());
        System.out.println("It has following info : ");

        long totalPopulation = 0;
        int index = 1;

        System.out.println("It has following states : ");
        for(GeographicalArea geographicalArea : listOfGeographicalArea) {

            System.out.println(index + " : "+ geographicalArea.getAreaName() );
            System.out.println("It has population around : " + geographicalArea.getPopulation());
            totalPopulation = totalPopulation + geographicalArea.getPopulation();
            index++;
        }

        System.out.println("Total Population of "+ getAreaName()+ " is : " + totalPopulation);

    }

}



3. State.java


package com.hubberspot.designpattern.structural.composite;

//It extends GeographicalArea
public class State extends GeographicalArea {

    // has a property called as population 
    private long population;


    public long getPopulation() {
        return population;
    }

    public State(String areaName, long population) {
        super(areaName);
        this.population = population;        
    }

    // displaying the information for the states
    @Override
    public void displayAreaInfo() {

        System.out.println("State name : " + getAreaName());
        System.out.println("State population : " + getPopulation());

    }

}


4. TestCompositePattern.java

package com.hubberspot.designpattern.structural.composite;

public class TestCompositePattern {

    public static void main(String[] args) {

        // Create two countries such as India and USA
        GeographicalArea countryIndia = new Country("India");
        GeographicalArea countryUSA = new Country("USA");

        // Create two Indian States with name and population
        GeographicalArea stateMadhyaPradesh = new State("Madhya Pradesh", 10000000);
        GeographicalArea stateMaharashtra = new State("Maharashtra", 20000000);

        // Create two USA States with name and population
        GeographicalArea stateNewYork = new State("New York", 19570261);
        GeographicalArea stateWashington = new State("Washington", 6897012);

        // Add the Indian States to India 
        countryIndia.addGeographicalArea(stateMadhyaPradesh);
        countryIndia.addGeographicalArea(stateMaharashtra);

        // Add the USA States to USA
        countryUSA.addGeographicalArea(stateNewYork);
        countryUSA.addGeographicalArea(stateWashington);

        // Display Info for country India 
        countryIndia.displayAreaInfo();
        System.out.println();

        // Display Info for country USA
        countryUSA.displayAreaInfo();

    }

}



Output of the program :
 
© 2021 Learn Java by Examples Template by Hubberspot