Para exercitar a utilização do padrão de projeto Composite, desenvolva um programa que apresente a somatória dos elementos armazenados num vetor, numa matriz e/ou num cubo.
/*************************************************************************
* Copyright (C) 2009/2025 - Cristiano Lehrer (cristiano@ybadoo.com.br) *
* Ybadoo - Solucoes em Software Livre (ybadoo.com.br) *
* *
* Permission is granted to copy, distribute and/or modify this document *
* under the terms of the GNU Free Documentation License, Version 1.3 or *
* any later version published by the Free Software Foundation; with no *
* Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A *
* A copy of the license is included in the section entitled "GNU Free *
* Documentation License". *
* *
* Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic) *
* OpenJDK Version "1.8.0_121" *
* OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode) *
*************************************************************************/
package com.ybadoo.tutoriais.poo.tutorial09.exercicio27;
import java.io.Serializable;
/**
* Interface para indicação de objetos contáveis
*/
public interface Countable extends Serializable
{
/**
* Retornar a quantidade de objetos contáveis
*
* @return quantidade de objetos contáveis
*/
public Number count();
}
/*************************************************************************
* Copyright (C) 2009/2025 - Cristiano Lehrer (cristiano@ybadoo.com.br) *
* Ybadoo - Solucoes em Software Livre (ybadoo.com.br) *
* *
* Permission is granted to copy, distribute and/or modify this document *
* under the terms of the GNU Free Documentation License, Version 1.3 or *
* any later version published by the Free Software Foundation; with no *
* Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A *
* A copy of the license is included in the section entitled "GNU Free *
* Documentation License". *
* *
* Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic) *
* OpenJDK Version "1.8.0_121" *
* OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode) *
*************************************************************************/
package com.ybadoo.tutoriais.poo.tutorial09.exercicio27;
/**
* Definição dos elementos da composição
*
* Component - declara a interface para os objetos na composição;
* - implementa comportamento por falta para a interface comum a
* todas as classes, conforme apropriado;
* - declara uma interface para acessar e gerenciar os seus
* componentes-filhos;
* - (opcional) define uma interface para acessar o pai de um
* componente na estrutura recursiva e a implementa, se isso
* for apropriado (Gamma, 2000).
*
* @param <T> elemento contável
*/
public interface Element<T extends Countable>
{
/**
* Adicionar o elemento na composição
*
* @param element elemento
*/
public void add(Element<T> element);
/**
* Remover o elemento da composição
*
* @param element elemento
*/
public void remove(Element<T> element);
/**
* Retornar a somatória dos elementos da composição
*
* @return somatória dos elementos da composição
*/
public Number sum();
}
/*************************************************************************
* Copyright (C) 2009/2025 - Cristiano Lehrer (cristiano@ybadoo.com.br) *
* Ybadoo - Solucoes em Software Livre (ybadoo.com.br) *
* *
* Permission is granted to copy, distribute and/or modify this document *
* under the terms of the GNU Free Documentation License, Version 1.3 or *
* any later version published by the Free Software Foundation; with no *
* Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A *
* A copy of the license is included in the section entitled "GNU Free *
* Documentation License". *
* *
* Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic) *
* OpenJDK Version "1.8.0_121" *
* OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode) *
*************************************************************************/
package com.ybadoo.tutoriais.poo.tutorial09.exercicio27;
/**
* Item unitário da composição
*
* Leaf - representa objetos-folha na composição. Uma folha não tem filhos;
* - define comportamento para objetos primitivos na composição
* (Gamma, 2000).
*
* @param <T> elemento contável
*/
public class Item<T extends Countable> implements Element<T>
{
/**
* Elemento contável
*/
private Countable value;
/**
* Inicializar o valor do elemento contável
*
* @param value valor do elemento contável
*/
public Item(Countable value)
{
this.value = value;
}
/* (non-Javadoc)
* @see com.ybadoo.tutoriais.poo.tutorial09.exercicio27.Element
* #add(com.ybadoo.tutoriais.poo.tutorial09.exercicio27.Element)
*/
@Override
public void add(Element<T> element)
{
throw new UnsupportedOperationException();
}
/* (non-Javadoc)
* @see com.ybadoo.tutoriais.poo.tutorial09.exercicio27.Element
* #remove(com.ybadoo.tutoriais.poo.tutorial09.exercicio27.Element)
*/
@Override
public void remove(Element<T> element)
{
throw new UnsupportedOperationException();
}
/* (non-Javadoc)
* @see com.ybadoo.tutoriais.poo.tutorial09.exercicio27.Element#sum()
*/
@Override
public Number sum()
{
return value.count();
}
}
/*************************************************************************
* Copyright (C) 2009/2025 - Cristiano Lehrer (cristiano@ybadoo.com.br) *
* Ybadoo - Solucoes em Software Livre (ybadoo.com.br) *
* *
* Permission is granted to copy, distribute and/or modify this document *
* under the terms of the GNU Free Documentation License, Version 1.3 or *
* any later version published by the Free Software Foundation; with no *
* Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A *
* A copy of the license is included in the section entitled "GNU Free *
* Documentation License". *
* *
* Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic) *
* OpenJDK Version "1.8.0_121" *
* OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode) *
*************************************************************************/
package com.ybadoo.tutoriais.poo.tutorial09.exercicio27;
import java.util.ArrayList;
import java.util.List;
/**
* Definição da composição de elementos contáveis
*
* Composite - define comportamento para componentes que têm filhos;
* - armazena os componentes-filho;
* - implementa as operações relacionadas com os filhos presentes
* na interface de Component (Gamma, 2000).
*
* @param <T> elemento contável
*/
public class Array<T extends Countable> implements Element<T>
{
/**
* Lista de elementos contáveis
*/
private List<Element<T>> elements;
/**
* Inicializar a composição de elementos contáveis
*/
public Array()
{
elements = new ArrayList<>();
}
/* (non-Javadoc)
* @see com.ybadoo.tutoriais.poo.tutorial09.exercicio27.Element
* #add(com.ybadoo.tutoriais.poo.tutorial09.exercicio27.Element)
*/
@Override
public void add(Element<T> element)
{
elements.add(element);
}
/* (non-Javadoc)
* @see com.ybadoo.tutoriais.poo.tutorial09.exercicio27.Element
* #remove(com.ybadoo.tutoriais.poo.tutorial09.exercicio27.Element)
*/
@Override
public void remove(Element<T> element)
{
elements.remove(element);
}
/* (non-Javadoc)
* @see com.ybadoo.tutoriais.poo.tutorial09.exercicio27.Element#sum()
*/
@Override
public Number sum()
{
Integer sum = 0;
for(Element<T> element: elements)
{
sum = sum + element.sum().intValue();
}
return sum;
}
}
/*************************************************************************
* Copyright (C) 2009/2025 - Cristiano Lehrer (cristiano@ybadoo.com.br) *
* Ybadoo - Solucoes em Software Livre (ybadoo.com.br) *
* *
* Permission is granted to copy, distribute and/or modify this document *
* under the terms of the GNU Free Documentation License, Version 1.3 or *
* any later version published by the Free Software Foundation; with no *
* Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A *
* A copy of the license is included in the section entitled "GNU Free *
* Documentation License". *
* *
* Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic) *
* OpenJDK Version "1.8.0_121" *
* OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode) *
*************************************************************************/
package com.ybadoo.tutoriais.poo.tutorial09.exercicio27;
/**
* Classe para representar um estudante
*/
public class Student implements Countable
{
/**
* Identificador de serialização da classe
*/
private static final long serialVersionUID = 1L;
/* (non-Javadoc)
* @see com.ybadoo.tutoriais.poo.tutorial09.exercicio27.Countable#count()
*/
@Override
public Number count()
{
return new Integer(1);
}
}
/*************************************************************************
* Copyright (C) 2009/2025 - Cristiano Lehrer (cristiano@ybadoo.com.br) *
* Ybadoo - Solucoes em Software Livre (ybadoo.com.br) *
* *
* Permission is granted to copy, distribute and/or modify this document *
* under the terms of the GNU Free Documentation License, Version 1.3 or *
* any later version published by the Free Software Foundation; with no *
* Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A *
* A copy of the license is included in the section entitled "GNU Free *
* Documentation License". *
* *
* Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic) *
* OpenJDK Version "1.8.0_121" *
* OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode) *
*************************************************************************/
package com.ybadoo.tutoriais.poo.tutorial09.exercicio27;
/**
* Classe responsavel pela execucao do padrao Composite
*
* Client - manipula objetos na composição através da interface de
* Component (Gamma, 2000).
*/
public class Application
{
/**
* Construtor para inicializar a execucao do padrao Composite
*/
private Application()
{
}
/**
* Metodo principal da linguagem de programacao Java
*
* @param args argumentos da linha de comando (nao utilizado)
*/
public static void main(String[] args)
{
Item<Student> student = new Item<>(new Student());
System.out.println("Estudante: " + student.sum());
Array<Student> room = new Array<>();
room.add(student);
room.add(student);
room.add(student);
room.add(student);
room.add(student);
System.out.println("Estudantes em sala de aula: " + room.sum());
Array<Student> course = new Array<>();
course.add(room);
course.add(room);
course.add(room);
course.add(room);
course.add(room);
System.out.println("Estudantes no curso: " + course.sum());
Array<Student> college = new Array<>();
college.add(course);
college.add(course);
college.add(course);
college.add(course);
college.add(course);
System.out.println("Estudantes na faculdade: " + college.sum());
}
}