Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

composite design pattern

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

/** "Component" */
interface Graphic {
    //Prints the graphic.
    public void print();
}

/** "Composite" */
class CompositeGraphic implements Graphic {
    //Collection of child graphics.
    private final List<Graphic> childGraphics = new ArrayList<>();

    //Adds the graphic to the composition.
    public void add(Graphic graphic) {
        childGraphics.add(graphic);
    }
    
    //Prints the graphic.
    @Override
    public void print() {
        for (Graphic graphic : childGraphics) {
            graphic.print();  //Delegation
        }
    }
}

/** "Leaf" */
class Ellipse implements Graphic {
    //Prints the graphic.
    @Override
    public void print() {
        System.out.println("Ellipse");
    }
}

/** Client */
class CompositeDemo {
    public static void main(String[] args) {
        //Initialize four ellipses
        Ellipse ellipse1 = new Ellipse();
        Ellipse ellipse2 = new Ellipse();
        Ellipse ellipse3 = new Ellipse();
        Ellipse ellipse4 = new Ellipse();

        //Creates two composites containing the ellipses
        CompositeGraphic compositGraphic2 = new CompositeGraphic();
        compositGraphic2.add(ellipse1);
        compositGraphic2.add(ellipse2);
        compositGraphic2.add(ellipse3);
        
        CompositeGraphic compositGraphic3 = new CompositeGraphic();
        compositGraphic3.add(ellipse4);
        
        //Create another graphics that contains two graphics
        CompositeGraphic compositGraphic = new CompositeGraphic();
        compositGraphic.add(compositGraphic2);
        compositGraphic.add(compositGraphic3);

        //Prints the complete graphic (Four times the string "Ellipse").
        compositGraphic.print();
    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: java generate random string and number 
Java :: java compare char 
Java :: java remove double spaces 
Java :: lombok 
Java :: java hello world program 
Java :: can you use java in unity 
Java :: java format double no decimal places 
Java :: maven set repository location command line 
Java :: Java Access HashMap Elements 
Java :: ArrayIndexOutOfBoundsException 
Java :: local inner class in java 
Java :: Add an instance variable of type Safe to the class Room. This instance variable should be initialised in the constructor of Room, and an appropriate query should be defined to get it. 
Java :: mambalam srardham online booking 
Java :: bootstrap error message 
Sql :: mysql create user with mysql_native_password 
Sql :: sql server 2016 reseed identity 
Sql :: oracle get table schema 
Sql :: postgresql server restart 
Sql :: delete sql row by id 
Sql :: Duplicating a MySQL table with all the data Command 
Sql :: neo4j display all nodes and relationships 
Sql :: how to increase size of column in sql 
Sql :: installer postgresql sur ubuntu 
Sql :: mysql connectorj maven de 
Sql :: oracle search source code 
Sql :: delete database mysql 
Sql :: oracle service name view 
Sql :: find a column in all tables postgres 
Sql :: mysql concat two columns laravel eloquent 
Sql :: psql list rules 
ADD CONTENT
Topic
Content
Source link
Name
1+4 =