Created
January 8, 2018 02:16
-
-
Save ericyhliu/19c81cc785d8dfed84bc852a0488c810 to your computer and use it in GitHub Desktop.
Design Patterns - Composite - CompositeComponent
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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