Skip to content

Instantly share code, notes, and snippets.

@ericyhliu
Created January 8, 2018 02:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericyhliu/19c81cc785d8dfed84bc852a0488c810 to your computer and use it in GitHub Desktop.
Save ericyhliu/19c81cc785d8dfed84bc852a0488c810 to your computer and use it in GitHub Desktop.
Design Patterns - Composite - CompositeComponent
package com.algorithmhelper.softwareengineering.designpatterns.composite;
import com.algorithmhelper.datastructures.interfaces.List;
import com.algorithmhelper.datastructures.lists.DynamicArray;
public class CompositeComponent implements AbstractComponent {
private static int globalID = 0;
private int id = globalID++;
private List<AbstractComponent> components;
/**
* Initializes a CompositeComponent.
*/
public CompositeComponent() {
components = new DynamicArray<>();
}
/**
* Inserts an AbstractComponent to the list of components.
*
* @param component, the AbstractComponent
*/
public void addComponent(AbstractComponent component) {
components.insertBack(component);
}
/**
* Performs some operation on the CompositeComponent.
*/
public void operation() {
System.out.println("from CompositeComponent with id " + id);
for (AbstractComponent component : components)
component.operation();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment