mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-17 16:59:32 +00:00
added composite sample
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
package com.iluwatar;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public abstract class LetterComposite {
|
||||
|
||||
private List<LetterComposite> children = new ArrayList<LetterComposite>();
|
||||
|
||||
public void add(LetterComposite letter) {
|
||||
children.add(letter);
|
||||
}
|
||||
|
||||
public int count() {
|
||||
return children.size();
|
||||
}
|
||||
|
||||
protected abstract void printThisBefore();
|
||||
|
||||
protected abstract void printThisAfter();
|
||||
|
||||
public void print() {
|
||||
printThisBefore();
|
||||
for (LetterComposite letter: children) {
|
||||
letter.print();
|
||||
}
|
||||
printThisAfter();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user